Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with code

Status
Not open for further replies.

FURY

Programmer
Aug 9, 2001
17
GB
can someone tell me why this code doesnt work?

#include <stdio.h>
#include <conio.h>
#include <ctype.h>




int main(void)
{
char c;
char option;

printf(&quot;\nMAIN MENU\n&quot;);
printf(&quot;\n1. Add Order\n&quot;);
printf(&quot;2. Chart\n&quot;);
printf(&quot;3. Percentage\n&quot;);
printf(&quot;4. Average\n&quot;);
printf(&quot;5. Total Cost\n&quot;);
printf(&quot;6. Exit\n&quot;);





printf(&quot;\nWhich menu would you like?&quot;);
scanf(&quot;%c&quot;, &c);


switch(c)
{
case '1' : addorder();
break;

case '2' : printf(&quot;\ndisplay chart here&quot;);
main ();
break;

case '3' : printf(&quot;\ndisplay percentage here&quot;);
main ();
break;

case '4' : printf(&quot;\naverage&quot;);
main ();
break;

case '5' : printf(&quot;\nTotal cost is &quot;);
main ();
break;

case '6' : printf(&quot;You chose to exit, press a button&quot;);
option = getch();
exit(0);

}
return 0;

}

int addorder()

{
int meal;
int side;
int portions;
float mealprice;
float sideprice;
float ordtotal;

printf(&quot;\nMain Meal &quot;);
printf(&quot;Side Order\n&quot;);
printf(&quot;\n1. Beef Burger @ 1.50 &quot;);
printf(&quot;6. Chips @ 1.60\n&quot;);
printf(&quot;2. Chicken Bites @ 1.80 &quot;);
printf(&quot;7. Salad @ 1.50\n&quot;);
printf(&quot;3. Vegetarian Burger @ 2.00 &quot;);
printf(&quot;8. Baked Potatoe @ 1.50\n&quot;);
printf(&quot;4. Breakfast Burger @ 1.50 &quot;);
printf(&quot;9. NONE @ 0.00\n&quot;);
printf(&quot;5. Scampi @ 1.50 \n&quot;);


printf(&quot;\nEnter Meal Number: &quot;);
scanf(&quot;%d&quot;, &meal);

printf(&quot;\nEnter Side Order Number: &quot;);
scanf(&quot;%d&quot;, &side);

printf(&quot;\nEnter Number of Portions: &quot;);
scanf(&quot;%d&quot;, &portions);

if(meal==&quot;1&quot; &&side==&quot;6&quot;)
{
mealprice=1.50;
sideprice=1.50;
ordtotal=(mealprice+sideprice)*portions;
}

else if(meal==&quot;1&quot;&&side==&quot;7&quot;)
{
mealprice=1.50;
sideprice=1.50;
ordtotal=(mealprice+sideprice)*portions;
}

else if(meal==&quot;1&quot;&&side==&quot;8&quot;)
{
mealprice=1.50;
sideprice=1.50;
ordtotal=(mealprice+sideprice)*portions;
}

else if(meal==&quot;1&quot;&&side==&quot;9&quot;)
{
mealprice=1.50;
sideprice=1.50;
ordtotal=(mealprice+sideprice)*portions;
}

else if(meal==&quot;2&quot;&&side==&quot;6&quot;)
{
mealprice=1.50;
sideprice=1.50;
ordtotal=(mealprice+sideprice)*portions;
}

else if(meal==&quot;2&quot;&&side==&quot;7&quot;)
{
mealprice=1.50;
sideprice=1.50;
ordtotal=(mealprice+sideprice)*portions;
}

else if(meal==&quot;2&quot;&&side==&quot;8&quot;)
{
mealprice=1.50;
sideprice=1.50;
ordtotal=(mealprice+sideprice)*portions;
}

else if(meal==&quot;2&quot;&&side==&quot;9&quot;)
{
mealprice=1.50;
sideprice=1.50;
ordtotal=(mealprice+sideprice)*portions;
}

else if(meal==&quot;3&quot;&&side==&quot;6&quot;)
{
mealprice=1.50;
sideprice=1.50;
ordtotal=(mealprice+sideprice)*portions;
}

else if(meal==&quot;3&quot;&&side==&quot;7&quot;)
{
mealprice=1.50;
sideprice=1.50;
ordtotal=(mealprice+sideprice)*portions;
}

else if(meal==&quot;3&quot;&&side==&quot;8&quot;)
{
mealprice=1.50;
sideprice=1.50;
ordtotal=(mealprice+sideprice)*portions;
}

else if(meal==&quot;3&quot;&&side==&quot;9&quot;)
{
mealprice=1.50;
sideprice=1.50;
ordtotal=(mealprice+sideprice)*portions;
}

else if(meal==&quot;4&quot;&&side==&quot;6&quot;)
{
mealprice=1.50;
sideprice=1.50;
ordtotal=(mealprice+sideprice)*portions;
}

else if(meal==&quot;4&quot;&&side==&quot;7&quot;)
{
mealprice=1.50;
sideprice=1.50;
ordtotal=(mealprice+sideprice)*portions;
}

else if(meal==&quot;4&quot;&&side==&quot;8&quot;)
{
mealprice=1.50;
sideprice=1.50;
ordtotal=(mealprice+sideprice)*portions;
}

else if(meal==&quot;4&quot;&&side==&quot;9&quot;)
{
mealprice=1.50;
sideprice=1.50;
ordtotal=(mealprice+sideprice)*portions;
}

else if(meal==&quot;5&quot;&&side==&quot;6&quot;)
{
mealprice=1.50;
sideprice=1.50;
ordtotal=(mealprice+sideprice)*portions;
}

else if(meal==&quot;5&quot;&&side==&quot;7&quot;)
{
mealprice=1.50;
sideprice=1.50;
ordtotal=(mealprice+sideprice)*portions;
}

else if(meal==&quot;5&quot;&&side==&quot;8&quot;)
{
mealprice=1.50;
sideprice=1.50;
ordtotal=(mealprice+sideprice)*portions;
}

else if(meal==&quot;5&quot;&&side==&quot;9&quot;)
{
mealprice=1.50;
sideprice=1.50;
ordtotal=(mealprice+sideprice)*portions;
}

printf(&quot;\nOrder Total = %d&quot;, ordtotal);


main ();

}
 
the problem is the total ordtotal doesnt work just gives me 0 and when it returns the the main menu the program exits
 
Do not you see that you are using the same code over, over,over,over , over again. Prepare a function and just it will calculate everything you want for any choice the user chooses.

eof Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
i know but this is my first c program and i havnt a clue what im doing forgive my stupidity
 
hahaha...my sentiments exactly aphrodita :)

Here are some general comments on your code. Try to work on this stuff and post again if you still can't get it to work.

> #include <stdio.h>
> #include <conio.h>
> #include <ctype.h>
>
>
>
>
> int main(void)
> {
> char c;
> char option;
>
> printf(&quot;\nMAIN MENU\n&quot;);
> printf(&quot;\n1. Add Order\n&quot;);
> printf(&quot;2. Chart\n&quot;);
> printf(&quot;3. Percentage\n&quot;);
> printf(&quot;4. Average\n&quot;);
> printf(&quot;5. Total Cost\n&quot;);
> printf(&quot;6. Exit\n&quot;);
>
>
>
>
>
> printf(&quot;\nWhich menu would you like?&quot;);

fflush(stdout);

since output won't be flushed unless a line ends in a newline or the buffer fills up, it's best to explicitly flush it here to make sure your user sees the prompt before they can enter any input.

> scanf(&quot;%c&quot;, &c);

Part of your problem may have to do with using scanf() which can be tricky to use. Try fgets() with strtol() instead.

Below you call main() recursively. Although this isn't wrong, it's unnecessary and adds the overhead of another function call. Use a &quot;forever&quot; loop instead and remove the calls to main():

for (;;) {
>
>
> switch(c)
> {
> case '1' : addorder();
> break;
>
> case '2' : printf(&quot;\ndisplay chart here&quot;);
> main ();
> break;
>
> case '3' : printf(&quot;\ndisplay percentage here&quot;);
> main ();
> break;
>
> case '4' : printf(&quot;\naverage&quot;);
> main ();
> break;
>
> case '5' : printf(&quot;\nTotal cost is &quot;);
> main ();
> break;
>
> case '6' : printf(&quot;You chose to exit, press a button&quot;);
> option = getch();
> exit(0);
>
> }
} /* end of for loop */
> return 0;
>
> }
>
> int addorder()
>
> {
> int meal;
> int side;
> int portions;
> float mealprice;
> float sideprice;
> float ordtotal;
>
> printf(&quot;\nMain Meal &quot;);
> printf(&quot;Side Order\n&quot;);
> printf(&quot;\n1. Beef Burger @ 1.50 &quot;);
> printf(&quot;6. Chips @ 1.60\n&quot;);
> printf(&quot;2. Chicken Bites @ 1.80 &quot;);
> printf(&quot;7. Salad @ 1.50\n&quot;);
> printf(&quot;3. Vegetarian Burger @ 2.00 &quot;);
> printf(&quot;8. Baked Potatoe @ 1.50\n&quot;);
> printf(&quot;4. Breakfast Burger @ 1.50 &quot;);
> printf(&quot;9. NONE @ 0.00\n&quot;);
> printf(&quot;5. Scampi @ 1.50 \n&quot;);

This is a bizarre ordering of numbers. Option 9, NONE, is a nice touch. Um, I'd like to have 5 NONEs please ;-)

>
>
> printf(&quot;\nEnter Meal Number: &quot;);

fflush(stdout);

> scanf(&quot;%d&quot;, &meal);
>
> printf(&quot;\nEnter Side Order Number: &quot;);
> scanf(&quot;%d&quot;, &side);
>
> printf(&quot;\nEnter Number of Portions: &quot;);
> scanf(&quot;%d&quot;, &portions);
>
> if(meal==&quot;1&quot; &&side==&quot;6&quot;)

This comparison doesn't make any sense, you're comparing an integer to a string literal, same goes for all the stuff below. Try:

if (meal==1 && side==6)

> {
> mealprice=1.50;
> sideprice=1.50;

If these are fixed amounts, put them somewhere in a define at the top of your source file:

#define MEAL_PRICE 1.5
#define SIDE_PRICE 1.5

> ordtotal=(mealprice+sideprice)*portions;
> }
>
> else if(meal==&quot;1&quot;&&side==&quot;7&quot;)
> {
> mealprice=1.50;
> sideprice=1.50;
> ordtotal=(mealprice+sideprice)*portions;
> }
>
> else if(meal==&quot;1&quot;&&side==&quot;8&quot;)
> {
> mealprice=1.50;
> sideprice=1.50;
> ordtotal=(mealprice+sideprice)*portions;
> }
>
> else if(meal==&quot;1&quot;&&side==&quot;9&quot;)
> {
> mealprice=1.50;
> sideprice=1.50;
> ordtotal=(mealprice+sideprice)*portions;
> }
>
> else if(meal==&quot;2&quot;&&side==&quot;6&quot;)
> {
> mealprice=1.50;
> sideprice=1.50;
> ordtotal=(mealprice+sideprice)*portions;
> }
>
> else if(meal==&quot;2&quot;&&side==&quot;7&quot;)
> {
> mealprice=1.50;
> sideprice=1.50;
> ordtotal=(mealprice+sideprice)*portions;
> }
>
> else if(meal==&quot;2&quot;&&side==&quot;8&quot;)
> {
> mealprice=1.50;
> sideprice=1.50;
> ordtotal=(mealprice+sideprice)*portions;
> }
>
> else if(meal==&quot;2&quot;&&side==&quot;9&quot;)
> {
> mealprice=1.50;
> sideprice=1.50;
> ordtotal=(mealprice+sideprice)*portions;
> }
>
> else if(meal==&quot;3&quot;&&side==&quot;6&quot;)
> {
> mealprice=1.50;
> sideprice=1.50;
> ordtotal=(mealprice+sideprice)*portions;
> }
>
> else if(meal==&quot;3&quot;&&side==&quot;7&quot;)
> {
> mealprice=1.50;
> sideprice=1.50;
> ordtotal=(mealprice+sideprice)*portions;
> }
>
> else if(meal==&quot;3&quot;&&side==&quot;8&quot;)
> {
> mealprice=1.50;
> sideprice=1.50;
> ordtotal=(mealprice+sideprice)*portions;
> }
>
> else if(meal==&quot;3&quot;&&side==&quot;9&quot;)
> {
> mealprice=1.50;
> sideprice=1.50;
> ordtotal=(mealprice+sideprice)*portions;
> }
>
> else if(meal==&quot;4&quot;&&side==&quot;6&quot;)
> {
> mealprice=1.50;
> sideprice=1.50;
> ordtotal=(mealprice+sideprice)*portions;
> }
>
> else if(meal==&quot;4&quot;&&side==&quot;7&quot;)
> {
> mealprice=1.50;
> sideprice=1.50;
> ordtotal=(mealprice+sideprice)*portions;
> }
>
> else if(meal==&quot;4&quot;&&side==&quot;8&quot;)
> {
> mealprice=1.50;
> sideprice=1.50;
> ordtotal=(mealprice+sideprice)*portions;
> }
>
> else if(meal==&quot;4&quot;&&side==&quot;9&quot;)
> {
> mealprice=1.50;
> sideprice=1.50;
> ordtotal=(mealprice+sideprice)*portions;
> }
>
> else if(meal==&quot;5&quot;&&side==&quot;6&quot;)
> {
> mealprice=1.50;
> sideprice=1.50;
> ordtotal=(mealprice+sideprice)*portions;
> }
>
> else if(meal==&quot;5&quot;&&side==&quot;7&quot;)
> {
> mealprice=1.50;
> sideprice=1.50;
> ordtotal=(mealprice+sideprice)*portions;
> }
>
> else if(meal==&quot;5&quot;&&side==&quot;8&quot;)
> {
> mealprice=1.50;
> sideprice=1.50;
> ordtotal=(mealprice+sideprice)*portions;
> }
>
> else if(meal==&quot;5&quot;&&side==&quot;9&quot;)
> {
> mealprice=1.50;
> sideprice=1.50;
> ordtotal=(mealprice+sideprice)*portions;
> }

Whew! All of these if statements do the same thing, what is the purpose of even having them in the 1st place if the meal price, side price and order total calculation are the same regardless of the choice?

>
> printf(&quot;\nOrder Total = %d&quot;, ordtotal);

Add a newline at the end is probably better, so the next prompt doesn't occur on the same line as the order total.

>
>
> main ();

Take this out and return an int:

return 0;
>
> }



Russ
bobbitts@hotmail.com
 
ok i posted sort of the wrong code the prices were supposed to be different but i copied the ones b4 i changed it foolish me i know :) thx for all the help ill have another try, and havent u ever wanted to order nothing ;-) (its not me that wants the none as a option)
 
ok well this may be a dumb idea but i thought i try and replace all the if and else statements with 2 switches one to represent the meal and one for the side order, but the 2 switches dont seem to like each other as the code i post below has it so the main meal works but the side order always reads 0 i tryed changed them both but the main meal started out putting only 0 as well, heres my new and probably even worse code:


#include <stdio.h>
#include <conio.h>
#include <ctype.h>




int main(void)
{
char c;
char option;

printf(&quot;\nMAIN MENU\n&quot;);
printf(&quot;\n1. Add Order\n&quot;);
printf(&quot;2. Chart\n&quot;);
printf(&quot;3. Percentage\n&quot;);
printf(&quot;4. Average\n&quot;);
printf(&quot;5. Total Cost\n&quot;);
printf(&quot;6. Exit\n&quot;);





printf(&quot;\nWhich menu would you like?&quot;);
scanf(&quot;%c&quot;, &c);


switch(c)
{
case '1' : addorder();
break;

/*ignore case 2-5 as they will be set up later they return main after displaying tet so they do something for now and go back to the main menu*/

case '2' : printf(&quot;\ndisplay chart here&quot;);
main ();
break;

case '3' : printf(&quot;\ndisplay percentage here&quot;);
main ();
break;

case '4' : printf(&quot;\naverage&quot;);
main ();
break;

case '5' : printf(&quot;\nTotal cost is &quot;);
main ();
break;

case '6' : printf(&quot;You chose to exit, press a button&quot;);
option = getch();
exit(0);

}
return 0;

}

int addorder()

{
char meal;
int side;
int portions;
float mealprice;
float sideprice;
float ordtotal;

/*this looks a strange order as it prints the main meal and side orders side by side and yes NONE is still there*/

printf(&quot;\nMain Meal &quot;);
printf(&quot;Side Order\n&quot;);
printf(&quot;\nA. Beef Burger @ 1.50 &quot;);
printf(&quot;1. Chips @ 1.60\n&quot;);
printf(&quot;B. Chicken Bites @ 1.80 &quot;);
printf(&quot;2. Salad @ 1.50\n&quot;);
printf(&quot;C. Vegetarian Burger @ 2.00 &quot;);
printf(&quot;3. Baked Potatoe @ 1.50\n&quot;);
printf(&quot;D. Breakfast Burger @ 1.50 &quot;);
printf(&quot;4. NONE @ 0.00\n&quot;);
printf(&quot;E. Scampi @ 1.50 \n&quot;);

/*im hoping these statements scan the letter or number typed for use in the switch statements below apart from portions which will be used to calculate total cost*/

printf(&quot;\nEnter Meal Number: &quot;);
scanf(&quot;%s&quot;, &meal);

printf(&quot;\nEnter Side Order Number: &quot;);
scanf(&quot;%d&quot;, &side);

printf(&quot;\nEnter Number of Portions: &quot;);
scanf(&quot;%d&quot;, &portions);

/* im hoping the switch statements will read the value stored from the ones above and store the appropriate value in mealprice and sideprice*/

switch(meal)
{

case 'a':
mealprice=1.50;
break;

case 'b':
mealprice=1.60;
break;

case 'c':
mealprice=1.80;
break;

case 'd':
mealprice=1.50;
break;

case 'e':
mealprice=2.00;
break;


}

switch(side)
{

case '1':
sideprice=1.60;
break;

case '2':
sideprice=1.50;
break;

case '3':
sideprice=1.50;
break;

case '4':
sideprice=0.00;
break;

}

/*this ordtotal does not seem to work for me*/

printf(&quot;\nmeal pricel = %f&quot;, mealprice);
printf(&quot;\nside pricel = %f&quot;, sideprice);
ordtotal=mealprice+sideprice*portions;
printf(&quot;\nOrder Total = %f&quot;, ordtotal);

/*i want it to return to the main menu once this is done which is why i call the main method but the program displays the menu and exits i dont know why*/

main ();

}
 
else if(meal==&quot;5&quot;&&side==&quot;6&quot;)

try replace the double quote with single quote
== is used for character comparison
for string comparison, use strcmp(...)
 
No,no,no !

You have to include all this code // without writing main() in the end
in the while loop.

...
bool status = true;
while (status)
{

All the code that you have go in this loop
...
...

printf(&quot;Do you want to start again ? (Y-Yes / N-No)&quot;);
char state;
scanf(&quot;%c&quot;,state);
if (upcase(state) != 'Y')
status = false;
} // end while loop



Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
ok it kicks up errors with the loop when compiling
 
ok forgetting everything else why doesnt the add bit work its the main thing i need to get working, it adds the main meal as 1.5000 and nothing for the side it doesnt multiply by the portions and its driving me mad heres the code

int addorder()
{
char option;
char state;
char meal;
int side;
int portions;
float mealprice;
float sideprice;
float ordtotal;



printf(&quot;\nMain Meal &quot;);
printf(&quot;Side Order\n&quot;);
printf(&quot;\nA. Beef Burger @ 1.50 &quot;);
printf(&quot;1. Chips @ 1.60\n&quot;);
printf(&quot;B. Chicken Bites @ 1.80 &quot;);
printf(&quot;2. Salad @ 1.50\n&quot;);
printf(&quot;C. Vegetarian Burger @ 2.00 &quot;);
printf(&quot;3. Baked Potatoe @ 1.50\n&quot;);
printf(&quot;D. Breakfast Burger @ 1.50 &quot;);
printf(&quot;4. NONE @ 0.00\n&quot;);
printf(&quot;E. Scampi @ 1.50 \n&quot;);



printf(&quot;\nEnter Meal Number: &quot;);
fflush(stdout);
scanf(&quot;%s&quot;, &meal);

printf(&quot;\nEnter Side Order Number: &quot;);
fflush(stdout);
scanf(&quot;%d&quot;, &side);

printf(&quot;\nEnter Number of Portions: &quot;);
fflush(stdout);
scanf(&quot;%d&quot;, &portions);


switch(meal)
{

case 'a':
mealprice=1.50;
break;

case 'b':
mealprice=1.60;
break;

case 'c':
mealprice=1.80;
break;

case 'd':
mealprice=1.50;
break;

case 'e':
mealprice=2.00;
break;


}

switch(side)
{

case '1':
sideprice=1.60;
break;

case '2':
sideprice=1.50;
break;

case '3':
sideprice=1.50;
break;

case '4':
sideprice=0.00;
break;

}



printf(&quot;\nmeal pricel = %f&quot;, mealprice);
printf(&quot;\nside pricel = %f&quot;, sideprice);
ordtotal=mealprice+sideprice*portions;
printf(&quot;\nOrder Total = %f&quot;, ordtotal);


}
 
Interesting ...
Your meal variable is a string. Make it char !
Your side variable is a integer.Make it char !

eof Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top