help me. program dont work

i tried but its not working. help me
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream.h>
#include<math.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>//is this neccesory
int main()
{
int add,sub,answer,done;
labelA:
cout<<"whats is the basic mathamatical operation you want to perform? \n";
cout<<"please answer with add-for addition sub-for subtraction.\n";//program prints this then become unresponsive.when the applications "close on exit" is unchecked from the properties.when it checked closes automatically. 
cin<<answer;
if (answer==add)
{
//program of addition
long double value1;
long double value2;
long double output1;

cout<<"Enter first number for addition. \n";
cin>>value1;
cout<<"Enter second number for addtion. \n";
cin>>value2;
output1=value1+value2;
cout<<"The sum of these two number: "<<output1;

goto labelA;

}
if (answer==sub)
{
//program of subtraction
long double sub1;
long double sub2;
long double ans2;

cout<<"Enter the first number for subtraction.\n";
cin>>sub1;
cout<<"Enter the second number for subtraction.\n";
cin>>sub2;
ans2=sub1-sub2;
cout<<"The sub of these two numbers are: "<< ans2;
goto labelA;
}
if (answer==done)
{cout<<"thank you for using this program.Good bye" <<endl;
getch();
}
return 0;
}
//any solutions will be greatly appreciated. 
Last edited on
@haiphonghponline

add, sub, done and answer probably should be set as strings, since you're wanting someone to type words and not integers.
You are asking the user to type text ("add" or "sub") when your program is trying to read an integer.

You are also trying to compare answer to add -- a variable that has not been given an initial value. (Your compiler should be complaining about that to you!)

By the way, your compiler is ancient. I learned C++ after the standard library was actually standardized, so I don't know how to get a string using the classic iostream.h methods (at least, not safely).

You do need to input a string, then compare it with string operators to the strings "add" and "sub".

The other option would be to ask the user to input 1 for add and 2 for subtract, or something like that. Then read the integer and see if answer == 1 or if answer == 2. Etc.

Hope this helps.
First off, I'd like to say, definitely a fan of the use of goto. I love it, although it's evil.

If you truly want the user to select add or subtract, you're going to want to make them strings. Same with answer. Also yeah, string.h(<string> works too) is necessary for using strings.

Lets say the user input's add:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (answer[0]=='a') && (answer[1]=='d') && (answer[2]=='d') {
long double value1;
long double value2;
long double output1;

cout<<"Enter first number for addition. \n";
cin>>value1;
cout<<"Enter second number for addition. \n";
cin>>value2;
output1=value1+value2;
cout<<"The sum of these two numbers are: "<<output1;

goto labelA;

}


It'll compare the first character in answer to the character 'a', then 'd', and 'd' again. Same thing goes for sub.

EDIT: If you want, add an else statement, incase they misspell add, the else will pick it up and could say "Invalid, try again". Then goto start; (Where start: will be somewhere above where it asks to input add or sub.)
Don't get too much into the habit of goto's though.
Didn't notice where LabelA: was.


Second off, you want to use the namespace std. so under your #includes type
using namespace std;

This way you won't have to type std::cout and std::cin over and over again, you can just type cout, and cin. (Since you're already using cout and cin, you need it)



PS: After you go to LabelA, ask if the user is done. And add a loop over everything so if he's not done, he can type add or sub again. Didn't notice where LabelA: was.

EDIT2: I'm 98% sure you don't need
1
2
#include<math.h>
#include<stdio.h> 


EDIT3: You never tell the user that "done" ends the program. Probably should.


Also, what happens if the user types something that isn't add/sub/done?
Last edited on
Topic archived. No new replies allowed.