Help with my code please

Edit: I am trying to make a simple tool that will add/subtract/multiply/divide and i cant seem to get it to work. I am noob!

Heres my code:
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// stringstreams
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
int n;
cout << "Add:1 Subtract:2 Multiply:3 Divide:4 > ";
cin >> n;


while (n==1) {
cout << n << ", ";
--n;
}

string mystr;
float first=0;
int plus=0;

cout << "Enter First Number: ";
getline (cin,mystr);
stringstream(mystr) >> first;
cout << "Plus: ";
getline (cin,mystr);
stringstream(mystr) >> plus;
cout << "Total: " << first+plus <<
}

while (n==2) {
cout << n << ", ";
--n;
}

string mystr;
float number=0;
int minus=0;

cout << "Enter Number: ";
getline (cin,mystr);
stringstream(mystr) >> number;
cout << "Minus: ";
getline (cin,mystr);
stringstream(mystr) >> minus;
cout << "Heres your total: " << number-minus <<
endl;

while (n==3) {
cout << n << ", ";
--n;
}

string mystr;
float price=0;
int quantity=0;

cout << "Enter price: ";
getline (cin,mystr);
stringstream(mystr) >> price;
cout << "Enter quantity: ";
getline (cin,mystr);
stringstream(mystr) >> quantity;
cout << "Total price: " << price*quantity <<
endl;

while (n==4) {
cout << n << ", ";
--n;
}

string mystr;
float what=0;
int divided=0;

cout << "What: ";
getline (cin,mystr);
stringstream(mystr) >> what;
cout << "Divided by: ";
getline (cin,mystr);
stringstream(mystr) >> divided;
cout << "Is: " << what/divided <<
endl;

cin.get();
cin.get();

return 0;
}
Last edited on
for once, there seems to be problem with the brackets.
27. stringstream(mystr) >> plus;
28. cout << "Total: " << first+plus <<
29. }

your while loops of --n are not clear. why don't you use a simple switch statement?
Line 28: the line is unfinished.
Line 29: that bracket shouldn't exist.

Lines 13, 31, 49 and 67: those while loops do not make any sense. I think what you want to do is execute some code only on a certain condition. In that case you must use "if":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (n==1)
{
    string mystr;
    float first=0;
    int plus=0;

    cout << "Enter First Number: ";
    getline (cin,mystr);
    stringstream(mystr) >> first;
    cout << "Plus: ";
    getline (cin,mystr);
    stringstream(mystr) >> plus;
    cout << "Total: " << first+plus << endl;
}

Ok I got it to run but it is using random math i.e addition, subtraction, multiplication, and division all at the same time. you never know what its going to be.
Thank you all for the help!
Heres my code now
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// stringstreams
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
int n;
cout << "Addition: 1 Subtraction: 2 Multiplication: 3 Division: 4 ";
cin >> n;


if (n==1)
{
    string mystr;
    float first=0;
    int plus=0;

    cout << "Enter First Number: ";
    getline (cin,mystr);
    stringstream(mystr) >> first;
    cout << "Plus: ";
    getline (cin,mystr);
    stringstream(mystr) >> plus;
    cout << "Total: " << first+plus << endl;
}

if (n==1)
{
    string mystr;
    float first=0;
    int minus=0;

    cout << "Enter First Number: ";
    getline (cin,mystr);
    stringstream(mystr) >> first;
    cout << "Minus: ";
    getline (cin,mystr);
    stringstream(mystr) >> minus;
    cout << "Total: " << first-minus << endl;
}

if (n==1)
{
    string mystr;
    float first=0;
    int times=0;

    cout << "Enter First Number: ";
    getline (cin,mystr);
    stringstream(mystr) >> first;
    cout << "Times: ";
    getline (cin,mystr);
    stringstream(mystr) >> times;
    cout << "Total: " << first*times << endl;
}

if (n==1)
{
    string mystr;
    float first=0;
    int divided=0;

    cout << "Enter First Number: ";
    getline (cin,mystr);
    stringstream(mystr) >> first;
    cout << "Divided By: ";
    getline (cin,mystr);
    stringstream(mystr) >> divided;
    cout << "Total: " << first/divided << endl;
}

return 0;
}
Last edited on
for addition you should have if (n==1)
for subtraction you should have if (n==2)
for multiplication you should have if (n==3)
for division you should have if (n==4)

it'll be better if you use "else if" statements or switch / case statements.
Oh wow... now i feel stupid haha!
Thank you!
Edit: Now it only says "Enter First Number: Plus:" all 1 line and when i try to type something and click enter it closes.
Last edited on
Some psuedo code:

1
2
3
4
//get a number
//get an operator
//get a number
//calc & print the answer 


This shows how you identify what goes in a function, thereby reducing your code.

HTH
why are you using strings?
try this instead:
1
2
3
4
5
6
7
8
    float first=0;
    float plus=0;

    cout << "Enter First Number: ";
    cin >> first;
    cout << "Plus: ";
    cin >> plus;
    cout << "Total: " << first+plus << endl;
Awesome thanks everyone, It works perfectly now, but how could i get it to rerun it self after it solves an equation so i could keep typing new equations instead of it just ending after it displays the first Answer.

For instance:
Addition: 1 Subtraction: 2 Multiplication: 3 Division: 4 1
Enter First Number: 3
Plus: 4
Total: 7
Addition: 1 Subtraction: 2 Multiplication: 3 Division: 4

and so on.
Edit: here is my code now:
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
int n;
cout << "Addition: 1 Subtraction: 2 Multiplication: 3 Division: 4 ";
cin >> n;


if (n==1)
{
    float first=0;
    float plus=0;

    cout << "Enter First Number: ";
    cin >> first;
    cout << "Plus: ";
    cin >> plus;
    cout << "Total: " << first+plus << endl;
}

if (n==2)
{
    float first=0;
    float minus=0;

    cout << "Enter First Number: ";
    cin >> first;
    cout << "Minus: ";
    cin >> minus;
    cout << "Total: " << first-minus << endl;
}

if (n==3)
{
    float first=0;
    float times=0;

    cout << "Enter First Number: ";
    cin >> first;
    cout << "Times: ";
    cin >> times;
    cout << "Total: " << first*times << endl;
}


if (n==4)
{
    float first=0;
    float divided=0;

    cout << "Enter First Number: ";
    cin >> first;
    cout << "Divided by: ";
    cin >> divided;
    cout << "Total: " << first/divided << endl;

}

 
cin.get();
	cin.get();
return 0;


}
Last edited on
Yeah try using a do...while loop checking whether a variable say bExit is true, before exiting.

You can also use the system(); function passing "CLS" as a parameter(with windows) at the end of the loop, to clear the screen.

eg:
1
2
3
4
5
6
7
8
9
10
bool bExit=false;
char strExit;

do
{
    ...
    cout<<"Do u weanna quit?";
    cin>>strExit;
    if(strExit=='y') bExit=true;
}while(bExit!=true);


HTH,
Aceix.
Try using a do...while loop.

Aceix.
you can also try this..

1
2
3
4
5
6
7
8
9
10
11
int n;
do {
  cout << "Addition: 1 Subtraction: 2 Multiplication: 3 Division: 4 Exit: 5";
  cin >> n;
  if (n==1) {
      ....
  }
  ....
  ....
} while (n != 5);


I would prefer a while loop with a switch inside:

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
bool Quit = false;
int Option = 1;

while (!Quit) {

//get input

//switch statement here
switch (Option) {
    case  1:
        //addition stuff here
    break;

    case 2:
         //Subtraction
    break;

    case 3:
         //Multiplication
    break;

    case 4:
         //Division
    break;

    case 5:  //user wants to quit
         Quit = true
    break;

     default:  //bad input
          std::cout << "Bad Input try again" << std::endl;
} //end of switch


}//end of while loop 


Notice with some forms of the do loop, the conditional appears twice.

I hope this works for you.
Thanks all!
Topic archived. No new replies allowed.