Can not get this program to work?

This is a discussion on Can not get this program to work? within the C Programming forums, part of the General Programming Boards category; When I run this program, I am not getting this answer: "You will eat 76 meals at Subway and 74 ...

  1. #1
    BLG
    BLG is offline
    Registered User
    Join Date
    Sep 2009
    Posts
    14

    Can not get this program to work?

    When I run this program, I am not getting this answer:
    "You will eat 76 meals at Subway and 74 meals at Lazy Moon."
    "You will have $1 left on your card."

    Heres the Question:
    You will prompt the user for the following information:
    1. The cost of a meal at Subway (in dollars must be an integer).
    2. The cost of a meal at Lazy Moon (in dollars must be an integer and greater than #1).
    3. The number of meals you will need to eat for the semester.
    4. The amount of money (in dollars must be an integer) your parents have put on your meal card.

    Input Specification
    1. The cost of both meals will be positive integers less than 20 with the cost of a meal at Lazy Moon being more expensive than the cost of a meal at Subway.
    2. The number of meals will be a positive integer less than 300.
    3. The amount of money your parents place on your meal card will be a positive integer that is somewhere in between the cost of eating every meal at Subway and the cost of eating every meal at Lazy Moon, inclusive.

    Sample Run
    How much do you spend on a meal at Subway?
    4
    How much do you spend on a meal at Lazy Moon?
    6
    How many meals will you eat this semester?
    150
    How much money did your parents put on your meal card?
    749
    You will eat 76 meals at Subway and 74 meals at Lazy Moon.
    You will have $1 left on your card.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int number_of_meals_eaten_at_subway, number_of_meals_eaten_at_Lazy_Moon, spent_at_subway, spent_at_Lazy_Moon, 
            eat_this_semester, limit_on_meal_card; 
        char x, y, a, b, c, d;  
        
        x = number_of_meals_eaten_at_subway;
        y = number_of_meals_eaten_at_Lazy_Moon;
        a = spent_at_subway;
        b = spent_at_Lazy_Moon;
        c = eat_this_semester;
        d = limit_on_meal_card; 
        
        printf ("How much do you spend on a meal at subway? \n");
        scanf ("%d", &spent_at_subway, a);
        
        printf ("How much do you spend on a meal at Lazy Moon? \n");
        scanf ("%d", &spent_at_Lazy_Moon, b);
        
        printf ("How many meals will you eat this semester? \n");
        scanf ("%d", &eat_this_semester, c);
        
        printf ("How much money did your parents put on your meal card? \n");
        scanf ("%d", &limit_on_meal_card, d);    
        
        y = (c - a*d)/(-a + b);
        x = (b*d + c - 2*a*d)/(-a + b);
        
        printf ("You will eat %d meals at subway" "and %d meals at Lazy Moon\n", number_of_meals_eaten_at_subway, 
                 number_of_meals_eaten_at_Lazy_Moon);
        printf ("You will have %d left on your card", limit_on_meal_card);
        
        getch ();
        
        return 0;
    }
    I did my math like this:
    Ax + By = C
    X + Y = D
    (I put what a, b, c, and d are in the program).

    When I solve for X and Y, I get the equation that I placed in the program.

    X + Y = D
    X = D - Y
    X = D - (C - AD) / (-A + B) (tried leaving like this, but did not come out to right answer)
    X = (BD + C - 2AD) / (-A + B) (Did it like I was adding fraction. Still not right answer)

    AX + BY = C
    A(D - Y) + BY = C
    AD - AY + BY = C
    -AY + BY = C - AD
    Y = (C - AD) / (-A + B)

    Can anyone tell me what I missing or doing wrong?

    Thanks,
    BLG

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    x = number_of_meals_eaten_at_subway;
    That doesn't do what you think it does. A variable assignment like that copies the value of the right-hand side and assigns it to the variable on the left-hand side at the time the line is executed. Future changes to number_of_meals_eaten_at_subway will not affect x in any way. You're not somehow tying these variables together so that changes to one will be reflected in the other (although there are ways to do this); you're just copying the value of one into the other. The end.

    Code:
    scanf ("%d", &spent_at_subway, a);
    Not sure what you're trying to do here. I think you're trying to perhaps read the value into both variables at once. Unfortunately, it won't work. The "%d" tells scanf to expect one pointer to an int; it won't expect or even notice extra parameters that you pass it. If you enable compiler warnings it will probably catch these sorts of mistakes.

    So, a suggestion: use one variable for one purpose. Don't try to represent exactly the same thing with two separate variables. If you must, you could read into spent_at_subway etc. and then later execute lines like
    Code:
    a = spent_at_subway;
    once, of course, you've actually gotten a meaningful value into spent_at_subway with scanf().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    BLG
    BLG is offline
    Registered User
    Join Date
    Sep 2009
    Posts
    14
    Quote Originally Posted by dwks View Post
    Code:
    x = number_of_meals_eaten_at_subway;
    That doesn't do what you think it does. A variable assignment like that copies the value of the right-hand side and assigns it to the variable on the left-hand side at the time the line is executed. Future changes to number_of_meals_eaten_at_subway will not affect x in any way. You're not somehow tying these variables together so that changes to one will be reflected in the other (although there are ways to do this); you're just copying the value of one into the other. The end.

    I was assuming that since I had the formula x and y equals, that I had to specify which was which with the variable and when you run the program and you put in a number for what it is asking, it would put that number into that variable. I see what you are saying though.

    Code:
    scanf ("%d", &spent_at_subway, a);
    Not sure what you're trying to do here. I think you're trying to perhaps read the value into both variables at once. Unfortunately, it won't work. The "%d" tells scanf to expect one pointer to an int; it won't expect or even notice extra parameters that you pass it. If you enable compiler warnings it will probably catch these sorts of mistakes.

    I was just specifying what "a" was and that scanf just allows you to put in value for how much you spent a subway. When I compiled the whole program, I had no error, but just kept giving me the wrong value. It was not giving me 76 meals at subway and 74 meals at Lazy Moon and that was the issue I am having.

    So, a suggestion: use one variable for one purpose. Don't try to represent exactly the same thing with two separate variables. If you must, you could read into spent_at_subway etc. and then later execute lines like
    Code:
    a = spent_at_subway;
    once, of course, you've actually gotten a meaningful value into spent_at_subway with scanf().

    I'll try and see if I can do that. Thanks for your help
    Thanks for your help. I'll try to do that.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Also, I'm not sure why you're using char variables for x, y, etc. Your requirements specifically state
    Code:
    1. The cost of a meal at Subway (in dollars – must be an integer).
    ...
    So why not just use ints?

    [And just so you know: you can have multiple [quote][/quote] sections, and put your own responses outside the quoted text. For example:

    [quote]Thanks for your help. I'll try to do that. [/quote]
    Have fun. ]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    BLG
    BLG is offline
    Registered User
    Join Date
    Sep 2009
    Posts
    14
    Quote Originally Posted by dwks View Post
    Also, I'm not sure why you're using char variables for x, y, etc. Your requirements specifically state
    Code:
    1. The cost of a meal at Subway (in dollars – must be an integer).
    ...
    So why not just use ints?

    [And just so you know: you can have multiple sections, and put your own responses outside the quoted text. For example:


    Have fun. ]
    ,lol...well reason why I was using char is so I can specify the variable. When I gave the equation: Ax + By = C, etc. That is why I was using "Char." I was going to do the quotes after I get this program to run. When you talking about using "cost of meal at subway" if you look in the code that I put here, I already had it there.

    Edit:
    SAS = Spent At Subway
    SALM = spent at Lazy Moon
    I am trying all sorts of ways, trying to get the math part, but for some reason it is not working for me. I took out the char and now I have this:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int number_of_meals_eaten_at_subway, number_of_meals_eaten_at_Lazy_Moon, sas, salm, eat_this_semester, limit_on_meal_card; 
    
        
        printf ("How much do you spend on a meal at subway? \n");
        scanf ("%d", &sas);
        
        printf ("How much do you spend on a meal at Lazy Moon? \n");
        scanf ("%d", &salm);
        
        printf ("How many meals will you eat this semester? \n");
        scanf ("%d", &eat_this_semester);
        
        printf ("How much money did your parents put on your meal card? \n");
        scanf ("%d", &limit_on_meal_card);    
        
        number_of_meals_eaten_at_subway = (eat_this_semester - salm*limit_on_meal_card)/(sas - salm);
        number_of_meals_eaten_at_Lazy_Moon = limit_on_meal_card - number_of_meals_eaten_at_subway;
        
        printf ("You will eat %d meals at subway and %d meals at Lazy Moon\n", number_of_meals_eaten_at_subway, 
                 number_of_meals_eaten_at_Lazy_Moon);
        printf ("You will have %d left on your card", limit_on_meal_card);
        
        getch ();
        
        return 0;
    }
    program runs w/o any errors. It is just it is not calculating the total meals at subway and Lazy Moon and what I have left over.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    ,lol...well reason why I was using char is so I can specify the variable. When I gave the equation: Ax + By = C, etc. That is why I was using "Char." I was going to do the quotes after I get this program to run. When you talking about using "cost of meal at subway" if you look in the code that I put here, I already had it there.
    I'm not sure what you're saying here. You can do anything with an int that you can with a char, except that an int has even more range. (You can print an int with printf("%d"), is that what you're after?)

    I'm also not sure what's up with your program now. It must be your math though, the code looks fine otherwise.

    Just two notes: here,
    Code:
    number_of_meals_eaten_at_subway = (eat_this_semester - salm*limit_on_meal_card)/(sas - salm);
    you're using integer division. It will truncate down to the nearest int. Might not be what you want (consider doubles instead, or at least casting that expression to type double).

    Also, getch() is non-standard. I'd suggest using getchar() instead. See the FAQ.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    BLG
    BLG is offline
    Registered User
    Join Date
    Sep 2009
    Posts
    14
    Quote Originally Posted by dwks View Post
    I'm not sure what you're saying here. You can do anything with an int that you can with a char, except that an int has even more range. (You can print an int with printf("%d"), is that what you're after?)

    I'm also not sure what's up with your program now. It must be your math though, the code looks fine otherwise.

    Just two notes: here,
    Code:
    number_of_meals_eaten_at_subway = (eat_this_semester - salm*limit_on_meal_card)/(sas - salm);
    you're using integer division. It will truncate down to the nearest int. Might not be what you want (consider doubles instead, or at least casting that expression to type double).

    Also, getch() is non-standard. I'd suggest using getchar() instead. See the FAQ.
    The problem I am having is I can not get this answer :
    You will eat 76 meals at Subway and 74 meals at Lazy Moon.
    You will have $1 left on your card.

    Can you tell me where in my program where my mistake is and what I can do to fix it. That is the problem I am having.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by BLG View Post
    Can you tell me where in my program where my mistake is and what I can do to fix it. That is the problem I am having.
    You did not explain what went wrong. Does execution complete without errors? If so, what is the output and how does it differ from what you wanted? (Since your code looks ok otherwise, I would focus on what "truncate down" means)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    BLG
    BLG is offline
    Registered User
    Join Date
    Sep 2009
    Posts
    14
    Quote Originally Posted by MK27 View Post
    You did not explain what went wrong. Does execution complete without errors? If so, what is the output and how does it differ from what you wanted? (Since your code looks ok otherwise, I would focus on what "truncate down" means)
    I do not know what you mean by "truncate down but when I run the program, I do not get no errors. I just figured it out. I just have to figure out what the balance is left

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    LOL!

    Y'all might want to consider category one in this thread:

    Why do you visit cboard?

    Depends on how Draconian your prof is, but consider yerselves warned!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. threaded program doesn't work?
    By OcTO_VIII in forum Linux Programming
    Replies: 1
    Last Post: 12-11-2003, 12:37 PM
  4. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 11:05 AM
  5. how does this program work
    By ssjnamek in forum C++ Programming
    Replies: 2
    Last Post: 01-02-2002, 01:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21