Thread: truncated loop

  1. #1
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88

    truncated loop

    The switch statement truncates due to the fact that a char variable is being associated with the integer variable choice. How can I fix the switch statement so that it recognizes the Q inputted by the user instead of truncating?
    Can I scan for a integer or a char and place the value into integer choice?




    Code:
    #define _CRT_SECURE_NO_DEPRECATE
    #include <stdio.h>
    
    
    
    //function prototypes
    void sumIntegers ();
    
    void sumDoubles ();
    
    void calcFactorial ();
    
    
    
    int main()
    {
    	int choice = 0;
    	/*char quit = 0;*/
    	char Q = 0;
    	
    
    		printf("\t\tWelcome, please select from the following options.\n\n\n\n");
    
    do
    {
    		
    		printf("Please choose from the following options:\n\n\t1: Sum of even integers\n\n\t2: Sum of dollar amounts\n\n\t3: Factorial of a number\n\n\tQ: to quit\n\n\t:");
    		
    		
    		scanf(" %d", &choice);
    		/*scanf(" %c", &choice);		*/
    	
    	switch (choice)
    	{
    		
    		case 1 :
    			{
    			sumIntegers ();
    			break;
    			}
    		
    		case 2 :
    			{
    			sumDoubles ();
    			break;
    			}
    		
    		case 3 :
    			{
    			calcFactorial ();
    			break;
    			}
    		
    		case 'Q' :
    			{
    			printf(" Have a nice day.\n\n");
    			return 0;  // break
    			}
    		
    		default :
    			{
    			printf("\n\n\n*** You entered an invalid choice!!! ***\n\n\n");
    			}
    	}	
    
    
    	
    }
    while(choice != Q || choice > 3 || choice < 1);	// make sure to correct quit loop
    
    
    	return 0;
    }

  2. #2
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Why does choice have to be an integer? Why not make choice a char and then make the switch/case like so:

    Code:
    switch(choice)
    {
         case 'Q':
              ...
         break;
    
         case '1':
              ...
         break;
    }
    And so on?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  3. #3
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Quote Originally Posted by Neo1 View Post
    Why does choice have to be an integer? Why not make choice a char and then make the switch/case like so:

    Code:
    switch(choice)
    {
         case 'Q':
              ...
         break;
    
         case '1':
              ...
         break;
    }
    And so on?
    Thanks Neo1 I'll give that a try

  4. #4
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Quote Originally Posted by Neo1 View Post
    Why does choice have to be an integer? Why not make choice a char and then make the switch/case like so:

    Code:
    switch(choice)
    {
         case 'Q':
              ...
         break;
    
         case '1':
              ...
         break;
    }
    And so on?
    Neo1 Thanks again. The program runs great now. I appreciate you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-23-2012, 11:29 AM
  2. reversing bits in a truncated byte
    By droseman in forum C Programming
    Replies: 18
    Last Post: 03-08-2010, 09:56 AM
  3. Replies: 10
    Last Post: 04-28-2008, 05:45 PM
  4. Replies: 7
    Last Post: 09-02-2005, 04:40 AM
  5. truncated int?
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-08-2002, 08:23 PM