?
Solved

Beginner to C - Please help with scanf()

Posted on 2003-03-10
2
Medium Priority
?
210 Views
Last Modified: 2010-04-15
Hi,
      I am trying to read in an numeric variable with scanf(), then, based upon logic, goto the correct section of my program. I find that if I input an Alpha character instead of a number, it loops into hell.  Any ideas how I can implement error checking or force numeric input. My initial section of read in code is below:

Thanks
Paul

int operation,choice;
option:printf("\nMAIN MENU\n\n\n");
  printf("1: Dollars to some other currency\n\n");
  printf("\n2: Euros to dollars\n\n");
  printf("\n3: Display currency equivalents for USD\n\n\n");
  printf("\n4: Quit this program\n\n");/*Advise user of options*/
  operation=0; /*set menu choice default to zero*/
  scanf("%d",&operation);/*read in user option for operation to perform*/
  if(operation == 1)goto conv1; /*CHOICE 1 ADVANCE TO CONV1 LABEL*/
  if(operation == 2)goto conv2; /*CHOICE 2 ADVANCE TO CONV2 LABEL*/
  if(operation == 3)goto display; /*CHOICE 2 ADVANCE TO DISPLAY LABEL*/
  if(operation == 4)goto leave; /*CHOICE 3 ADVANCE TO LEAVE LABEL*/
  else goto oops;    /*ANY OTHER CHOICE ADVANCE TO OOPS LABEL*/
oops:printf("\n\nPLEASE CHOOSE A VALID OPTION");goto option;  /*LOOP BACK TO MAIN OPTIONS MENU ON INVALID CHOICE*/
0
Comment
Question by:simpsop
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
gj62 earned 300 total points
ID: 8105230
It is generally considered better to do this:

char inputStr[20]; - big enough to hold a reasonable number of chars...

scanf("%s", inputStr);
operation = atoi(inputStr);
if (operation < 1 || operation > 4)
   printf("please enter a number between 1 and 4\n");

etc...
0
 

Author Comment

by:simpsop
ID: 8105362
Perfect! Worked like a charm.

Thank you for the fast response!

Paul
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question