Thread: Please help me!

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    24

    Please help me!

    I don't know why, the moment I enter a value, the debugging is terminated. Someone please shed some light.
    Code:
    #include<stdio.h>
    
    void menu(void);
    
    int choice;
    
    main()
    {
    	menu();  
    }
    
    void menu(void)
    {
    	int selection1 = 0;
    	
    	do
        {
    		int choice = 0;
    		puts("\n\n\t-----Solar Airways-----");
    		puts("\n");
    		puts("\n\t1. Reserve Ticket");
    		puts("\n\t2. View Seats");
    		puts("\n\t3. Quit\n");
    		puts("\n\tEnter a selection: [1/2/3]");
    
    		scanf_s("%d", &choice);
        }
    	while (choice >= 1 && choice <= 3);
    	
    	selection1 = choice;
    
    	while (selection1>= 1 && selection1<= 3)
        {
    		if (selection1 == 1)
            {
    			int selection2 = 0;
    			printf("hi")
    			int choice2 = 0;
    		}
    
    		if (selection1 == 2)
    		{
    			printf("good")
    		}
    	}
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    For one, you should be careful about your scope rules -- are the two variables named choice actually the same variable? Which one is used in the while part of the do-while? Et cetera.

    Also, if you can get out of the loop, then choice must be outside the range of 1-3, and what's that gonig to do to your second while loop?

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    24
    How bout if I change it to this

    Code:
    #include<stdio.h>
    
    void menu(void);
    
    int choice, choice2;
    
    main()
    {
    	menu();  
    }
    
    void menu(void)
    {
    	int selection1 = 0;
    	
    	do
        {
    		int choice = 0;
    		puts("\n\n\t-----Solar Airways-----");
    		puts("\n");
    		puts("\n\t1. Reserve Ticket");
    		puts("\n\t2. View Seats");
    		puts("\n\t3. Quit\n");
    		puts("\n\tEnter a selection: [1/2/3]");
    
    		scanf_s("%d", &choice);
        }
      	while (choice < 1 || choice > 3);
    	
    	selection1 = choice;
    
    	while (selection1>= 1 && selection1<= 3)
        {
    		if (selection1 == 1)
            {
    			
    			printf("hi")
    		}
    
    		if (selection1 == 2)
    		{
    			printf("good")
    		}
    	}
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't know. How bout that?

    You still have two variables called choice, although at least your do-while loop looks better.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    24
    Can you point out where I have 2 choice? I only see one. Thanks.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have int choice in global status, before main(), and you another int choice in menu().

    That will almost always lead to errors/confusion.

Popular pages Recent additions subscribe to a feed