# Thread: Can not get this program to work?

1. ## 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
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. 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().

3. Originally Posted by dwks
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. 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. ]

5. Originally Posted by dwks
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. ,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.

7. Originally Posted by dwks
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. Originally Posted by BLG
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)

9. Originally Posted by MK27
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. 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!