Thread: Strange or stupidity

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    28

    Strange or stupidity

    On a particluar menu option I have different option 1-6. I have put an if statement in my code to produce an error mesage if the input is greater than 6 but my program just seems to ignore it and exit.

    [CODE]
    {

    int choice = 0; /* sets choice as an integer value starts at 0*/
    while ((choice <=6)) /* Sets a while statement so case is only active within range less than 6*/
    {
    switch (choice) /* Choice number will represent menu */
    {
    case 1:
    {
    num_lines(); /* When 1 is selected takes it to sub program num_lines */
    break;
    }
    case 2: /* Case 2 to case 6 same as above */
    {
    num_words();
    break;
    }
    case 3:
    {
    num_chars();
    break;
    }
    case 4:
    {
    chars_rev();
    break;
    }
    case 5:
    {
    lines_rev();
    break;
    }
    case 6:
    {
    exit(1);
    }
    if(choice >=7)
    {
    printf("invalid selection");
    getch();
    }

    }
    {/CODE]

    Any help would be much appreciated cheers
    Cheers

    Bazza

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    [Code]
    efterew
    [\Code]
    Cheers

    Bazza

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Layout the code properly (indent it) and you'll see that the if statement is in the wrong place. For a start, it's buried in the switch, after the exit(1), therefore it will never run. Also, it's inside a while loop that is controlled with (choice <=6), so testing for 7 or higher inside this loop will also never work.

    btw, code tags are like in my sig.

    And delete the duplicate thread!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    Thanks hammer sorry about that I type

    {/code]

    by accident at the end dont know how to delete other thread doh

    thx again
    Cheers

    Bazza

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    delete: edit the other thread, and at the top you'll see a delete tick box and button.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    Hmmmmm

    I cant seem to do it because if I put the if statement before the while statement it still ignores it. But if I change the int choice=0 it automatically runs the if statement

    your permanently in deep state of confusion
    Cheers

    Bazza

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    An example for you to study
    Code:
    int choice = -1;
    	
    while (choice != 0)
    {
    	switch (choice)
    	{
    		case 0:  /* Do nothing */
    			break;
    		case 1: 
    			dostuff1(); 
    			break;
    		case 2: 
    			dostuff2(); 
    			break;
    		case 3: 
    			dostuff3(); 
    			break;
    		case 4: 
    			dostuff4(); 
    			break;
    		default: 
    			dootherstuff(); 
    			break;
    	}
    }
    Obviously, somewhere you'll need to change the value of choice.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    OK im getting there I think but for some reason the error i.e Hello just for the sake of a message comes up when the program begins as well

    I know its probably something simple but i cant figure it out, think im thinking to hard
    Code:
    int choice=-1;   /* sets choice as an integer value starts at 0*/
    
    
    
    	while (choice!=0&&choice<7)    /* Sets a while statement so case is only active within range less than 6*/
    
    	{
    	switch (choice)         /* Choice number will represent menu */
    
    	{
    	case 1:
    	{
    	num_lines();            /* When 1 is selected takes it to sub program num_lines */
    	break;
    	}
    	case 2:                 /* Case 2 to case 6 same as above */
    	{
    	num_words();
    	break;
    	}
    	case 3:
    	{
    	num_chars();
    	break;
    	}
    	case 4:
    	{
    	chars_rev();
    	break;
    	}
    	case 5:
    	{
    	lines_rev();
    	break;
    	}
    	case 6:
    	{
    	exit();
    	}
    	default:
    	{
    	printf("hello");
    	getch();
    	}
    }
    thx again
    Cheers

    Bazza

  9. #9
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    You may also want to check for negative values since int is a signed type.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Bazza
    OK im getting there I think but for some reason the error i.e Hello just for the sake of a message comes up when the program begins as well

    I know its probably something simple but i cant figure it out, think im thinking to hard

    Code:
    int choice=-1;   /* sets choice as an integer value starts at 0*/
    
    	while (choice!=0&&choice<7)    
    <--snip--->
    	default:
    	{
    	printf("hello");
    	getch();
    	}
    }
    Look at the logic of the loop. The first time through, choice will be -1, and will therefore drop straight into the "default" section. And, I presume that you are actually changing the value of choice somewhere else in the code? In you example given you are asking the user for a choice, I take it this is done in code you haven't posted? It doesn't matter if so, I don't want/need to see it, just as long as you understand
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange error -- COM DLL is not installed correctly?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 07-16-2007, 08:32 AM
  2. "new" is strange
    By The Wazaa in forum C++ Programming
    Replies: 26
    Last Post: 03-06-2006, 12:32 PM
  3. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  4. strange strange functions
    By threahdead in forum C Programming
    Replies: 4
    Last Post: 10-13-2002, 05:31 PM
  5. bcc32 compiling error (really strange!!)
    By jester in forum C++ Programming
    Replies: 14
    Last Post: 01-26-2002, 04:00 PM