DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Shana Guest

## Emergency!!! Help!!

I'm deperately trying not to fail out of my C++ class, I'm supposed to graduate
this semester. No matter what I do I can not get the toupper to work. The
program is a calculator, please see below. I know that I did not do it the
most efficient way, but it was the only way I could get it to work. Can you
but this is how I started. Any suggestions you have for the rest, is of course,
appreciated.
Thanks! Shana

#include <iostream.h>
#include <cmath>
#include <ctype.h>

int main()
{
char operation;
float num1;
float num2;
int toupper(int ch);

while (operation != 'Q')
{
cout << "Please enter the appropriate code for the function you would
like to perform." << endl << endl;
cout << " + : Addition" << endl;
cout << " - : Subtraction" << endl;
cout << " * : Multiplication" << endl;
cout << " / : Division" << endl;
cout << " E : Exponentiation" << endl;
cout << " S : Square Root" << endl;
cout << " Q : Quit" << endl << endl;

cin >> operation;
toupper (operation);

switch (operation)
{
case '+' : cout << "Please enter the first number." << endl;
cin >> num1;
cout << "Please enter the second number." << endl;
cin >> num2;
cout << "The answer equals " << answer << "." << endl;
break;
case '-' : cout << "Please enter the first number." << endl;
cin >> num1;
cout << "Please enter the second number." << endl;
cin >> num2;
cout << "The answer equals " << answer << "." << endl;
break;
case '*' : cout << "Please enter the first number." << endl;
cin >> num1;
cout << "Please enter the second number." << endl;
cin >> num2;
cout << "The answer equals " << answer << "." << endl;
break;
case '/' : cout << "Please enter the first number." << endl;
cin >> num1;
cout << "Please enter the second number." << endl;
cin >> num2;
if (num2 == 0)
cout << "The divisor can not equal 0." << endl;
else
cout << "The answer equals " << answer << "." << endl;
break;
case 'E' : cout << "Please enter the first number." << endl;
cin >> num1;
cout << "Please enter the second number." << endl;
cin >> num2;
cout << "The answer equals " << answer << "." << endl;
break;
case 'S' : cout << "Please enter the number." << endl;
cin >> num1;
cout << "The answer equals " << answer << "." << endl;
break;
default : cout << operation << " is not an option. Please try again."
<< endl;
break;
case 'Q' : cout << "Exiting!" << endl;
}

}

return 0;
}

2. Shana Guest

## Re: Emergency!!! Help!!

uuughhh, I'm an idiot. I figured it out, finally.
operation = toupper (operation);
but, if you want to offer me suggetions on streamlining my program, that
would be great. I know there is a way to do a loop instead of the way I did
it, but I just couldn't get it to work.
Thanks, Shana

>
>I'm deperately trying not to fail out of my C++ class, I'm supposed to graduate
>this semester. No matter what I do I can not get the toupper to work. The
>program is a calculator, please see below. I know that I did not do it the
>most efficient way, but it was the only way I could get it to work. Can

you
>but this is how I started. Any suggestions you have for the rest, is of

course,
>appreciated.
>Thanks! Shana
>
>
>#include <iostream.h>
>#include <cmath>
>#include <ctype.h>
>
>int main()
>{
> char operation;
> float num1;
> float num2;
> int toupper(int ch);
>
> while (operation != 'Q')
> {
> cout << "Please enter the appropriate code for the function you would
>like to perform." << endl << endl;
> cout << " + : Addition" << endl;
> cout << " - : Subtraction" << endl;
> cout << " * : Multiplication" << endl;
> cout << " / : Division" << endl;
> cout << " E : Exponentiation" << endl;
> cout << " S : Square Root" << endl;
> cout << " Q : Quit" << endl << endl;
>
> cin >> operation;
> toupper (operation);
>
> switch (operation)
> {
> case '+' : cout << "Please enter the first number." << endl;
> cin >> num1;
> cout << "Please enter the second number." << endl;
> cin >> num2;
> answer = num1 + num2;
> cout << "The answer equals " << answer << "." << endl;
> break;
> case '-' : cout << "Please enter the first number." << endl;
> cin >> num1;
> cout << "Please enter the second number." << endl;
> cin >> num2;
> answer = num1 - num2;
> cout << "The answer equals " << answer << "." << endl;
> break;
> case '*' : cout << "Please enter the first number." << endl;
> cin >> num1;
> cout << "Please enter the second number." << endl;
> cin >> num2;
> answer = num1 * num2;
> cout << "The answer equals " << answer << "." << endl;
> break;
> case '/' : cout << "Please enter the first number." << endl;
> cin >> num1;
> cout << "Please enter the second number." << endl;
> cin >> num2;
> if (num2 == 0)
> cout << "The divisor can not equal 0." << endl;
> else
> answer = num1 / num2;
> cout << "The answer equals " << answer << "." << endl;
> break;
> case 'E' : cout << "Please enter the first number." << endl;
> cin >> num1;
> cout << "Please enter the second number." << endl;
> cin >> num2;
> cout << "The answer equals " << answer << "." << endl;
> break;
> case 'S' : cout << "Please enter the number." << endl;
> cin >> num1;
> cout << "The answer equals " << answer << "." << endl;
> break;
> default : cout << operation << " is not an option. Please try again."
><< endl;
> break;
> case 'Q' : cout << "Exiting!" << endl;
> }
>
> }
>
> return 0;
>}
>

3. jonnin Guest

## Re: Emergency!!! Help!!

>
>I'm deperately trying not to fail out of my C++ class, I'm supposed to graduate
>this semester. No matter what I do I can not get the toupper to work. The
>program is a calculator, please see below. I know that I did not do it the
>most efficient way, but it was the only way I could get it to work. Can

you
>but this is how I started. Any suggestions you have for the rest, is of

course,
>appreciated.
>Thanks! Shana
>
>

Stick to no hs or all hs, mixing can really mess things up!!

>#include <iostream.h>
>#include <cmath>
>#include <ctype.h>
>
>int main()
>{
> char operation;
> float num1;
> float num2;
> int toupper(int ch);

you do not need the headers for toupper; they are in
the standard .h's, I think you have this now...

initialize operation to be anything but 'Q' (it could be currently, at random!)

other than this, its fine...

>
> while (operation != 'Q')
> {
> cout << "Please enter the appropriate code for the function you would
>like to perform." << endl << endl;
> cout << " + : Addition" << endl;
> cout << " - : Subtraction" << endl;
> cout << " * : Multiplication" << endl;
> cout << " / : Division" << endl;
> cout << " E : Exponentiation" << endl;
> cout << " S : Square Root" << endl;
> cout << " Q : Quit" << endl << endl;
>
> cin >> operation;
> toupper (operation);
>
> switch (operation)
> {
> case '+' : cout << "Please enter the first number." << endl;
> cin >> num1;
> cout << "Please enter the second number." << endl;
> cin >> num2;
> answer = num1 + num2;
> cout << "The answer equals " << answer << "." << endl;
> break;
> case '-' : cout << "Please enter the first number." << endl;
> cin >> num1;
> cout << "Please enter the second number." << endl;
> cin >> num2;
> answer = num1 - num2;
> cout << "The answer equals " << answer << "." << endl;
> break;
> case '*' : cout << "Please enter the first number." << endl;
> cin >> num1;
> cout << "Please enter the second number." << endl;
> cin >> num2;
> answer = num1 * num2;
> cout << "The answer equals " << answer << "." << endl;
> break;
> case '/' : cout << "Please enter the first number." << endl;
> cin >> num1;
> cout << "Please enter the second number." << endl;
> cin >> num2;
> if (num2 == 0)
> cout << "The divisor can not equal 0." << endl;
> else
> answer = num1 / num2;
> cout << "The answer equals " << answer << "." << endl;
> break;
> case 'E' : cout << "Please enter the first number." << endl;
> cin >> num1;
> cout << "Please enter the second number." << endl;
> cin >> num2;
> cout << "The answer equals " << answer << "." << endl;
> break;
> case 'S' : cout << "Please enter the number." << endl;
> cin >> num1;
> cout << "The answer equals " << answer << "." << endl;
> break;
> default : cout << operation << " is not an option. Please try again."
><< endl;
> break;
> case 'Q' : cout << "Exiting!" << endl;
> }
>
> }
>
> return 0;
>}
>

4. James Curran Guest

## Re: Emergency!!! Help!!

Well, the best way to stream line it, is to unify all the inputting of
numbers. And the first step there is to break a common beginner's
habit --- even if switch is useful in your program, you shouldn't use it for
everything. If we pull the check for "Q" out of the switch statement, the
whole thing becomes much easier:

if (operation == 'Q')
cout << "Exiting!" << endl;
else
{
if (operation == 'S')
{
cout << "Please enter the number." << endl;
cin >> num1;
}
else
{
cout << "Please enter the first number." << endl;
cin >> num1;
cout << "Please enter the second number." << endl;
cin >> num2;
switch (operation)
{
case '+' : answer = num1 + num2; break;
case '-' : answer = num1 - num2; break;
case '*' : answer = num1 * num2; break;
case '/' :
if (num2 == 0)
cout << "The divisor can not equal 0." <<
endl;
else
break;
case 'E' : answer = float(pow(num1, num2));
break;
}
} // if (=='S')
cout << "The answer equals " << answer << "." << endl;
} // if (=='Q')

--
Truth,
James Curran
www.NJTheater.com (Professional)
www.NovelTheory.com (Personal)

news:3cc50e4a\$1@10.1.10.29...
>
> I'm deperately trying not to fail out of my C++ class, I'm supposed to

> this semester. No matter what I do I can not get the toupper to work. The
> program is a calculator, please see below. I know that I did not do it the
> most efficient way, but it was the only way I could get it to work. Can

you
> but this is how I started. Any suggestions you have for the rest, is of

course,
> appreciated.
> Thanks! Shana
>
>
> #include <iostream.h>
> #include <cmath>
> #include <ctype.h>
>
> int main()
> {
> char operation;
> float num1;
> float num2;
> int toupper(int ch);
>
> while (operation != 'Q')
> {
> cout << "Please enter the appropriate code for the function you

would
> like to perform." << endl << endl;
> cout << " + : Addition" << endl;
> cout << " - : Subtraction" << endl;
> cout << " * : Multiplication" << endl;
> cout << " / : Division" << endl;
> cout << " E : Exponentiation" << endl;
> cout << " S : Square Root" << endl;
> cout << " Q : Quit" << endl << endl;
>
> cin >> operation;
> toupper (operation);
>
> switch (operation)
> {
> case '+' : cout << "Please enter the first number." << endl;
> cin >> num1;
> cout << "Please enter the second number." << endl;
> cin >> num2;
> answer = num1 + num2;
> cout << "The answer equals " << answer << "." << endl;
> break;
> case '-' : cout << "Please enter the first number." << endl;
> cin >> num1;
> cout << "Please enter the second number." << endl;
> cin >> num2;
> answer = num1 - num2;
> cout << "The answer equals " << answer << "." << endl;
> break;
> case '*' : cout << "Please enter the first number." << endl;
> cin >> num1;
> cout << "Please enter the second number." << endl;
> cin >> num2;
> answer = num1 * num2;
> cout << "The answer equals " << answer << "." << endl;
> break;
> case '/' : cout << "Please enter the first number." << endl;
> cin >> num1;
> cout << "Please enter the second number." << endl;
> cin >> num2;
> if (num2 == 0)
> cout << "The divisor can not equal 0." << endl;
> else
> answer = num1 / num2;
> cout << "The answer equals " << answer << "." << endl;
> break;
> case 'E' : cout << "Please enter the first number." << endl;
> cin >> num1;
> cout << "Please enter the second number." << endl;
> cin >> num2;
> cout << "The answer equals " << answer << "." << endl;
> break;
> case 'S' : cout << "Please enter the number." << endl;
> cin >> num1;
> cout << "The answer equals " << answer << "." << endl;
> break;
> default : cout << operation << " is not an option. Please try

again."
> << endl;
> break;
> case 'Q' : cout << "Exiting!" << endl;
> }
>
> }
>
> return 0;
> }
>

#### Posting Permissions

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

 FAQ Latest Articles Java .NET XML Database Enterprise