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

#1 imustnotfear

imustnotfear

    CC Newcomer

  • Member
  • PipPip
  • 14 posts

Posted 08 March 2013 - 07:58 AM

Hi there,

 

I'm working on a program for class (the first C program I've ever actually coded...) and I am having a problem getting it figured out.

The specs for the program are listed below:

 

 

Write a program that prompts for customer number, for example: 

Enter the customer number (0 to quit):

For each customer number entered displays the following menu to the user:

A - Enter weight of artichokes
B - Enter the weight of beets
C - Enter the weight of carrots
Q - Exit the ordering process for customer customerNumber
Enter option:


(customerNumber in the menu is the customer number entered by the user prior to this menu being displayed)

When the order for the customer has been entered, the program should then compute the total charges, the discount, if any, the shipping charges, and the grand total. The program should then display all the purchase information: the cost per kg, the kilograms ordered, and the cost for that order for each vegetable (if the vegetable was not ordered, then do not include it in the display), the total cost of the order, the discount (only if there is one), shipping weight, the shipping charge, and the grand total of all the charges.

The program will then prompt the user for the next customer number, until a customer number of 0 (zero) is entered. The program will then display the total weight of each vegetable ordered for all customers and the total sales of all shipments.

 

So far, what I've come up with is:

 

#include <stdio.h>

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

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

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


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

int getCustomer(int custNo)
{
  printf("Enter the customer number (0 to quit):\n");
  scanf("%d",&custNo);
  while(custNo<0)
  {
    printf("Invalid customer number, please re-enter.");
    scanf("%d", &custNo);
  }
  printf("Customer number accepted. Please proceed to order menu.\n");
  return custNo;
}

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

float custOrder(float 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);
  while(option!='Q')
  {
    switch(option)
    {
      case ('A'):
        printf("Enter the weight of artichokes ordered:\n");
	    w = getWeight(w);
   	    weightA = weightA + w;
	    ttlArt = ttlArt + w;
      case ('B'):
        printf("Enter the weight of beets ordered:\n");
	    w = getWeight(w);
	    weightB = weightB + w;
	    ttlBeet = ttlBeet + w;
      case ('C'):
        printf("Enter the weight of carrots ordered:\n");
	    w = getWeight(w);
	    weightC = weightC + w;
	    ttlCar = ttlCar + w;
      default:
        printf("Invalid Option\n");option = showMenu(custNo);
    }
    option = showMenu(custNo);
  }
  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);
  printf("===============================================\n");
  printf("CUSTOMER NUMBER: %d\n");
  printf("ITEM    COST/KG    ORDERED    COST\n");
  printf("===============================================\n");
  if(costA>0)
    printf("Artichokes  1.25/kg - %f kg  $ %f", weightA, costA);
  if(costB > 0)
    printf("Beets  0.65/kg - %f kg  $ %f", weightB, costB);
  if(costC > 0)
    printf("Carrots  0.89/kg - %f kg  $ %f", weightC, costC);
  printf("----------------------------------");
  if(discount > 0)
    printf("Discount    : $ %f", discount);
  printf("Shipping Weight : %f kg \n Shipping Cost   : $%f\n Total Order     : $%f\n", shipWeight, shipCost, costO);
ttlOrd = ttlOrd + costO;
}

//This function will calculate the discount(if any) on the order.

float calcDiscount(float costO)
{
  float discount;
  discount = 0;
  if(costO > 100.00)
    discount = costO *0.05;
  return discount;
}

//This function will get the weight of the order from the user.

float getWeight(float w)
{
  scanf("%f", &w);
  while(w<0)
  {
    printf("Invalid Weight, please re-enter.\n");
    scanf("%f", &w);
  }
  return w;
}

//This function will display an order menu to the user.

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("D - Exit the ordering process for customer %d\n", custNo);
    printf("Enter option:\n");
    scanf("%c", &option);
    while(option!='A', option!='B', option!='C', option!='Q')
        printf("Invalid option, please re-enter.");
        scanf("%c", &option);
    return option;
}

//This function will calculate the cost of shipping for 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;
}

Somehow I feel like I am not getting the concept of passing and defining variables between functions... any input would be much appreciated!

 



#2 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 08 March 2013 - 08:16 AM

The passed variable is not the value from the original.  It's passed by value, not reference.  If you want the original value, set the param to the address of the variable. &w.

 

Since you are returning a value from your functions, you don't need the original variable to be passed.

 

Try not requiring any parameters for your getXXXX functions.  There is no need.

 

instead of

 

 

 

w = getWeight(w);
 

 

 

use

 

 

 

w = getWeight();
 

 

 

remove the parameter in the function

 

from:

 

 

float getWeight(float w)
{
scanf("%f", &w);
while(w<0)
{
printf("Invalid Weight, please re-enter.\n");
scanf("%f", &w);
}
return w;
}
 

 

To:

 

float getWeight()
{
float w; // <<=== NOTICE THAT A LOCAL VARIABLE DECLARATION WAS ADDED HERE
scanf("%f", &w);
while(w<0)
{
printf("Invalid Weight, please re-enter.\n");
scanf("%f", &w);
}
return w;
}
 

Notice the local variable declaration.  Since it is local to the function, you can name it anything. As a habit (NOT A RULE), I usually call my return value variable: retVal

 

 

 

float getWeight()
{
float retVal;

do
{
printf("\nEnter a valid Weight: ");
scanf("%f", &retVal);
} while(retVal<0)

return retVal;
}
 

The main reason I do that is that I know it's the return value anywhere in the program.  Also, it eliminates any confusion of it being the same name as any variable outside the function.



What problems are you having with the program?  Sorry, I'm not able to check it myself at the moment.


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

#3 imustnotfear

imustnotfear

    CC Newcomer

  • Member
  • PipPip
  • 14 posts

Posted 08 March 2013 - 09:17 AM

Well the main problem I'm having is that when I enter a number higher than 0, I get an endless loop of "Invalid option, please re-enter." from the showMenu function, leading me to believe that the next layer of the problem (hopefully the last :P) is in there.

 

char showMenu(int custNo) 
/* I have to pass the value of custNo into the function for it to print in the menu, right?*/
{
    char option;
    printf("A - Enter weight of artichokes\n");
    printf("B - Enter weight of beets\n");
    printf("C - Enter weight of carrots\n");
    printf("D - Exit the ordering process for customer %d\n", custNo);
    printf("Enter option:\n");
    scanf("%c", &option);
    while(option!='A', option!='B', option!='C', option!='Q')
        printf("Invalid option, please re-enter.");
        scanf("%c", &option);
    return option;
}


#4 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 08 March 2013 - 09:19 AM

instead of commas in the while clause, use && and it will work


Edited by lespauled, 08 March 2013 - 09:19 AM.

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

#5 imustnotfear

imustnotfear

    CC Newcomer

  • Member
  • PipPip
  • 14 posts

Posted 08 March 2013 - 09:28 AM

It's still looping the error statement, is there anything else wrong with it? :S

 

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("D - Exit the ordering process for customer %d\n", custNo);
    printf("Enter option:\n");
    scanf("%c", &option);
    while(option!='A' && option!='B' && option!='C' && option!='Q')
        printf("Invalid option, please re-enter.");
        scanf("%c", &option);
    return option;
}


#6 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 08 March 2013 - 09:36 AM

try printing option to see what is actually being stored


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

#7 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 08 March 2013 - 09:53 AM

you're probably having a problem with capital letters.

 

"A" does not equal "a"
 

try adding:

 

 

 

option = toupper(option);
 

 

 

after  scanf


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

#8 imustnotfear

imustnotfear

    CC Newcomer

  • Member
  • PipPip
  • 14 posts

Posted 08 March 2013 - 10:19 AM

#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);
  }
}


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

// 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);
    option = toupper(option);
    flushKeyboard();
    printf("%c <- still in showMenu\n", option);
    while(option!='A' && option!='B' && option!='C' && option!='Q')
        printf("Invalid option, please re-enter.\n");
        scanf("%c", &option);
        option = toupper(option);
        flushKeyboard();
    return option;
}

//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');
}

 

I put in the flushKeyboard function that was shown to us in class hoping that it would help, but I'm still ending up with some wonky results.

 

Sample output of the program:

o5tffq.jpg

 

I'm not sure about a few things.

a ) the showMenu() function doesn't seem to be returning the option variable correctly...

b ) the switch(option) line isn't working, hopefully only because of the option variable being incorrectly returned.


Edited by imustnotfear, 08 March 2013 - 10:20 AM.


#9 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 08 March 2013 - 10:49 AM

In showMenu() you are missing the brackets in the while, so it's only looping the first line after it.  Put the brackets around that block.



Add an option for Q in your case statement.  just break out of the loop at that point.


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

#10 imustnotfear

imustnotfear

    CC Newcomer

  • Member
  • PipPip
  • 14 posts

Posted 08 March 2013 - 11:04 AM

28a1086.jpg

 

Progress is definitely being made, I really appreciate the help :D

Any idea why the showMenu function wouldn't work the second time? 

It seems to be storing the last input as opposed to the current one, I think...

 

Continuing to fool around with it I did make it to the end of the custOrder function

2sbtwra.jpg

 

The switch doesn't seem to function either, as no matter what option is defined as it just proceeds from A to B to C without actually getting a new value for option... and Q doesn't work either. D:


Edited by imustnotfear, 08 March 2013 - 11:18 AM.


#11 lespauled

lespauled

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1360 posts

Posted 08 March 2013 - 11:22 AM

I'm not seeing anything obvious.  Post your updated code.  I wish I was able to run this and see what is going on.  I can do it when I get home from work, if nobody answers it first.

 

Try putting the printf's back in to tell you what the chosen values are.


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

#12 imustnotfear

imustnotfear

    CC Newcomer

  • Member
  • PipPip
  • 14 posts

Posted 08 March 2013 - 12:03 PM

somehow ended up editing this post instead of making a reply :P


Edited by imustnotfear, 08 March 2013 - 04:53 PM.





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