Hi there, I'm new to the site and new to learning C++.

The program I am writing is for a C++ college class, and I'm just having a little trouble on if I am formatting the if statements I am using correctly. I'll give a breakdown of the assignment. I have to get an input of either A(ddition), S(ubtraction), D(ivide), or M(ultiply) and then have the user enter two integers then perform the expressed operation based on what letter they entered. If the user inputs a letter other than a, s, d, or m, it gives off an error message.

So far my program works if I take out anything but the first nested if statement.

//Ch6AppE08.cpp
//Displays the answer to arithmetic problems
//Created/revised by Greg Schader on 6/22/2009

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{	

	//Define variables
	char function = ' ';
	int firstNum = 0;
	int secondNum = 0;
	int answer = 0;

	//Enter variables and perform operation
	cout << "Enter the letter of the operation you wish to perform: ";
	cin >> function;

	if (toupper(function) != 'A' && toupper(function) != 'S' && toupper(function) != 'D' && toupper(function) != 'M')
		cout << "That letter is not valid." << endl;
	else
	{
		if (toupper(function) == 'A')
			cout << "Enter the first number of the operation: ";
			cin >> firstNum;
			cout << "Enter the second number of the operation: ";
			cin >> secondNum;

			answer = firstNum + secondNum;

			cout << "Answer of operation is: " << answer << endl;
		else
		
			if (toupper(function) == 'S')
			cout << "Enter the first number of the operation: ";
			cin >> firstNum;
			cout << "Enter the second number of the operation: ";
			cin >> secondNum;

			answer = firstNum - secondNum;

			cout << "Answer of operation is: " << answer << endl;

			else
			
				if (toupper(function) == 'M')
			cout << "Enter the first number of the operation: ";
			cin >> firstNum;
			cout << "Enter the second number of the operation: ";
			cin >> secondNum;

			answer = firstNum * secondNum;

			cout << "Answer of operation is: " << answer << endl;

				else
				
					if (toupper(function) == 'D'){
			cout << "Enter the first number of the operation: ";
			cin >> firstNum;
			cout << "Enter the second number of the operation: ";
			cin >> secondNum;

			answer = firstNum / secondNum;

			cout << "Answer of operation is: " << answer << endl;
					}


	}
	

    return 0;
}   //end of main function

Don't forget the braces around your inner if and else statements, take a look in your book on why to do this (if it explains it)

Even when I add braces around my inner if else statements, I still get an illegal else without matching if statement.

If I take out the else statements completely, the program will compile, but when I run it, using A as my operation indicator, it runs through the normal steps but never hits the end of program press any key, and the other letters never return the responses from the couts.

When you fix something don't just say it didn't work and wait for another answer, post the updated code

Sorry, didn't even think of that.

//Ch6AppE08.cpp
//Displays the answer to arithmetic problems
//Created/revised by Greg Schader on 6/22/2009

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{	

	//Define variables
	char function = ' ';
	int firstNum = 0;
	int secondNum = 0;
	int answer = 0;

	//Enter variables and perform operation
	cout << "Enter the letter of the operation you wish to perform: ";
	cin >> function;

	if (toupper(function) != 'A' && toupper(function) != 'S' && toupper(function) != 'D' && toupper(function) != 'M')
		cout << "That letter is not valid." << endl;
	else
	{
		if (toupper(function) == 'A'){
			cout << "Enter the first number of the operation: ";
			cin >> firstNum;
			cout << "Enter the second number of the operation: ";
			cin >> secondNum;

			answer = firstNum + secondNum;

			cout << "Answer of operation is: " << answer << endl;
	
		
		else 
		}
		if (toupper(function) == 'S'){
			cout << "Enter the first number of the operation: ";
			cin >> firstNum;
			cout << "Enter the second number of the operation: ";
			cin >> secondNum;

			answer = firstNum - secondNum;

			cout << "Answer of operation is: " << answer << endl;
		
		else 
		}
		if (toupper(function) == 'M'){
			cout << "Enter the first number of the operation: ";
			cin >> firstNum;
			cout << "Enter the second number of the operation: ";
			cin >> secondNum;

			answer = firstNum * secondNum;

			cout << "Answer of operation is: " << answer << endl;
			
		else 
		}
			if (toupper(function) == 'D'){
			cout << "Enter the first number of the operation: ";
			cin >> firstNum;
			cout << "Enter the second number of the operation: ";
			cin >> secondNum;

			answer = firstNum / secondNum;

			cout << "Answer of operation is: " << answer << endl;
		}
	}

    return 0;
}   //end of main function

Not sure if the braces went in the right places. They were placed based on the very simple examples used in the book. It also does not give much explanation on why the braces are useful or necessary.

Ya can't just put braces in random spots and expect it to work :)

//Ch6AppE08.cpp
//Displays the answer to arithmetic problems
//Created/revised by Greg Schader on 6/22/2009

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{ 

  //Define variables
  char function = ' ';
  int firstNum = 0;
  int secondNum = 0;
  int answer = 0;

  //Enter variables and perform operation
  cout << "Enter the letter of the operation you wish to perform: ";
  cin >> function;

  if (toupper(function) != 'A' && toupper(function) != 'S' && toupper(function) != 'D' && toupper(function) != 'M') {
    cout << "That letter is not valid." << endl;
  } else {
    if (toupper(function) == 'A') {
      cout << "Enter the first number of the operation: ";
      cin >> firstNum;
      cout << "Enter the second number of the operation: ";
      cin >> secondNum;

      answer = firstNum + secondNum;

      cout << "Answer of operation is: " << answer << endl;
    } else if (toupper(function) == 'S') {
      cout << "Enter the first number of the operation: ";
      cin >> firstNum;
      cout << "Enter the second number of the operation: ";
      cin >> secondNum;

      answer = firstNum - secondNum;

      cout << "Answer of operation is: " << answer << endl;
    } else if (toupper(function) == 'M') {
      cout << "Enter the first number of the operation: ";
      cin >> firstNum;
      cout << "Enter the second number of the operation: ";
      cin >> secondNum;

      answer = firstNum * secondNum;

      cout << "Answer of operation is: " << answer << endl;
    } else if (toupper(function) == 'D') {
      cout << "Enter the first number of the operation: ";
      cin >> firstNum;
      cout << "Enter the second number of the operation: ";
      cin >> secondNum;

      answer = firstNum / secondNum;

      cout << "Answer of operation is: " << answer << endl;
    }
  }

    return 0;
}   //end of main function

As a side note, do you notice that you have a lot of repetitive code? That usually means it can be taken out and put in one place. Everything inside the if/else blocks does exactly the same thing except the answer = part. So take out the cout/cin, drop it above and then use a switch statement. Take a look in your book for the switch statement syntax.

As a side note, do you notice that you have a lot of repetitive code? That usually means it can be taken out and put in one place. Everything inside the if/else blocks does exactly the same thing except the answer = part. So take out the cout/cin, drop it above and then use a switch statement. Take a look in your book for the switch statement syntax.

See ,in my book it has a cascading example using if/else/if/else/if/else but uses no braces to separate the code. Also shows the same code using else if form with no braces. So that's probably where the random tossing of braces where it looks like they should go there came from.

And excellent pointing to the switch statement. I would have stuck with the repetitive if else waterfall since I like to see everything, its a bad habit.

For my function of division and subtraction, I would need to make the smaller number subtract from the larger and the larger number by the smaller, by using a swap right?

why would you have to swap for subtraction? Also, you're not checking to make sure that you're not dividing by zero in your division section

why would you have to swap for subtraction? Also, you're not checking to make sure that you're not dividing by zero in your division section

The instructions say we need to make firstNum the largest number for subtraction.

Oh, I hadn't thought of that. Would I just put that as

else if (toupper(function) == 'D') 
	{
      cout << "Enter the first number of the operation: ";
      cin >> firstNum;
      cout << "Enter the second number of the operation: ";
      cin >> secondNum;
	  if (firstNum == 0 || secondNum == 0)
		  cout <<"Error: 0 is a non-divisible number. " << endl;
	  else
	  {

      answer = firstNum / secondNum;

      cout << "Answer of operation is: " << answer << endl;
    }
	}
  }

Ok, then yeah, follow the instructions. And you only have to worry about dividing BY zero, it doesn't matter if numerator is zero, only check the denominator (secondNum in this case)

Also, for the switch statement would I format it as

1) enter operation letter
2)enter first number
3)enter second number
4)use the case of operation letter entry to do something like

switch (function)
{
case  'A':
             answer = firstNum + secondNum;
             cout << "Answer to operation is: " << answer <<endl;
             break;

?

Well it'd be enter operation, enter first number, enter second number -> switch(operation) { calculate answer } -> output answer -> done

EDIT: This is my 1000th post, wooo!

Congrats on your 1000th post! Glad I could help : )


Here's is my updated code. When I divide, I need to make the smaller number always divide by the larger number, but the answer always comes up as 0, no matter what numbers I entered. Do I need to rearrange which number are being swapped?

//Ch6AppE08.cpp
//Displays the answer to arithmetic problems
//Created/revised by Greg Schader on 6/22/2009

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{ 

  //Define variables
  char function = ' ';
  int firstNum = 0;
  int secondNum = 0;
  int answer = 0;

  //Enter variables and perform operation
  cout << "Enter the letter of the operation you wish to perform: ";
  cin >> function;
  cout << "Enter the first number of the operation: ";
  cin >> firstNum;
  cout << "Enter the second number of the operation: ";
  cin >> secondNum;
  function = toupper(function);


  switch (function)
  {
  case 'A':
	  answer = firstNum + secondNum;
	  cout << "Answer to operation is: " << answer<< endl;
	  break;
  case 'S':
	  if (firstNum < secondNum)
	  {
		  int temp = 0;
		  temp = firstNum;
		  firstNum = secondNum;
		  secondNum = temp;
	  }
      answer = firstNum - secondNum;
      cout << "Answer of operation is: " << answer << endl;
	  break;
  case 'M':
	  answer = firstNum * secondNum;
	  cout << "Answer to operation is: " << answer << endl;
	  break;
  case 'D':
	  if (secondNum == 0)
		  cout <<"Error: 0 is a non-divisible number. " << endl;
	  else
	  {
		  if (firstNum > secondNum)
		  {
			int temp = 0;
			temp = firstNum;
			firstNum = secondNum;
			secondNum = temp;
		  }
      answer = firstNum / secondNum;

      cout << "Answer of operation is: " << answer << endl;
    }
	  break;
  default:
	  cout << "Input error: please enter a correct operation letter." << endl;
  }

    return 0;
}   //end of main function

Well now...who would have thought.

case 'D':
	  if (secondNum == 0)
		  cout <<"Error: 0 is a non-divisible number. " << endl;
	  else
	  {
		  if (firstNum > secondNum)
		  {
			int temp = 0;
			temp = firstNum;
			firstNum = secondNum;
			secondNum = temp;
		  }
      answer = secondNum / firstNum;

      cout << "Answer of operation is: " << answer << endl;
    }
	  break;

If I put the operation function in the right order...suddenly it works. My fault, lol.

Dear ShawnCplus,

Thank you ever so much for your help. This site is a wonderful asset for all people trying to learn how to program and your help was invaluable. I really appreciate the time you put into helping me with this, and I'm sorry my rookie self was so difficult and blindsighted.

Thanks,

Cloneminds (Greg)

+Rep for you, and problem solved! = ]

No problem, just study-up and you'll have the concepts down in no time at all

I liked your program. Dont get offended or anything (just wanted to make sure, because people seem to do that sometimes.) I am also new at programming and I wanted to say thank you because I didnt know about the toupper command until I read your thread. Anyways, I wanted to see if I could revise your script into a smaller one. However it is still your program that is the backbone of it (I didnt really change anything, I just condensed it, I dont know if this is frowned upon in this community (what i have done) but if it is I'm sorry.) Here is what I came up with:

//Created by Greg Schader revised by Valley

#include <iostream> 
#include <string>
using namespace std;

int main()
{   
char function = ' ';  
int firstNum = 0;  
int secondNum = 0;  
int answer = 0;   
string show= "Answer of operation is: \n";

cout << "\n\nEnter the letter of the operation you wish to perform (a/s/m/d): ";  
cin >> function;   
cout << "\nEnter the first number of the operation: "<<endl;      
cin >> firstNum;      
cout << "\nEnter the second number of the operation: "<<endl;      
cin >> secondNum;

switch (toupper(function))
{
       case 'A':
       answer = firstNum + secondNum;       
       cout <<show<< answer;
       break;
       case 'S':
       answer = firstNum - secondNum;    
       cout <<show<< answer;
       break;
       case 'M':
       answer = firstNum * secondNum;   
       cout <<show<< answer;
       break;
       case 'D':
       answer = firstNum / secondNum;       
       if (firstNum==0)
       cout<<"Inappropriate Opperation!\n";
       else cout <<show<< answer;
       break;
       default:
               cout<<"Illegal Choice\n";
}
               
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.