Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: ToUpper

  1. #1
    New Coder
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question ToUpper

    I am trying to write a program, and I am using classes when doing it. My problem right now is I am using a class, and have gotten it setup to ask a question and take in a char. I was trying to use the toupper command to make the char uppercase. For some reason, it is not changing the character to uppercase. Please check out my code and see if you can tell what I am doing wrong. I am using the #include <cctype> header to get the toupper, but it is not changing it to uppercase. Please assist!

    Here is the code:
    Code:
    void bill::aBills()
    {
    	// Add Bills to the application.
    	system("cls");
    	cout << "This is the Add Bills screen.\n";
    
    	char choice;
    				
    	do  // Runs question until a correct response is made.
    	{		
    		cout << "\n\nPlease select a category that matches the bill:  "
    			<< "Medical (M), House (H), Credit (C):  ";
    		cin >> choice;
    
    		toupper(choice);
    		
    		switch(choice) // Gives user a choice of the categories to choose from.
    		{
    			case 'M':	bCategory = "Medical";
    						break;
    			case 'H':	bCategory = "House";
    						break;
    			case 'C':	bCategory = "Credit";
    						break;
    			default	:	cout << "Incorrect Selection, please try again!\n\n";
    						system("pause");
    						break;
    			}
    		}while ((choice != 'M') || (choice != 'H') || (choice != 'C'));
    		
    	setCategory(bCategory);
    	system("pause");
    }
    Then, when I changed the toupper to the switch function:

    Code:
    void bill::aBills()
    {
    	// Add Bills to the application.
    	system("cls");
    	cout << "This is the Add Bills screen.\n";
    
    	char choice;
    				
    	do  // Runs question until a correct response is made.
    	{		
    		cout << "\n\nPlease select a category that matches the bill:  "
    			<< "Medical (M), House (H), Credit (C):  ";
    		cin >> choice;
    
    		//toupper(choice);
    		
    		switch(toupper(choice)) // Gives user a choice of the categories to choose from.
    		{
    			case 'M':	bCategory = "Medical";
    						break;
    			case 'H':	bCategory = "House";
    						break;
    			case 'C':	bCategory = "Credit";
    						break;
    			default	:	cout << "Incorrect Selection, please try again!\n\n";
    						system("pause");
    						break;
    		}
    		
    		}while ((choice != 'M') || (choice != 'H') || (choice != 'C'));
    		
    	setCategory(bCategory);
    	system("pause");
    }
    it made it so that the character changed to uppercase into the switch, but when it ran through the while loop, it only sees it as its lowercase form. Please help.
    Last edited by karichie; 02-11-2014 at 03:55 AM.

  • #2
    New Coder
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Found an answer to this one, but have another question to follow it up. The answer was to change my code a bit to

    choice = toupper(choice);


    My issue now is no matter what the answer is, the while loop still continues to run. When the choice answer is 'M', it should break the loop, but instead, it continues running. Is my logic wrong on this?

  • #3
    New to the CF scene
    Join Date
    Feb 2014
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post
    The 'OR' operator will result in TRUE if any of the statement is true. Thus, you cannot get a FALSE. Use 'AND' operator instead. See if it solves the problem.

  • Users who have thanked sakai for this post:

    karichie (02-11-2014)

  • #4
    New Coder
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks!! I don't know what I was thinking. My logic was wrong when it came to this. It took me a few minutes of putting the statements together, and turning them into the logic statements to determine what I was doing wrong. Thanks for the help!! Sometimes its the most simplest of answers that cause the biggest issues.

  • #5
    New Coder
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts
    So can someone tell me how do we bump a post, and how do we close a post.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •