Thread: help on code thats all

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    20

    help on code thats all

    i am studying c on my own because i need it for my senior project and i also want to know it for my own.

    Objective: Make menu with 10 options. Option 0 takes u to a silly "prelab" sub function and option 7 (will make it q later but for now 7 is good, will make the program quit)

    Problem: I can make the program quit with the option 7 and make the program go to the prelab with option 0 but i can not make it go back to the main menu. Ive tried implementing another loop and many other things but can seem to figure it out, any help will be apreciated it. Thank you.

    The program doesnt wait for the user to input a choice YES OR NO like I wanted to; in the desicionTime()

    Code:
    // TheMenu.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "stdio.h"
    #include "stdlib.h"
    
    void desicionTime (void);
    
    int main()
    {
    	for(;;)
    	{
    	char input;
    	int choice;
    
    	printf("\n\n	0. PreLab\n");
    	printf("	1. The Menu\n");
    	printf("	2. Switch Case\n");
    	printf("	3. Random\n");
    	printf("	4. Conversion\n");
    	printf("	5. Game of Chance\n");
    	printf("	6. Function Gnenerator\n");
    	printf("	7. Attributes Menu\n");
    	printf("	8. Resistor Color Code\n");
    	printf("	9. Regression Line Crap\n");
    	printf("	q. Press q to quit: \n\n");
    
    	printf("	Choose an option and press enter please:");
    	scanf("%d",&choice);
    		if (choice==7)
    		{
    		break;
    		}
    		if (choice==0)
    		{
    		system("cls");
    		desicionTime();
    		}
    	}
    	return (0);
    }
    
    
    
    
    // desicionTime.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include "stdio.h"
    #include "stdlib.h"
    
    desicionTime(void)
    {
    int age;
    char input;
    int loop;
    	for (;;)
    	{
    		printf("hey yo, what is your age? ");
    		scanf("%d",&age);
    
    		if (age<=25)
    		{
    		printf("\n\nyou are a young buck\n\n");
    		printf("\n\nagain?(y/n)");
    		input=getchar();
    			if (input=='n')
    			{
    			break;
    			}
    			if (input=='y')
    			{
    			desicionTime();
    			}
                                                    return(0)
    		}
    		else if (age>45)
    		{
    		printf("\n\nyou are an old fart\n\n");
    		}
    		else
    		{
    		printf("\n\nyou aiight\n\n");
    		}
    	}
    	return(0);
    }
    I dont want to use switch case for now, only if that is the only option left. But there are probably many other ways to do it.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    return makes the function stop. You don't want to stop while the for-loop is still going, do you? Of course not. So don't put return inside the for-loop.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    decision time doesn't have a return type. I suppose the compiler gives the void type with a warning.
    But you return (0) from it. That should give a warning/error. Remove the return(0) from decisionTime.

    Specify a bit the error. Probably decisionTime never returns, too sleepy to see why.
    Provide more information about what is the proble. Never ends? Crashes? Prints the same thing all over again? Though I m sure somebody will solve this real quick

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    i dont know which return you are talking about but i tried taking each of them at a time and it didnt help. Can you be a little more specific?

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    Quote Originally Posted by C_ntua View Post
    decision time doesn't have a return type. I suppose the compiler gives the void type with a warning.
    But you return (0) from it. That should give a warning/error. Remove the return(0) from decisionTime.

    Specify a bit the error. Probably decisionTime never returns, too sleepy to see why.
    Provide more information about what is the proble. Never ends? Crashes? Prints the same thing all over again? Though I m sure somebody will solve this real quick
    in this case when i put 21 for desicionTime() the line that says "again?" prints out on the screen but it does not wait for an answer and it goes straight back to the main menu.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    getchar and scanf do not agree with one another. Use one, or the other, but never both. In this case, getchar grabs the return key pressed after the previous input and uses that, instead of waiting for a letter.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    tried it just now doesnt make a difference

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    Quote Originally Posted by tabstop View Post
    getchar and scanf do not agree with one another. Use one, or the other, but never both. In this case, getchar grabs the return key pressed after the previous input and uses that, instead of waiting for a letter.
    tried it just now it doesnt work

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by tru.cutru View Post
    tried it just now it doesnt work
    That's what I said. That's why you should try
    Code:
    scanf(" %c", &input);
    instead of getchar. Note that the space is important.

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    20
    holy moly you are a big baller lol i had that scanf but not the spaces, I didnt know that could impact the code so much. WOOOW, thanks alot

    thats tricky stuf right there

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM