1 Replies - 235 Views - Last Post: 30 January 2011 - 04:59 PM Rate Topic: -----

#1 Tivor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-January 11

Help with else?

Posted 30 January 2011 - 04:50 PM

I had a question about something that I think is pretty stupid. I decided to try to make a calculator using everything I had learned so far from Accelerated C++ and C++ Primer Plus. (Just got started in both!) I got the calculator part done, but when I tried to use "else" to indicate if you had typed in something wrong it... goes ahead and it displays at the end even if you do everything correctly.

Here is the code. The part I am talking about is in lines 71-74. What I learned about the if and else statements is from the internet, and not from the books, which is probably why I don't know the answer to this. Also, if you see something else that is a bad habit or something I could improve on, please mention that too.

#include "stdafx.h"											// Because of Visual C++ 2010. Disreguard is using a different IDE.
#include <iostream>
#include <cstring>

// January 30, 2011
// Calculator

int main(void)
{
	std::cout << "Please enter what you want to do. (Add, Subtract, Multiply, or Divide)" << std::endl;
	std::cout << "Choose one of those listed above: ";
	char type [256];
	std::cin >> type;
	std::cout << std::endl;
	if (strcmp(type, "Add") == 0)															// Adding
		{
		 std::cout << "Please enter your first value, press ENTER," << std::endl;
		 std::cout << "then enter the second and press enter." << std::endl;
		 unsigned add;
		 std::cin >> add;
		 unsigned add1;
		 std::cout << "+" << std::endl;
		 std::cin >> add1;
		 unsigned answerAdd;
		 answerAdd = add + add1;
		 std::cout << std::endl;
		 std::cout << "Your answer is " << answerAdd << ".";
		}
	if (strcmp(type, "Subtract") == 0)													// Subtracting
		{
		 std::cout << "Please enter your first value, press ENTER," << std::endl;
		 std::cout << "then enter the second and press enter." << std::endl;
		 unsigned subtract;
		 std::cin >> subtract;
		 std::cout << "-" << std::endl;
		 unsigned subtract1;
		 std::cin >> subtract1;
		 unsigned answerSubtract;
		 answerSubtract = subtract - subtract1;
		 std::cout << std::endl;
		 std::cout << "Your answer is " << answerSubtract <<".";
		}
	if (strcmp(type, "Muliply") == 0)														// Multiplying
		{
		 std::cout << "Please enter your first value, press ENTER," << std::endl;
		 std::cout << "then enter the second and press enter." << std::endl;
		 unsigned multiply;
		 std::cin >> multiply;
		 std::cout << "*" << std::endl;
		 unsigned multiply1;
		 std::cin >> multiply1;
		 unsigned answerMultiply;
		 answerMultiply = multiply * multiply1;
		 std::cout << std::endl;
		 std::cout << "Your answer is " << answerMultiply <<".";
		}
	if (strcmp(type, "Divide") == 0)															// Dividing
		{
		 std::cout << "Please enter your first value, press ENTER," << std::endl;
		 std::cout << "then enter the second value and press enter." << std::endl;
		 unsigned divide;
		 std::cin >> divide;
		 std::cout << "/" << std::endl;
		 unsigned divide1;
		 std::cin >> divide1;
		 unsigned answerDivide;
		 answerDivide = divide / divide1;
		 std::cout << std::endl;
		 std::cout << "Your answer is " << answerDivide <<".";
	    }
	else;																					// If a wrong value is typed in. Subtrct instead of Subtract, for instance. 
		{
		 std::cout << "Invalid. Typo?";
		}
	std::cin.ignore();
	std::cin.get();
	return 0;
}	 


Is This A Good Question/Topic? 0
  • +

Replies To: Help with else?

#2 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: Help with else?

Posted 30 January 2011 - 04:59 PM

using an else will only compare it to the one if above it being if( strcmp(type, "Divide") == 0) if you wish to do what you are you should use a series of else-if statements.

// Psuedo Code //
if( A ) {
  // ..code
}
else if (B)/> {
  // ..code
}
else if (C) {
  // ..code
}
else {
  // ..code
}

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1