While Statement Issue.
-
In the below code I have put together a text example of a shopping cart whereby the user selects a commodity to purchase and based upon a menu selection, tells the program which leg in my switch statement to execute. After a selection is made, the program drops out of the switch statement and proceeds the the bottom of the while statement where "ordercom" tells the while statement to continue taking orders. This is obviously an un-ending loop but is coded this way to trouble shoot the issue at hand. I am testing the code with code breaks. What is happening is that after I select two menu selections or two loops of the while statement the program skips over the scanf statement. Can someone help me with this issue?
#include <stdio.h>
#define Artichoke 1.25
#define Beets .65
#define Carrots .89
#define Discount .05
#define Shipping1 10.00
#define Shipping2 8.00int main(void)
{
char ordercom;
char type;
float pounds;
float gross;
float poundTot = 0;
ordercom = 'Y';
while(ordercom == 'Y')
{
printf("*******************************************************************************************************************\n");
printf("Determine What You Want To Buy\n");
printf("1) (a)\t Artichokes\t 3) (c)\t Carrots\n");
printf("2) (b)\t Beets\t 4) (q)\t Quit\n");
printf("*******************************************************************************************************************\n");
printf("\n");
printf("Select The Type Of Vegetable You Want And The Pounds Of The Vegetable You Want Or (q) To Quit :");
scanf("%c ""%f",&type,£s);switch(type) { case 'a' : { gross = pounds \* Artichoke; break; } case 'b' : { gross = pounds \* Beets; break; } case 'c' : { gross = pounds \* Carrots; break; } case 'q' : { break; } default : { break; } } ordercom = 'Y'; } }
Mike Certini wrote:
What is happening is that after I select two menu selections or two loops of the while statement the program skips over the scanf statement. Can someone help me with this issue?
Likely because something is in the keyboard buffer. Try using
fflush()
afterscanf()
."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
In the below code I have put together a text example of a shopping cart whereby the user selects a commodity to purchase and based upon a menu selection, tells the program which leg in my switch statement to execute. After a selection is made, the program drops out of the switch statement and proceeds the the bottom of the while statement where "ordercom" tells the while statement to continue taking orders. This is obviously an un-ending loop but is coded this way to trouble shoot the issue at hand. I am testing the code with code breaks. What is happening is that after I select two menu selections or two loops of the while statement the program skips over the scanf statement. Can someone help me with this issue?
#include <stdio.h>
#define Artichoke 1.25
#define Beets .65
#define Carrots .89
#define Discount .05
#define Shipping1 10.00
#define Shipping2 8.00int main(void)
{
char ordercom;
char type;
float pounds;
float gross;
float poundTot = 0;
ordercom = 'Y';
while(ordercom == 'Y')
{
printf("*******************************************************************************************************************\n");
printf("Determine What You Want To Buy\n");
printf("1) (a)\t Artichokes\t 3) (c)\t Carrots\n");
printf("2) (b)\t Beets\t 4) (q)\t Quit\n");
printf("*******************************************************************************************************************\n");
printf("\n");
printf("Select The Type Of Vegetable You Want And The Pounds Of The Vegetable You Want Or (q) To Quit :");
scanf("%c ""%f",&type,£s);switch(type) { case 'a' : { gross = pounds \* Artichoke; break; } case 'b' : { gross = pounds \* Beets; break; } case 'c' : { gross = pounds \* Carrots; break; } case 'q' : { break; } default : { break; } } ordercom = 'Y'; } }
Before you start guessing, why not collect some information? How about printing the values of type and pounds right after your scanf got them? add a simple
printf
line, and you will probably know what is going on. :)Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Mike Certini wrote:
What is happening is that after I select two menu selections or two loops of the while statement the program skips over the scanf statement. Can someone help me with this issue?
Likely because something is in the keyboard buffer. Try using
fflush()
afterscanf()
."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
This is correct. It will be the carriage return ('\n' = 0x0A) character that is left in the input buffer. And as you suggested,
fflush(stdin);
should fix the problem -
Before you start guessing, why not collect some information? How about printing the values of type and pounds right after your scanf got them? add a simple
printf
line, and you will probably know what is going on. :)Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Luc, I will do this on my next post. Thank you for the recommendation.
-
This is correct. It will be the carriage return ('\n' = 0x0A) character that is left in the input buffer. And as you suggested,
fflush(stdin);
should fix the problemAndrew, Excellent! Thank you for your reply. Can you answer another question? Why is my "q" selection or default not working?
-
Andrew, Excellent! Thank you for your reply. Can you answer another question? Why is my "q" selection or default not working?
there are 2 reasons: 1. the scanf function wont return until it gets a char and a float 2. You are only breaking out of the switch statement, not the while loop
switch (type) {
//other cases
case 'q':
break;
default:
break;
}those are simply breaking out of the switch statement. you need to tell the while loop to exit
#include <conio.h> //for _getch()
//#defines
int main() {
//declare variables
while (ordercom == 'Y') {
//printf ...
type = _getch(); //get a single character
putc(type, stdout); //print that character to stdout
if (type == 'q') {
break;
}
scanf("%f",£s);
switch (type) {
//other cases
default:
ordercom = 'N'; //Is this really what you want? this will close the program if they make the wrong choice
break;
}
}
} -
there are 2 reasons: 1. the scanf function wont return until it gets a char and a float 2. You are only breaking out of the switch statement, not the while loop
switch (type) {
//other cases
case 'q':
break;
default:
break;
}those are simply breaking out of the switch statement. you need to tell the while loop to exit
#include <conio.h> //for _getch()
//#defines
int main() {
//declare variables
while (ordercom == 'Y') {
//printf ...
type = _getch(); //get a single character
putc(type, stdout); //print that character to stdout
if (type == 'q') {
break;
}
scanf("%f",£s);
switch (type) {
//other cases
default:
ordercom = 'N'; //Is this really what you want? this will close the program if they make the wrong choice
break;
}
}
}Andrew, Thanks. I am not familiar with putc() and _getch(). I will take a look into this. Mike
-
Andrew, Excellent! Thank you for your reply. Can you answer another question? Why is my "q" selection or default not working?
-
How did u confirm they are not working ? Try to put printf() in those breaks and check/debug.
[Delegates] [Virtual Desktop] [Tray Me !]
-Malli...! :rose:****Malli, Thank you for your input. I found a number of corrections on other boards. As a result here is the corrected version:
#include <stdio.h>
#include <conio.h>#define Artichoke 1.25
#define Beets .65
#define Carrots .89
#define Discount .05
#define Shipping1 10.00
#define Shipping2 8.00int main(void)
{
char ordercom;
char type;
float pounds;
float gross;
float poundTot = 0;
ordercom = 'Y';
while(ordercom == 'Y')
{
printf("*******************************************************************************************************************\n");
printf("Determine What You Want To Buy\n");
printf("1) (a)\t Artichokes\t 3) (c)\t Carrots\n");
printf("2) (b)\t Beets\t 4) (q)\t Quit\n");
printf("*******************************************************************************************************************\n");
printf("\n");
printf("Select The Type Of Vegetable You Want:\n");
scanf("%c",&type);
fflush(stdin);
printf("Select The Amount Of Pounds You Want To Purchase:\n");
scanf("%f",£s);
fflush(stdin);
switch(type)
{
case 'a' :
{
gross = pounds * Artichoke;
ordercom = 'Y';
break;
}
case 'b' :
{
gross = pounds * Beets;
ordercom = 'Y';
break;
}
case 'c' :
{
gross = pounds * Carrots;
ordercom = 'Y';
break;
}
case 'q' :
{
ordercom = 'N';
break;
}
default :
{
ordercom = 'N';
break;
}
}
}}
-
How did u confirm they are not working ? Try to put printf() in those breaks and check/debug.
[Delegates] [Virtual Desktop] [Tray Me !]
-Malli...! :rose:****for the breaks in the switch statement, I could just see what was hapening for the rest I placed a breakpoint on the line
switch(type)
so when it hit that I could see what the value of the variables were. for the 2nd time in the looptype = '\n' (0x0A)
when you hover over the variable name. For the 'q' case for quitting I just know how scanf works, that it wont return until it gets a new line character, so another solution was needed -
Malli, Thank you for your input. I found a number of corrections on other boards. As a result here is the corrected version:
#include <stdio.h>
#include <conio.h>#define Artichoke 1.25
#define Beets .65
#define Carrots .89
#define Discount .05
#define Shipping1 10.00
#define Shipping2 8.00int main(void)
{
char ordercom;
char type;
float pounds;
float gross;
float poundTot = 0;
ordercom = 'Y';
while(ordercom == 'Y')
{
printf("*******************************************************************************************************************\n");
printf("Determine What You Want To Buy\n");
printf("1) (a)\t Artichokes\t 3) (c)\t Carrots\n");
printf("2) (b)\t Beets\t 4) (q)\t Quit\n");
printf("*******************************************************************************************************************\n");
printf("\n");
printf("Select The Type Of Vegetable You Want:\n");
scanf("%c",&type);
fflush(stdin);
printf("Select The Amount Of Pounds You Want To Purchase:\n");
scanf("%f",£s);
fflush(stdin);
switch(type)
{
case 'a' :
{
gross = pounds * Artichoke;
ordercom = 'Y';
break;
}
case 'b' :
{
gross = pounds * Beets;
ordercom = 'Y';
break;
}
case 'c' :
{
gross = pounds * Carrots;
ordercom = 'Y';
break;
}
case 'q' :
{
ordercom = 'N';
break;
}
default :
{
ordercom = 'N';
break;
}
}
}}
There is no harm in it, but in the cases for 'a', 'b' and 'c' you dont need to set
ordercom = 'Y';
because it already has that value. You should no longer need#include <conio.h>
now that you got rid of_getch()
-
Malli, Thank you for your input. I found a number of corrections on other boards. As a result here is the corrected version:
#include <stdio.h>
#include <conio.h>#define Artichoke 1.25
#define Beets .65
#define Carrots .89
#define Discount .05
#define Shipping1 10.00
#define Shipping2 8.00int main(void)
{
char ordercom;
char type;
float pounds;
float gross;
float poundTot = 0;
ordercom = 'Y';
while(ordercom == 'Y')
{
printf("*******************************************************************************************************************\n");
printf("Determine What You Want To Buy\n");
printf("1) (a)\t Artichokes\t 3) (c)\t Carrots\n");
printf("2) (b)\t Beets\t 4) (q)\t Quit\n");
printf("*******************************************************************************************************************\n");
printf("\n");
printf("Select The Type Of Vegetable You Want:\n");
scanf("%c",&type);
fflush(stdin);
printf("Select The Amount Of Pounds You Want To Purchase:\n");
scanf("%f",£s);
fflush(stdin);
switch(type)
{
case 'a' :
{
gross = pounds * Artichoke;
ordercom = 'Y';
break;
}
case 'b' :
{
gross = pounds * Beets;
ordercom = 'Y';
break;
}
case 'c' :
{
gross = pounds * Carrots;
ordercom = 'Y';
break;
}
case 'q' :
{
ordercom = 'N';
break;
}
default :
{
ordercom = 'N';
break;
}
}
}}
1 more thing I missed at a first glance. For the case when the user presses q to quit, they still need to enter a float for the weight. you should have the 2nd scanf inside an if to check for that