Need help!!

My program seems to work just fine except for one small thing. If I enter numbers other than what makes the condition right, it still displays "Inventory code" and "invalid output." Ideally, it would just display "invalid output" without a blank inventory code.

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

int main()
{
char choicefurniture;
char choicecolor ;
string furniturecode ="";
string furniturecolor="";
string furniture ="";

cout<< " Enter 1 for chair or 2 for table"<< endl;
cin >> choicefurniture;
cout << " Enter 1 for red, 2 for black or 3 for green"<<endl;
cin>>choicecolor;

if (choicefurniture=='1')
{ furniturecode="c47";
}
else if (choicefurniture=='2')
{furniturecode="t47";
}

if (choicecolor == '1')
{furniturecolor="42";
}
else if (choicecolor=='2')
{furniturecolor="25";
}
else if (choicecolor=='3')
{furniturecolor="30";
}
else
cout<<"invalid output"<<endl;

furniture= furniturecode + furniturecolor;
cout<< "Inventory code: "<< furniture <<endl;

system("pause");
return 0;
}

To begin, this really should be upgraded to switch statements, and the validation should be done with a while statement.

Nonetheless, if you want to keep your code similar to what you have, then you'll need a Boolean that is set to true by default, and is switched to false if anything fails. Then on your final statements, you just need to wrap it in an if statement checking that Boolean.

And please wrap you code in [code] tags when posting.
Topic archived. No new replies allowed.