If Statement Trouble

Hey, im new to c++, so no doubt making stupied mistakes, im working on a program which could get me into a job learning c++ and making software from scratch, but i have to create a program beforehand, i am doing a program which give the user a choice wether to add and takeaway with an if statement, but when you enter a number to choose from, it just adds another line then closes, please help :)p.s its not a finished program, i havent started on choice two i have just put "two" so i know if it works or not.

#include<iostream>
using namespace std;
int firstnumber;
int secondnumber;
int main()
{
char answer[10];
cout<<"Please Choose an option"<<endl;
cout<<" * Addition - one"<<endl;
cout<<" * Subrtraction - two"<<endl;
cin>>answer;
cin.ignore();

if (answer == "one") {
cout<<"You have choosen addition, please enter your first number...";
cin>>firstnumber;
cout<<"please enter your second number...";
cin>>secondnumber;
cout<<"you answer is.."<<firstnumber + secondnumber <<endl;
}
else if (answer == "two") {
cout<<"Choice two";
}
cin.get();
}
You should add a total else at the end (so if the input is not "one" or "two") and have it print the "answer" variable, just to see what's wrong.

Anyway, when dealing with text, you should always go for std::string instead of char arrays. You're doing C++, not C!
I created your program.
i hope that i will help you,tell me if it helped or if you want anything else.

my skype is:magas1291

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
#include<string>
using namespace std;

int main()
{
float firstnumber;
float secondnumber;
string answer;


cout<<"Please Choose an option"<<endl;
cout<<" * Addition - one"<<endl;
cout<<" * Subrtraction - two"<<endl;
cin>>answer;
cout<<endl;


if (answer == "one") {
cout<<"You have choosen addition, please enter your first number...";
cin>>firstnumber;
cout<<"please enter your second number...";
cin>>secondnumber;
cout<<"you answer is.."<<firstnumber + secondnumber <<endl;
}
else if (answer == "two")
{
 cout<<"Choice two\n";
}
else
{
    cout<<"You choice nothing."<<endl;
}

}



Also put [code]
here your code and after [code] with this/ in the start netx to (c),
so your code will be looked better.
Topic archived. No new replies allowed.