Issues with program

Working on a program that combines everything i learned so far. I'm getting weird answers though

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
 //ultimate MATH PROGRAM
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int a, b;
float c;
    cout << "two integers" <<endl << "first value" << endl;
    cin >> a;
    cout << "second value" << endl;
    cin >> b;
    cout << "is this correct" << a << b;
    char r;
    cin >> r;
        if (r == 'y'){
            cout << "please enter float/ decimal";
            cin >> c;
            cout << c;
            }
        else(r == 'n'){ //says "expected ';' before '{' token
            cout << "restart program";
            break;
            }

return 0;
}


I really have no idea what the problem is. I checked everywhere and didnt see a missing semicolon. Any ideas?

The else is part of the if block.
If you wanted to check for r=='n' you would use another if statement.
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
 //ultimate MATH PROGRAM
#include <iostream>
#include <string>
using namespace std;
int main ()
{
    int a, b;
    float c;
    cout << "two integers " <<endl << "first value " << endl;
    cin >> a;
    cout << "second value " << endl;
    cin >> b;
    cout << "is this correct " << a << b;
    char r;
    cin >> r;
    if (r == 'y')
    {
        cout << "please enter float/ decimal ";
        cin >> c;
        cout << c;
    }
    else
    {
        cout << " restart program";
    }

    return 0;
}

AAAAAAAAAHHHHHHHHHHHHH... how could i make that mistake. Anyway i decided to use another if and i also made the program better.

//ultimate MATH PROGRAM
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int a, b;
float c;
cout << "two integers" <<endl << "first value" << endl;
cin >> a;
cout << "second value" << endl;
cin >> b;
cout << "is this correct " << a << "\t" << b;
char r;
cin >> r;
if (r == 'y' || r == 'Y'){ //case sensitivity!!!!
cout << "please enter float/ decimal";
cin >> c;
cout << "your float" << c;
}
if (r == 'n'|| r == 'N'){
cout << "restart program";

}
else{cout << "GET OUT!! PROGRAM RESTARTED";} //a program with attitude (⌐■_■)


return 0;
}
You could also do an else if statement, although @CodeWriter's solution is better,
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
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int a, b;
float c;
    cout << "two integers" <<endl << "first value" << endl;
    cin >> a;
    cout << "second value" << endl;
    cin >> b;
    cout << "is this correct" << a << b;
    char r;
    cin >> r;
        if (r == 'y'){
            cout << "please enter float/ decimal";
            cin >> c;
            cout << c;
            }
        else if (r == 'n'){
            cout << "restart program";
            break;
            }

return 0;
}
Last edited on
now im having another problem

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
//ultimate MATH PROGRAM
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int a, b;
float c;
    cout << "two integers" <<endl << "first value" << endl;
    cin >> a;
    cout << "second value" << endl;
    cin >> b;
    cout << "is this correct     " << a << "\t" << b;
    char r;
    cin >> r;
        if (r == 'y' || r == 'Y'){
            cout << "please enter float/ decimal";
            cin >> c;
            cout << "your float" << c;


            }

        if (r == 'n'|| r == 'N'){
            cout << "restart program";

            }
                 else{cout << "GET OUT!! PROGRAM RESTARTED";} //this 
keeps getting activated. Instead of going into the next set of commands


        while(c>0){
            cout << --c <<",";
                    }
            if(c >! 0){
                cout << "enter two integers";
                cin >> a >> b;
                }

return 0;
}




See line 28
Last edited on
Topic archived. No new replies allowed.