C Problem (Reading in input from user with loops)
Hi this is just a simple question for programing in C.
I have my main function using a while loop that gets 1 character at a time from the user.
When the user enter '1' it calls a different method add().
add() also asks the user for some input and does a simliair loop asking scanning in 1 character at a time.. The problem is that when I try inputing something in now it jumps back to the main loop.
For Example:
Please enter choice from 1-6:
(I enter 1)
It calls add()
Please enter number:
(I enter 54)
It goes back to the main method without doing anything and gives me:
Error: Please only enter number from 1-6.
How do i make it so it stays in the add method until i actually return?
Thx.
[790 byte] By [
p1kn1c] at [2007-11-11 8:01:45]

# 1 Re: C Problem (Reading in input from user with loops)
You need to show us your loop.
In short, you add() function has to have a similar loop that reads and validated input.
Danny at 2007-11-11 21:01:45 >

# 2 Re: C Problem (Reading in input from user with loops)
I sorta fixed the inital problem yet i still have one.
This is the code. If you enter 1 from the initial menu it will go to the add method and ask for input..which works somewhat.
The only problem i have now is that it always gets invalid input.
Whats supposed to happen is you enter a Nameofitem then a space then a price.
Like: Banana 1.34. it reads each thing 1 by one..it gets the right value and all however at the end it still says invalid.
I thinks its because its forsomereson getting the last char to be " " or something. Im sorta stuck trying to fix this so if you got any ideas please help :D. Here is the code:
#include <stdio.h>
double total=0;
int items=0;
void printMenu();
void addItem();
int checkInput(char);
int main(void){
char c;
printMenu();
scanf("%c",&c);
while(c!=EOF){
if(c=='1'){
addItem();
}
else if(c=='2'){
printf("2\n");
}
else if(c=='3'){
printf("3\n");
}
else if(c=='4'){
}
else if(c=='5'){
total=0;
items=0;
printf("Cash register has been reset\n");
}
else if(c=='6'){
printf("Quitting, have a nice day\n");
exit(-1);
}
else{
printf("Please only eneter a choice from 1 to 6\n");
}
printMenu();
scanf("%c",&c);
scanf("%c",&c);
}
return(0);
}
void printMenu(){
printf("1. Add Item\n2. ComputeTotal\n3. Compute Average\n4. Compute Change\n5. Reset Cash-register\n6. Quit\nEnter Option:\n");
return;
}
void addItem(){
char x='n';
double price=0.0;
int findNum=0;
int decimals=0;
int check=0;
printf("Please enter an item to add\n");
while(x!=EOF){
scanf("%c",&x);
if(x==' '){
findNum=1;
continue;
}
else if(x=='.'){
decimals=1;
continue;
}
else if(findNum){
check = checkInput(x);
if(check==1 && decimals==0){
/*found an acutal number (before the decimal) so add it to the price*/
price=(price*10)+(double)(x-'0');
continue;
}
else if(check==1 && (decimals==1 || decimals==2)){
/*found an actual number after decimal*/
if(decimals==1){
price=price+((double)(x-'0')/10);
}
if(decimals==2){
price=price+((double)(x-'0')/100);
}
decimals++;
continue;
}
else if(check==1 && decimals==3){
printf("too many decimals in number..invalid input please try again\n");
addItem();
}
else if(check==0){
printf("%f",price);
printf("INVALID INPUT, please try again\n");
addItem();
}
}
}
total=total+price;
items++;
return;
}
int checkInput(char x){
if((x>='0') &&(x<='9')){
return(1);
}
else{
return(0);
}
}
p1kn1c at 2007-11-11 21:02:40 >

# 3 Re: C Problem (Reading in input from user with loops)
I think you need to change the input type to a whole string, not just a single char. The problem is that when you read a char and the user presses something like 1+Enter, then the remaining char stays in the input buffer. It's read when the next input statement takes place.
Danny at 2007-11-11 21:03:39 >

# 5 Re: C Problem (Reading in input from user with loops)
if you cant use arrays, you can flush the input stream using fflush function.
common usage:
fflush(stdin);
try this. meanwhile i shall give u some more inputs after working on this prob.
bye for now
jwala05 :)