# Thread: Help with a program.

1. ## Help with a program.

Yes, this is for homework. Well I need help on creating a menu. I want to create it using either switch, or a while if. I want it to display as:

Code:
```1)  Classify Your Triangle.
2)  String That Does Nothing.
3)  Cash Register Receipt.
5)  End Program.```
Now I want it so that when an option is chosen, and that part of the program it completes, it goes back to the original menu so that you can choose 1 to 5 over again. Also, I would prefer if a goto was not used because it does not seem to be working very easily.

2. So, show us what you've done to solve it so far, then we can help you. That's the "homework rule".
And we shouldn't (obviously) be providing you with the direct solutions, but rather help you solve the problem ourselves.

If you ask your maths teacher "How much is 2 + 2?" then the teacher can say "4" - but if he shows you how to perform addition, you won't come back two minutes later with "How much is 2+3?", right? And he's also "Doing his job as a teacher" if he teaches you to add, rather than to memorize certain number combinations.

Same here, if we show you how you go about solving the problem, then you are much better off than if we show you what the actual answer is. [Not that anyone here is getting paid - from the forum that is!]

I should add that I think you will need BOTH while and switch in your code, and possibly some if-statements too.

Goto's shouldn't be needed [the general consensus is that goto's shouldn't be used - there are some exceptions].

--
Mats

3. I think I may have messed up on some stuff in there, but I'm still learning haha. Hopefully no flamers are here ;x.
Oh and also, they have us programming with CodeWarrior, so we have to use void main instead of int main.

Code:
```			#include <stdio.h>
void main()
{
int side1 = 0;
int side2 = 0;
int side3 = 0;

printf("Here is a list of options for you to choose from:\n1)  Classify Your Trinagle\n2)  String That Does Nothing\n3)  Cash Register Receipt\n4)  Quadratic Forumla\n5)  End Program.\n");

printf("You chose Classify Your Tinagle .  I'm now going to classify a trinagle for you.\nWhat are the side lengths of your trinagle? (Please enter greatest to least)\nA) ");
scanf("%d", &side1);
printf("B) ");
scanf("%d", &side2);
printf("C) ");
scanf("%d", &side3);}
if (side1>side2){
if (side2==side3)
printf("Your trinagle is classified as Obtuse Isoceles Trinagle.\n");
}
if (side1 == side2){
if (side1 == side3)
printf("Your trinagle is classified as Right Equilateral Trinagle.\n");
}
if (side1 != side2){
if (side2 != side3)
printf("Your trinagle is classified as Obtuse Scalene Triangle.\n");
}}

printf("You chose Cash Register Receipt.  Please srart entering prices.  When you are done, enter -999 to stop and your sub-total tax and final total will print out in a receipt.\n");

printf("You chose Quadratic Forumla.  What are the coefficients of your variables?  (Please enter zero if you are missing a term).\n");

end:
printf("Goodbye.");```

4. Your basic flow should be:
Code:
```Display menu to user and get user's response
{
else if( menuchoice == 2 ) Call the "String That Does Nothing" function.
else if( menuchoice == 3 ) Call the "Cash Register Receipt" function.
Display menu to user and get user's response
}```
Your bits of code that do things should be nicely wrapped up in functions.

5. Further to what hkmp... says:

Not sure why you have a bunch of spaces in front of #include <stdio.h> - it's a traditionally not done that way.

Code:
`void main()`
Should be
Code:
`int main()`
And you should put a "return 0;" at the end of your main function.

Code:
```	int menuopt1 = 0;
Not used variables - remove.

If you break long text-lines it makes it MUCH easier to see what's going on [and this way, it reflects what it would look like on screen too!]
Code:
```	printf("Here is a list of options for you to choose from:\n"
"2)  String That Does Nothing\n"
"3)  Cash Register Receipt\n"
"5)  End Program.\n");```
Code:
`end:`
Unused label.

--
Mats

6. Originally Posted by hk_mp5kpdw
Code:
```Display menu to user and get user's response
{
else if( menuchoice == 2 ) Call the "String That Does Nothing" function.
else if( menuchoice == 3 ) Call the "Cash Register Receipt" function.
Display menu to user and get user's response
}```
Your bits of code that do things should be nicely wrapped up in functions.
And how would I call the functions? As in, inside of the if's / else if's I would write that part of the program?

Originally Posted by matsp
Further to what hkmp... says:
Code:
`void main()`
Should be
Code:
`int main()`
I had stated, for the program they have us using and what my teacher prefers (stupid choice) he has us using void main instead of int main.

About the "return 0;" he also said it was uneeded most of the time, although in all of the sources I see it's almost always used. He hasn't taught us, but may I ask what it's use is?

And I was just trying to write the program fast so I didn't break it up, I was going to do that at the end of it.

The "end:" was when I was attempting to try out goto which didn't work in the first place heh.

7. Well, main() is a function that returns an integer showing "success or fail" - whether you declare it as void, float, double, int *, struct somehorrible_long_name_thing or anything else. Because main is called from the startup code of the C runtime. As for ALL code that gets CALLED FROM SOMEWHERE YOU HAVE NO CONTROL OVER, the calling code DICTATES IN NO UNCERTAIN TERMS what the return value and parameters should be.

Imagine if we all had the right to define how to interpret and use traffic lights the way we feel. "Excuse me officer, but the other car hit me when I drove as I have defined the traffic lights: Stop on Green and go on Red". Someone "independent" has to say that red means stop, green means go and amber/yellow means "attention - about to change soon".

Likewise, the C runtime says what parameters main take and how it returns the result.

Now what happens if you declare main as "void"? Nothing much. Until something else is USING the return value from the application to determine whether your application "succeded or not" - you will have a return value that is "random" - it is not strictly random as a random number generator, but it is "undefined", as in "there's no way to say for sure what it will be". It will depend on what is inside main, what functions are called, how registers are used in main - some register holds the return value from a function, and that means whatever is in that register will be used. A void function doesn't "set" the return value register to anything - it leaves it to whatever it is when the function ends - some possible scenarios include:
- the result of some calculation
- an intermediate value inside some calculation
- the contents of one of your variables
- the memory address of some array location used in your code
- the last return value from some function called from main

None of these are likely to be "correct" in indicating whether your application completed or not.

There is one exception : if you ALWAYS use "exit()" to leave your application, main() never returns, so it serves no function to return a value from main.

--
Mats

--
Mats