I have tried to get this program to work with no luck. How do you properly use the nested selection structure? (sorry for the lack of braces, I tried getting rid of some for less confusion. Didn't think it would work but thought I might as well try :D). I'm just beginning in CPP.

//Created by Ted
//on 8/2/2011

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::transform;
using std::string;
using std::fixed;
using std::getline;

int main()
{
	//Declare variables
	string prompt1 = "";
	string prompt2 = "";
	double num1    = 0.0;
	double num2    = 0.0;
	double answer  = 0.0;

	//Prompt for info
	cout << "Hello, what's you name? ";
	getline(cin, prompt1);
	transform(prompt1.begin(), prompt1.end(), prompt1.begin(), toupper);
	cout << "What operation would you like to use? ";
	getline(cin, prompt2);
	transform(prompt2.begin(), prompt2.end(), prompt2.begin(), toupper);
	cout << "Ok "<< prompt1 <<", what are your two numbers? \n(seperate numbers with Enter button) \n";
	cin >> num1;
	cout << "And the second number? ";
	cin >> num2;

	//Calculate answer
	if(prompt2 == "ADD", "ADDITION", "+", "PLUS")
		answer = num1 + num2;
	else
		if(prompt2 == "SUBTRACTION","MINUS", "SUBTRACTION")
		answer = num1 - num2;
		else
			if(prompt2 == "divide", "division", "/", "multiply", "multiplication", "times", "X")
				cout << "Sorry, please enter another operation. " << endl;
			else
				cout << "Sorry, invalid operation." << endl;

	//Display answer
	cout <<"---------------- \nDear " << prompt1 <<", \nThe answer is "<< answer <<"." << endl;
return 0;
} //End of main function

(sorry for the lack of braces, I tried getting rid of some for less confusion. Didn't think it would work but thought I might as well try :D)

1) Don't apologize, fix it. Add braces and you won't be sorry.
2) Removing braces makes the code confusing.
3) So put them back and repost.

The operation:

if(prompt2 == "ADD", "ADDITION", "+", "PLUS")

will not do what you expect. It will always evaluate to true, regardless of the value of prompt2. This is because it uses the comma operator. The comma operator has the effect of evaluating each elements between commas and return the value of the last element. In this case, it will evaluate the equality, then it will evaluate each string literal "ADDITION" "+" and "PLUS", and it will return the value of "PLUS" which is a pointer to char (C-style string) which will not be NULL, and thus, will be "true" as a conditional.

If you want to test that prompt2 is equal to any of those strings, you need to do:

if(prompt2 == "ADD" ||
   prompt2 == "ADDITION" ||
   prompt2 == "+" ||
   prompt2 == "PLUS")

If you have C++0x support for std::initializer_list, you can also define a simple function like:

template <typename T>
bool is_in_set(const std::initializer_list<T>& L, const T& value) {
  return std::count(L.begin(), L.end(), value) != 0;
};

//then, you can have the conditional:
  if( is_in_set({"ADD","ADDITION","+","PLUS"}, prompt2) )

But that is a bit more advanced, better stick to the traditional way given previously.

Ok, I've got it fixed. Thanks guys. I've added the braces and the Or operator to the source code.

//Created by Ted on 8/2/2011
//Modified on 8/3/2011
//Posted to this site on 8/3/2011 :)

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <string>
#include <time.h>
#include <Windows.h>

using std::cout;
using std::cin;
using std::endl;
using std::transform;
using std::string;
using std::getline;

int main()
{
	//Declare variables
	string prompt1   = "";
	string prompt2   = "";
	double num1      = 0.0;
	double num2      = 0.0;
	double answer    = 0.0;
	bool   tf        = true;

	//Prompt for info
	cout << "This program claculates answer of two numbers \n----------\nHello, what's you name? ";
	getline(cin, prompt1);
	transform(prompt1.begin(), prompt1.end(), prompt1.begin(), toupper);
	cout << "Ok "<< prompt1 <<", what are your two numbers? \n(seperate numbers with Enter button) \n  ";
	cin >> num1;
	cout << "And the second number? \n  ";
	cin >> num2;
	cin.ignore(10, '\n');
	cout << "What operation would you like to use? \nEx. Addition, Subtraction... \n  ";
	getline(cin, prompt2);
	transform(prompt2.begin(), prompt2.end(), prompt2.begin(), toupper);

	//Calculate answer
	{
	if(prompt2 == "ADD" || prompt2 == "ADDITION" ||prompt2 == "+" ||prompt2 == "PLUS")
	{
		answer = num1 + num2;
	}
	else
		if(prompt2 == "SUBTRACT" || prompt2 == "MINUS" || prompt2 == "SUBTRACTION" || prompt2 == "-")
		{
		answer = num1 - num2;
		}
		else
			if(prompt2 == "DIVIDE" || prompt2 == "DIVISION" || prompt2 == "/" || prompt2 == "MULTIPLY" || prompt2 == "MULTIPLICATION" || prompt2 == "TIMES" || prompt2 == "X")
			{
				cout << "Sorry, please enter another operation. " << endl;
			}
			else
			{
				cout << "Sorry, invalid operation." << endl;
				tf = false;
			}
	}
	if(tf == false)
		cout << "---------------- \nSorry " <<prompt1<<", your problem could not be solved."<<endl;
	else
		//Display answer
		cout <<"---------------- \nDear " << prompt1 <<", \nThe answer is "<< answer <<"." << endl;
return 0;
} //End of main function
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.