Jump to content
  • Advertisement
Sign in to follow this  

This topic is 2551 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am working on a project for my C programming class and I have the whole program done. Now we have just started the class so we dont know much but I do have some other programming experience. I get the program to run through the first time with a selection but the it will display the info once more but wont let the user selection another option after the first time it jumps right to another function and I am not sure why. I have changed stuff around and tried to figure it out but I can so if someone could point me in the right direction or give me a hint anything would be helpful thanks.



#include<stdlib.h>
#include<stdio.h>

#define SALES_TAX .07
#define INCHES_IN_FEET 12
#define COVERAGE_BAG 2

void display();
char entry();
float mulchBed();
float numberOfBags(float area);

int main()
{
printf("welcome to Andrew's Home Improvement Store!\n\n");
// variables for different types of mulch using parallel arrays
float mulchPriceOne = 3.97;
float mulchPriceTwo = 4.97;
float mulchPriceThree = 2.25;

float area; // the area of the mulch bed
float bags; // number of bags of mulch
float mulchPrice;
float price;
float tax;
float totalPrice;

char mulchType;
mulchType = entry();
while( mulchType != 'Q')
{

if(mulchType == 'E')
{
printf("You choose the Earth Gro mulch\n.");
mulchPrice = mulchPriceOne;
}
else if(mulchType == 'N')
{
printf("You choose the Nature Scapes mulch\n.");
mulchPrice = mulchPriceTwo;
}
else if(mulchType == 'G')
{
printf("You choose the Gardens Pro mulch\n.");
mulchPrice = mulchPriceThree;
}

area = mulchBed();
bags = numberOfBags(area);
price = mulchPrice * bags;
tax = price * SALES_TAX;
totalPrice = price + tax;


printf("The area of your mulch bed is %.0f square feet\n", area);
printf("The number of bags you will need %.2f bags\n", bags);
printf("The price of your mulch will be $%.2f\n", price);
printf("The tax on your bags will be $%.2f\n", tax);
printf("The total price including sales tax will be $%.2f\n\n\n", totalPrice);

mulchType = entry();
}


printf("Results were provided by Andrew Blalock")
return 0;

}
void display()
{
printf("Mulch\t\t\tSq Footage\t\t\tPrice Sq Ft\n\n");
printf("Earth Gro\t\t\t2 cu ft\t\t\t$3.97\n");
printf("Nature Scapes\t\t\t2 cu ft\t\t\t$4.97\n");
printf("Gardens Pro\t\t\t2 cu ft\t\t\t$2.25\n\n\n\n");
}
char entry()
{
char selection;
display();
printf("Choose what type of mulch you wish to purchase or exit the program.\n");
printf("(E)Earth Gro\n(N) Nature Scapes\n(G)Garden Pro\n(Q)Quit\n");
scanf("%c", &selection);
return selection;

}

float mulchBed()
{
int length;
int width;
int depthInches;
float depthFeet;

printf("What the is length of you mulch bed going to be(in feet):");
scanf("%i", &length);
printf("What the is width of you mulch bed going to be(in feet):");
scanf("%i", &width);
printf("What the is depth of you mulch bed going to be(in inches):");
scanf("%i", &depthInches);

depthFeet = depthInches / INCHES_IN_FEET;

float area;
area = length * width * depthFeet;
return area;

}

float numberOfBags(float area)
{
float bags;
bags = area / COVERAGE_BAG;
return bags;

}

Share this post


Link to post
Share on other sites
Advertisement
When a scanf conversion string starts with "%c", it doesn't skip over white space. Upon pressing the Enter key in a Windows console, 2 characters are in fact generated -- a '\r' and a '\n'. I suspect the '\n' is converted by the scanf() call inside entry() at the end of your primary while-loop.

The easiest fix is to change 'scanf("%c", &selection);' to 'scanf(" %c", &selection);' (note the extra space).

Or you can explicitly skip over white space inside entry() by use fgetc() and isspace() in a loop.

Share this post


Link to post
Share on other sites
`scanf ("%c",...)' will read any characters, including spaces and end-of-line characters. The first time you run your program, there is nothing to be read, and it works as you expect. But after you have called `scanf("%i",...)', the end-of-line character is left in the buffer, and the next call to `scanf("%c",...)' reads it. You can patch your program with something that will consume spaces and end-of-line characters before reading the character you want, but it's sort of a kludge.

Console input is notoriously hard to get right in C. My preferred solution is to always read a complete line of input (using something like fgets), and then parsing what you just read (using something like sscanf). It's a bit of work and it might be too complicated for you at this stage, if you still don't know how to use strings. But it's about the only solution that doesn't suffer from the type of problem you are experiencing.

Share this post


Link to post
Share on other sites

Console input is notoriously hard to get right in C.


I'd have to agree, given that my answer was incorrect -- if I could down-vote myself, I would! Just wanted to mention explicitly that the confusing half-truth I posted should be ignored.

Share this post


Link to post
Share on other sites
getch();

EDIT: The above was my post, and it solves all the issues the OP had. I didn't feel there needed to be more to add, but for for clarity. Just call:

selection = getch();

in entry()

Share this post


Link to post
Share on other sites

[quote name='alvaro' timestamp='1306346333' post='4815689']
Console input is notoriously hard to get right in C.


I'd have to agree, given that my answer was incorrect -- if I could down-vote myself, I would! Just wanted to mention explicitly that the confusing half-truth I posted should be ignored.
[/quote]
Just wondering why this particular post was downvoted?!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!