Help with a program.

This is a discussion on Help with a program. within the C Programming forums, part of the General Programming Boards category; Yes, this is for homework. Well I need help on creating a menu. I want to create it using either ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    3

    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.
    4)  Quadratic Formula.
    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.
    Last edited by Breakthrough; 11-05-2007 at 07:22 AM. Reason: Grammar/Spelling Errors

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Last edited by matsp; 11-05-2007 at 07:33 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    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 menuopt = 0;
    	int menuopt1 = 0;
    	int menuopt2 = 0;
    	int menuopt3 = 0;
    	int menuopt4 = 0;
    	int menuopt5 = 0;
    	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");
    			
    			scanf("&#37;d", &menuopt);
    			while  (menuopt != 5) {
    			if (menuopt == 1){
    			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");
    			}}
    			
    			
    			if (menuopt == 2)
    			printf("Your chose String That Does Nothing.  Please enter your string:\n");
    			
    			
    			
    			if (menuopt == 3)
    			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");
    			
    			
    			
    			if (menuopt == 4)
    			printf("You chose Quadratic Forumla.  What are the coefficients of your variables?  (Please enter zero if you are missing a term).\n");
    			
    			
    			
    			if (menuopt == 5)
    			end:
    			printf("Goodbye.");
    Last edited by Breakthrough; 11-05-2007 at 01:27 PM.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Your basic flow should be:
    Code:
    Display menu to user and get user's response
    while( menuchoice != 5 )
    {
        if( menuchoice == 1 )  Call the "Classify Your Triangle" function
        else if( menuchoice == 2 ) Call the "String That Does Nothing" function.
        else if( menuchoice == 3 ) Call the "Cash Register Receipt" function.
        else if( menuchoice == 4 ) Call the "Quadratic Formula" function
        Display menu to user and get user's response
    }
    Your bits of code that do things should be nicely wrapped up in functions.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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;
    	int menuopt2 = 0;
    	int menuopt3 = 0;
    	int menuopt4 = 0;
    	int menuopt5 = 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"
                  "1)  Classify Your Trinagle\n"
                  "2)  String That Does Nothing\n"
                  "3)  Cash Register Receipt\n"
                  "4)  Quadratic Forumla\n"
                  "5)  End Program.\n");
    Code:
    end:
    Unused label.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    3
    Quote Originally Posted by hk_mp5kpdw View Post
    Your basic flow should be:
    Code:
    Display menu to user and get user's response
    while( menuchoice != 5 )
    {
        if( menuchoice == 1 )  Call the "Classify Your Triangle" function
        else if( menuchoice == 2 ) Call the "String That Does Nothing" function.
        else if( menuchoice == 3 ) Call the "Cash Register Receipt" function.
        else if( menuchoice == 4 ) Call the "Quadratic Formula" 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?



    Quote Originally Posted by matsp View Post
    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. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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