Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


C Program


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 blalaw   Members   -  Reputation: 100

Like
0Likes
Like

Posted 25 May 2011 - 11:31 AM

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;

}


Sponsor:

#2 GregMichael   Members   -  Reputation: 143

Like
0Likes
Like

Posted 25 May 2011 - 11:42 AM

What IDE are you using ?

If it's Visual Studio 2010 etc....try setting some break points and step through the code to see what it is doing.

#3 e‍dd   Members   -  Reputation: 2105

Like
-1Likes
Like

Posted 25 May 2011 - 11:52 AM

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.

#4 Álvaro   Crossbones+   -  Reputation: 13880

Like
2Likes
Like

Posted 25 May 2011 - 11:58 AM

`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.

#5 e‍dd   Members   -  Reputation: 2105

Like
0Likes
Like

Posted 25 May 2011 - 03:25 PM

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.

#6 BeerNutts   Crossbones+   -  Reputation: 2998

Like
-2Likes
Like

Posted 26 May 2011 - 01:50 PM

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()
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#7 e‍dd   Members   -  Reputation: 2105

Like
1Likes
Like

Posted 27 May 2011 - 12:10 PM


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.

Just wondering why this particular post was downvoted?!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS