Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,802 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,336 people online right now. Registration is fast and FREE... Join Now!




Where do I place the toupper function in this C++ program?

 
Reply to this topicStart new topic

Where do I place the toupper function in this C++ program?

ladylindsay
30 Sep, 2008 - 07:34 PM
Post #1

New D.I.C Head
*

Joined: 30 Sep, 2008
Posts: 5


My Contributions
The following program compiles, but if I enter a, s, m, or d, it says they're invalid. I want to use the toupper function so that the program will accept lowercase a, s, m, and d as well as uppercase A, S, M, and D. But I don't know where to put it. Please help me!


CODE

#include <iostream>
using namespace std;
int main ()
{
    //declare variables
    char operation = ' ';
    int num1 = 0;
    int num2 = 0;
    int result = 0;

    //calculate and display
    cout << "Enter operation (A/S/M/D): ";
    cin >> operation;
    if (operation != 'A', 'a' && operation != 'M', 'm' && operation != 'D', 'd' && operation != 'S', 's')
    {
        cout << "Invalid operator" << endl;
        return 0;
    }
    else
        cout << "Enter first number: ";
        cin >> num1;
        cout << "Enter second number: ";
        cin >> num2;
        switch (operation)
        {
        case 'A':
            result = num1 + num2;
            cout << "Result: " << result << endl;
            break;
        case 'M':
            result = num1 * num2;
            cout << "Result: " << result << endl;
            break;
        case 'S':
            if (num2 > num1)
            {
                int temp = 0;
                temp = num2;
                num2 = num1;
                num1 = temp;
                result = num1 - num2;
                    cout << "Result: " << result << endl;
            }
            else
            {
                result = num1 - num2;
                cout << "Result: " << result << endl;
            }    //end if
            break;
        case 'D':
            if (num2 > num1)
            {
                int temp = 0;
                temp = num2;
                num2 = num1;
                num1 = temp;
                result = num1 / num2;
                cout << "Result: " << result << endl;
            }
            else
            {
                result = num1 / num2;
                cout << "Result: " << result << endl;
            }    //end if
            break;
        }    //end switch
    //end if
            return 0;
        }


This post has been edited by ladylindsay: 30 Sep, 2008 - 07:44 PM
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Where Do I Place The Toupper Function In This C++ Program?
30 Sep, 2008 - 07:37 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,476



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
cpp

if (toupper(operation) != 'A' && toupper(operation) != 'M' && toupper(operation) != 'D' && toupper(operation) != 'S')
{
cout << "Invalid operator" << endl;
return 0;
}

User is offlineProfile CardPM
+Quote Post

Tiron
RE: Where Do I Place The Toupper Function In This C++ Program?
3 Oct, 2008 - 10:58 PM
Post #3

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 8



Thanked: 1 times
My Contributions
I would put it right above
CODE
switch(operation)
{...

To avoid changing ealier code
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 02:03PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month