c++ homework trouble

October 19, 2011 at 12:50:50
Specs: Windows 7
ok here is the first program I need to write.

Create a program using nested if statements allowing various mathematical operators to be utilized.


16. In this exercise, you are to create a program that adds, subtracts, multiplies, or divides two integers. The program will need to get a letter (A for addition, S for subtractions, M for multiplication, or D for division) and two integers from the user. If the user enters an invalid letter, the program should display an appropriate error message before the program ends. If the letter is A (or a), the program should calculate and display the sum of both integers. If the letter is S (or s), the program should display the difference between both integers. When calculating the difference, always subtract the smaller number from the larger one. If the letter is M (or m), the program should display the product of both integers. If the letter is D (or d), the program should divide both integers, always dividing the larger number by the smaller one.

Here is what I have written so far, it's not working exactly like it should though

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
char choice =' ';
int number1 = 0;
int number2 = 0;
double answer = 0.0;


cout<<"Enter A for addition, S for subtraction, D for division, M for multiply : ";

cin>>choice;

cout<<"Enter your first number : ";
cin>>number1;

cout<<"Enter your second number : ";
cin>>number2;

choice=toupper(choice);

if (choice == 'A')
answer=number1+number2;

else
if (choice == 'S')
if (number1>number2)
answer=number1-number2;
else
if (number2>number1)
answer=number2-number1;
//end if
else
if (choice == 'D')
if (number1>number2)
answer=number2/number1;
else
if (number2>number1)
answer=number1/number2;
//end if
else
if (choice == 'M')
answer=number1*number2;

//end if
//end if
//end if
//end if

cout<<endl;

{
cout<<fixed<<setprecision(2);
cout<<"Your answer is : "<<answer<<endl;
}


system("pause");
return 0;


}


See More: c++ homework trouble

Report •


#1
October 20, 2011 at 10:27:38
I ran your code though the formatter and got this. I'm pretty sure this isn't what you want to do, but this is how C++ will parse it.
  if (choice == 'A')
    answer=number1+number2;

  else
    if (choice == 'S')
      if (number1>number2)
        answer=number1-number2;
      else
        if (number2>number1)
          answer=number2-number1;
  //end if
        else
          if (choice == 'D')
            if (number1>number2)
              answer=number2/number1;
            else
              if (number2>number1)
                answer=number1/number2;
  //end if
              else
                if (choice == 'M')
                  answer=number1*number2;

Note how (choice == 'D') and (choice == 'M') are attached to the (number2>number1) tests, and not the preceding choice test. In the future, I suggest using curly brackets ({ }) to encapsulate your conditional execution clauses. Not only will they allow you to have more than one line per conditional, but they'll also let you better convey your intentions to the compiler / the TA doing the grading, and avoid these types of misunderstandings.

How To Ask Questions The Smart Way


Report •
Related Solutions


Ask Question