Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[C] Not sure what I am doing wrong...

c scanf functions

This topic has been archived. This means that you cannot reply to this topic.
21 replies to this topic

#13 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 08 March 2013 - 12:13 PM

The first thing I'm seeing is this:

 

 

 

default:
   printf("Invalid Option.\n");option = showMenu(custNo);
   option = showMenu(custNo);
}
option = showMenu(custNo);
 


None of your case statements have a break.  So, it's falling through the entire thing when you choose A.  The last statement should be a break in each case statement.  

 

Also, since the menu will show at the bottom of the while loop, there is no need for it to be called inside each case statement.

 

In the above example, it's actually being called 3 times.  twice inside and once outside the default case.

 

The Q option should only have a break statement.  A return will drop it out of that entire function.


My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#14 imustnotfear

imustnotfear

    CC Newcomer

  • Member
  • PipPip
  • 14 posts

Posted 08 March 2013 - 04:53 PM

My scanf()s seem to be intermittently returning a value that isn't what I've entered, regardless of my flushKeyboard function, so that some of the input is working and some is not.

 

Updated Code: 

#include <stdio.h>

int getCustomer();
void flushKeyboard(void);
float custOrder(int custNo, float ttlArt, float ttlBeet, float ttlCar, float ttlOrd);
float calcDiscount(float costO);
char showMenu(int custNo);
float getWeight();
float calcShipping(float shipWeight);

int main(void)
{
int custNo;
float ttlArt, ttlBeet, ttlCar, ttlOrd;

custNo = getCustomer();
printf("Customer number: %d\n", custNo);
while(custNo != 0)
{
custOrder(custNo, ttlArt, ttlBeet, ttlCar, ttlOrd);
}
custNo = getCustomer();
}


// This function is designed to prompt a user for their customer number.

int getCustomer()
{
int custNo; // Local Variable
printf("Enter the customer number (0 to quit):\n");
scanf("%d",&custNo);
flushKeyboard();
while(custNo<0)
{
printf("Invalid customer number, please re-enter.");
scanf("%d", &custNo);
flushKeyboard();
}
return custNo;
}

//This function is where the user enters the quantities and calculates the cost of their order.

float custOrder(int custNo, float ttlArt, float ttlBeet, float ttlCar, float ttlOrd)
{
float weightA, weightB, weightC, shipCost, shipWeight, discount, w, costA, costB, costC, costO;
char option;

option = showMenu(custNo);
printf("You chose: %c\n", option);
while(option!='Q')
{
switch(option)
{
case ('A'):
{
printf("Enter the weight of artichokes ordered:\n");
     w = getWeight();
     weightA = weightA + w;
     ttlArt = ttlArt + w;
     break;
}
case ('B'):
{
printf("Enter the weight of beets ordered:\n");
     w = getWeight();
     weightB = weightB + w;
     ttlBeet = ttlBeet + w;
     break;
}
case ('C'):
{
printf("Enter the weight of carrots ordered:\n");
     w = getWeight();
     weightC = weightC + w;
     ttlCar = ttlCar + w;
     break;
}
case ('Q'):
{
break;
}
default:
printf("Invalid Option.\n");option = showMenu(custNo);
option = showMenu(custNo);
}
option = showMenu(custNo);
printf("You chose: %c\n", option);
}
costA = 0;
costB = 0;
costC = 0;
weightA = 0;
weightB = 0;
weightC = 0;
cost0 = 0;
discount = 0;
costA = weightA * 1.25;
costB = weightB * 0.65;
costC = weightC * 0.89;
costO = costA + costB + costC;
discount = calcDiscount(costO);
costO = costO - discount;
shipWeight = weightA + weightB + weightC;
shipCost = calcShipping(shipWeight);
costO = costO + shipCost;
printf("===============================================\n");
printf("CUSTOMER NUMBER: %d\n", custNo);
printf("ITEM COST/KG ORDERED COST\n");
printf("===============================================\n");
if(costA>0)
{
printf("Artichokes 1.25/kg %.2f kg $ %.2f\n", weightA, costA);
}
if(costB > 0)
printf("Beets 0.65/kg %.2f kg $ %.2f\n", weightB, costB);
if(costC > 0)
printf("Carrots 0.89/kg %.2f kg $ %.2f\n", weightC, costC);
printf("-----------------------------------------------\n");
if(discount > 0)
printf("Discount : $ %.2f", discount);
printf("Shipping Weight : %.2f kg \nShipping Cost : $%.2f\nTotal Order : $%.2f\n\n\n", shipWeight, shipCost, costO);
ttlOrd = ttlOrd + costO;
}

// This function calculates the discount (if any).
float calcDiscount(float costO)
{
float discount;
discount = 0;
if(costO > 100.00)
discount = costO *0.05;
return discount;
}

// This function gets the weight of an order (in kg) from the user.
float getWeight()
{
float w; // local Variable
scanf("%f", &w);
while(w<0)
{
printf("Invalid Weight, please re-enter.\n");
scanf("%f", &w);
}
return w;
}

// This function displays a menu to the user asking them to select an item to order or exit the program.
char showMenu(int custNo)
{
char option;
printf("A - Enter weight of artichokes\n");
printf("B - Enter weight of beets\n");
printf("C - Enter weight of carrots\n");
printf("Q - Exit the ordering process for customer %d\n", custNo);
printf("Enter option:\n");
void flushKeyboard(void);
scanf("%c", &option);
void flushKeyboard(void);
option = toupper(option);
while(option!='A' && option!='B' && option!='C' && option!='Q')
{
printf("Invalid option, please re-enter.\n");
scanf("%c", &option);
option = toupper(option);
}
return option;
}

//This function calculates the shipping cost of the order.
float calcShipping(float shipWeight)
{

float shipCost;
shipCost = 0.00;
if(shipWeight>20.0)
shipCost = 8.00 + (0.1 * shipWeight);
if(shipWeight>5.0)
shipCost = 10.00;
if(shipWeight>0.0)
shipCost = 3.50;
return shipCost;
}

//This module flushes the keyboard buffer to allow the user to enter multiple consecutive values into a scanf statement.

void flushKeyboard(void)
{
int ch; // Dummy variable to read data into.

while((ch = getc(stdin)) != EOF && ch != '\n');
}

 

Sample Output:

21evjbd.jpg

 

I've fixed the issue with the values of weightX and costX, so all that I can't figure out is why it's acting as though an invalid value for option is being entered after the weight of the prior item.


Edited by imustnotfear, 08 March 2013 - 10:31 PM.


#15 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 09 March 2013 - 07:42 AM

You have to be careful about your zeros and O's.

costO <-- an "o" is listed as cost0 <-- a zero in several instances.
My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#16 Guest

Guest

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 914 posts

Posted 09 March 2013 - 08:10 AM

Hi, right off the bat, I noticed that this line is a problem:

float weightA, weightB, weightC, shipCost, shipWeight, discount, w, costA, costB, costC, costO;

In C, the value of these floats starts off undefined so you end up getting junk data when you use them. I assume you want them to start off as zero as they would in Java or Python so you need to do that manually.

float weightA = 0, weightB = 0, weightC = 0; /* etc. */

Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#17 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 09 March 2013 - 09:05 AM

He's initializing them after the declaration


for some reason the code tag isn't working.

I replaced your flushkeyboard with getchar



 

#include <stdio.h>


int getCustomer();
void custOrder(int custNo, float ttlArt, float ttlBeet, float ttlCar, float ttlOrd);
float calcDiscount(float costO);
char showMenu(int custNo);
float getWeight();
float calcShipping(float shipWeight);

int main(void)
{
int custNo;
float ttlArt, ttlBeet, ttlCar, ttlOrd;

custNo = getCustomer();
printf("Customer number: %d\n", custNo);
while(custNo <= 0)
{
custNo = getCustomer();
}

custOrder(custNo, ttlArt, ttlBeet, ttlCar, ttlOrd);

}


// This function is designed to prompt a user for their customer number.

int getCustomer()
{
int custNo; // Local Variable
printf("Enter the customer number (0 to quit):\n");
scanf("%d",&custNo);
getchar();
while(custNo <0 )
{
printf("Invalid customer number, please re-enter.");
scanf("%d", &custNo);
getchar();
}
return custNo;
}

//This function is where the user enters the quantities and calculates the cost of their order.

void custOrder(int custNo, float ttlArt, float ttlBeet, float ttlCar, float ttlOrd)
{
float weightA, weightB, weightC, shipCost, shipWeight, discount, w, costA, costB, costC, costO;
char option;

option = showMenu(custNo);
printf("You chose: %c\n", option);
do
{
switch(option)
{
case ('A'):
{
printf("Enter the weight of artichokes ordered:\n");
w = getWeight();
weightA = weightA + w;
ttlArt = ttlArt + w;
break;
}
case ('B'):
{
printf("Enter the weight of beets ordered:\n");
w = getWeight();
weightB = weightB + w;
ttlBeet = ttlBeet + w;
break;
}
case ('C'):
{
printf("Enter the weight of carrots ordered:\n");
w = getWeight();
weightC = weightC + w;
ttlCar = ttlCar + w;
break;
}
case ('Q'):
{
break;
}
default:
printf("Invalid Option.\n");
}

//option = showMenu(custNo);
printf("You chose: %c\n", option);

} while ((option = showMenu(custNo))!='Q');

costA = 0;
costB = 0;
costC = 0;

weightA = 0;
weightB = 0;
weightC = 0;

costO = 0;

discount = 0;
costA = weightA * 1.25;
costB = weightB * 0.65;
costC = weightC * 0.89;
costO = costA + costB + costC;
discount = calcDiscount(costO);
costO = costO - discount;
shipWeight = weightA + weightB + weightC;
shipCost = calcShipping(shipWeight);
costO = costO + shipCost;

printf("===============================================\n");
printf("CUSTOMER NUMBER: %d\n", custNo);
printf("ITEM COST/KG ORDERED COST\n");
printf("===============================================\n");

if(costA>0)
{
printf("Artichokes 1.25/kg %.2f kg $ %.2f\n", weightA, costA);
}
if(costB > 0)
printf("Beets 0.65/kg %.2f kg $ %.2f\n", weightB, costB);
if(costC > 0)
printf("Carrots 0.89/kg %.2f kg $ %.2f\n", weightC, costC);
printf("-----------------------------------------------\n");
if(discount > 0)
printf("Discount : $ %.2f", discount);
printf("Shipping Weight : %.2f kg \nShipping Cost : $%.2f\nTotal Order : $%.2f\n\n\n", shipWeight, shipCost, costO);
ttlOrd = ttlOrd + costO;
}

// This function calculates the discount (if any).
float calcDiscount(float costO)
{
float discount;
discount = 0;
if(costO > 100.00)
discount = costO *0.05;
return discount;
}

// This function gets the weight of an order (in kg) from the user.
float getWeight()
{
float w; // local Variable
scanf("%f", &w);
while(w<0)
{
printf("Invalid Weight, please re-enter.\n");
scanf("%f", &w);
}
return w;
}

// This function displays a menu to the user asking them to select an item to order or exit the program.
char showMenu(int custNo)
{
char option;
printf("A - Enter weight of artichokes\n");
printf("B - Enter weight of beets\n");
printf("C - Enter weight of carrots\n");
printf("Q - Exit the ordering process for customer %d\n", custNo);
printf("Enter option:\n");
getchar();
scanf("%c", &option);
getchar();
option = toupper(option);

while(option!='A' && option!='B' && option!='C' && option!='Q')
{
printf("Invalid option, please re-enter.\n");
scanf("%c", &option);
option = toupper(option);
}
return toupper(option);
}

//This function calculates the shipping cost of the order.
float calcShipping(float shipWeight)
{

float shipCost;
shipCost = 0.00;
if(shipWeight>20.0)
shipCost = 8.00 + (0.1 * shipWeight);
if(shipWeight>5.0)
shipCost = 10.00;
if(shipWeight>0.0)
shipCost = 3.50;
return shipCost;
}
 

 

===== Edited my post to put code in code tags. ====


Edited by lespauled, 11 March 2013 - 05:12 AM.

My Blog: http://forum.codecal...699-blog-77241/
"Women and Music: I'm always amazed by other people's choices." - David Lee Roth

#18 Guest

Guest

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 914 posts

Posted 09 March 2013 - 03:30 PM

He's initializing them after the declaration

Oh yeah, I see that now. Problem is he's doing it in the wrong order. The weights are getting replaced with zeroes right before the costs are calculated and the receipt is printed on the screen. It should be done at the same time the variables are declared.

 

I replaced your flushkeyboard with getchar

Doesn't make a difference in the code for me. I actually like the original function a bit better because it should ignore any other junk data before reaching the newline. The actual culprit for his problem was the getWeight() function lacked flushKeyboard() after it's scanf() call. I corrected the problems and this code works for me:

#include <stdio.h>
int getCustomer();
void flushKeyboard(void);
float custOrder(int custNo, float ttlArt, float ttlBeet, float ttlCar, float ttlOrd);
float calcDiscount(float costO);
char showMenu(int custNo);
float getWeight();
float calcShipping(float shipWeight);
int main(void) {
	int custNo;
	float ttlArt, ttlBeet, ttlCar, ttlOrd;
	custNo = getCustomer();
	printf("Customer number: %d\n", custNo);
	while(custNo != 0) {
		custOrder(custNo, ttlArt, ttlBeet, ttlCar, ttlOrd);
	}
	custNo = getCustomer();
}
// This function is designed to prompt a user for their customer number.
int getCustomer() {
	int custNo;
	// Local Variable
	printf("Enter the customer number (0 to quit):\n");
	scanf("%d",&custNo);
	flushKeyboard();
	while(custNo<0) {
		printf("Invalid customer number, please re-enter.");
		scanf("%d", &custNo);
		flushKeyboard();
	}
	return custNo;
}
//This function is where the user enters the quantities and calculates the cost of their order.
float custOrder(int custNo, float ttlArt, float ttlBeet, float ttlCar, float ttlOrd) {
	float weightA = 0, weightB = 0, weightC = 0, shipCost = 0, shipWeight = 0, discount = 0, w = 0, costA = 0, costB = 0, costC = 0, costO = 0;
	char option;
	option = showMenu(custNo);
	printf("You chose: %c\n", option);
	while(option!='Q') {
		switch(option) {
			case ('A'): {
				printf("Enter the weight of artichokes ordered:\n");
				w = getWeight();
				weightA = weightA + w;
				ttlArt = ttlArt + w;
				break;
			}
			case ('B'): {
				printf("Enter the weight of beets ordered:\n");
				w = getWeight();
				weightB = weightB + w;
				ttlBeet = ttlBeet + w;
				break;
			}
			case ('C'): {
				printf("Enter the weight of carrots ordered:\n");
				w = getWeight();
				weightC = weightC + w;
				ttlCar = ttlCar + w;
				break;
			}
			case ('Q'): {
				break;
			}
			default:
			printf("Invalid Option.\n");
			option = showMenu(custNo);
			option = showMenu(custNo);
		}
		option = showMenu(custNo);
		printf("You chose: %c\n", option);
	}
	costA = weightA * 1.25;
	costB = weightB * 0.65;
	costC = weightC * 0.89;
	costO = costA + costB + costC;
	discount = calcDiscount(costO);
	costO = costO - discount;
	shipWeight = weightA + weightB + weightC;
	shipCost = calcShipping(shipWeight);
	costO = costO + shipCost;
	printf("===============================================\n");
	printf("CUSTOMER NUMBER: %d\n", custNo);
	printf("ITEM COST/KG ORDERED COST\n");
	printf("===============================================\n");
	if(costA>0) {
		printf("Artichokes 1.25/kg %.2f kg $ %.2f\n", weightA, costA);
	}
	if(costB > 0)
	printf("Beets 0.65/kg %.2f kg $ %.2f\n", weightB, costB);
	if(costC > 0)
	printf("Carrots 0.89/kg %.2f kg $ %.2f\n", weightC, costC);
	printf("-----------------------------------------------\n");
	if(discount > 0)
	printf("Discount : $ %.2f", discount);
	printf("Shipping Weight : %.2f kg \nShipping Cost : $%.2f\nTotal Order : $%.2f\n\n\n", shipWeight, shipCost, costO);
	ttlOrd = ttlOrd + costO;
}
// This function calculates the discount (if any).
float calcDiscount(float costO) {
	float discount;
	discount = 0;
	if(costO > 100.00)
	discount = costO *0.05;
	return discount;
}
// This function gets the weight of an order (in kg) from the user.
float getWeight() {
	float w;
	// local Variable
	scanf("%f", &w);
	flushKeyboard();
	while(w<0) {
		printf("Invalid Weight, please re-enter.\n");
		scanf("%f", &w);
	}
	return w;
}
// This function displays a menu to the user asking them to select an item to order or exit the program.
char showMenu(int custNo) {
	char option;
	printf("A - Enter weight of artichokes\n");
	printf("B - Enter weight of beets\n");
	printf("C - Enter weight of carrots\n");
	printf("Q - Exit the ordering process for customer %d\n", custNo);
	printf("Enter option:\n");
	scanf("%c", &option);
	flushKeyboard();
	option = toupper(option);
	while(option!='A' && option!='B' && option!='C' && option!='Q') {
		printf("Invalid option, please re-enter.\n");
		scanf("%c", &option);
		option = toupper(option);
	}
	return option;
}
//This function calculates the shipping cost of the order.
float calcShipping(float shipWeight) {
	float shipCost;
	shipCost = 0.00;
	if(shipWeight>20.0)
	shipCost = 8.00 + (0.1 * shipWeight);
	if(shipWeight>5.0)
	shipCost = 10.00;
	if(shipWeight>0.0)
	shipCost = 3.50;
	return shipCost;
}
//This module flushes the keyboard buffer to allow the user to enter multiple consecutive values into a scanf statement.
void flushKeyboard(void) {
	int ch; // Dummy variable to read data into.

	while((ch = getc(stdin)) != EOF && ch != '\n');
}

 

Edit: Also, just an exercise for imustnotfear, it would be a better program if you format the printed data nicely, by spacing out weights, costs, etc. evenly. There should also be a way to exit gracefully. I've been forcing it closed with ^C.


Edited by Guest, 09 March 2013 - 03:48 PM.

Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#19 imustnotfear

imustnotfear

    CC Newcomer

  • Member
  • PipPip
  • 14 posts

Posted 10 March 2013 - 07:50 PM

Yeah, I'm not completely finished with it yet, I still have to add a couple of things to it :P I'm kind of embarrassed I didn't think to add a flushKeyboard to my getWeight, in retrospect it seems kind of obvious that would be the problem haha. Thanks for the help guys!



#20 imustnotfear

imustnotfear

    CC Newcomer

  • Member
  • PipPip
  • 14 posts

Posted 10 March 2013 - 08:09 PM

One more question...

For the final statement of daily totals, in the custMenu function is there a way for me to alter the values of ttlArt, ttlBeet, ttlCar and ttlOrd outside of the function itself so that my displayDaily function will print the sum of all of the orders placed?

 

float custOrder(int custNo, float ttlArt, float ttlBeet, float ttlCar, float ttlOrd)
{
  float weightA, weightB, weightC, shipCost, shipWeight, discount, w, costA, costB, costC, costO;
  char option;
 
  costA = 0;
  costB = 0;
  costC = 0;
  costO = 0;
  weightA = 0;
  weightB = 0;
  weightC = 0;
  discount = 0;
  option = showMenu(custNo);
  printf("You chose: %c\n", option);
  while(option!='Q')
  {
    switch(option)
    {
      case ('A'):
        {
          printf("Enter the weight of artichokes ordered:\n");
     w = getWeight();
          weightA = weightA + w;
     ttlArt = ttlArt + w; //This line is what I'm talking about
     break;
        }
      case ('B'):
        {
          printf("Enter the weight of beets ordered:\n");
     w = getWeight();
     weightB = weightB + w;
     ttlBeet = ttlBeet + w; //This line is what I'm talking about
     break;
        }
      case ('C'):
        {
          printf("Enter the weight of carrots ordered:\n");
     w = getWeight();
     weightC = weightC + w;
     ttlCar = ttlCar + w; //This line is what I'm talking about
     break;
        }
      case ('Q'):
        {
          break;
        }
      default:
        {
          printf("Invalid Option.\n");
        }
    }
    option = showMenu(custNo);
    printf("You chose: %c\n", option);
  }
  costA = weightA * 1.25;
  costB = weightB * 0.65;
  costC = weightC * 0.89;
  costO = costA + costB + costC;
  discount = calcDiscount(costO);
  costO = costO - discount;
  shipWeight = weightA + weightB + weightC;
  shipCost = calcShipping(shipWeight);
  costO = costO + shipCost;
  printf("===============================================\n");
  printf("CUSTOMER NUMBER: %d\n", custNo);
  printf("ITEM         COST/KG       ORDERED         COST\n");
  printf("===============================================\n");
  if(costA>0)
  {
    printf("Artichokes  1.25/kg       %.2fkg        $ %.2f\n", weightA, costA);
  }
  if(costB > 0)
    printf("Beets       0.65/kg       %.2fkg        $ %.2f\n", weightB, costB);
  if(costC > 0)
    printf("Carrots     0.89/kg       %.2fkg        $ %.2f\n", weightC, costC);
  printf("-----------------------------------------------\n");
  if(discount > 0)
    printf("Discount        : $ %.2f\n", discount);
  printf("Shipping Weight :  %.2f kg\nShipping Cost   : $%.2f\nTotal Order     : $%.2f\n\n\n", shipWeight, shipCost, costO);
ttlOrd = ttlOrd + costO; //This line is what I'm talking about
}

Edited by imustnotfear, 10 March 2013 - 08:09 PM.


#21 Guest

Guest

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 914 posts

Posted 11 March 2013 - 08:20 AM

There are two ways you could go about doing that. You could take advantage of scope and declare those variables at the top of the file (outside any function). You could also use pass the variables as pointers to be able to modify them (see this pointer tutorial that I worked on).


Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#22 imustnotfear

imustnotfear

    CC Newcomer

  • Member
  • PipPip
  • 14 posts

Posted 11 March 2013 - 10:36 AM

Ended up using pointers, got it all working perfectly :D

Thanks a ton for all the help, I really appreciate it!






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download