Can you get this code to work properly?

Feb 23, 2014 at 4:21pm
I can probably get figure this out eventually but I wanted to make my first post on this forum to see how useful it is. Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    int num;
    cin >>num;
if (num == 1){
        cout<<"1 works";
        }
        
        else (num == 2);{
             cout<<"2 works"; endl;
             break;
             }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Feb 23, 2014 at 4:38pm
Update: I now managed to make the code execute. The only problem now is that if I input "1", the whole code executes instead of just the body inside of the if statement.

I'm also try to get the code to loop as many times as the user wants.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    int num;
    cin >>num;
if (num == 1){
        cout<<"1 works";
        }
        
        else (num == 2);{
             cout<<"2 works"<<endl;
             }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Last edited on Feb 23, 2014 at 4:46pm
Feb 23, 2014 at 4:49pm
closed account (zybCM4Gy)
Hmmm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main()
{
    int num;
    cin >>num;

    if (num == 1)
    {
        cout<<"1 works";
    }
    else if(num == 2)
    {
        cout<<"2 works"<<endl;
    }

    return 0;
}


Line 14. You have terminated the else far too early and it's also an incorrect usage of else. You need "else if".

Also. Using system("PAUSE"); == bad.

Also look into good practice regarding indentation.
Feb 23, 2014 at 4:55pm
Basically, your code is equivalent to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
    int num;
    cin >>num;
    if (num == 1)
    {
        cout<<"1 works";
    }
    else
        (num == 2); // Statement that does nothing

    { // This block has nothing to do with the 'else' above
        cout<<"2 works"<<endl;
    }
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

So change the else to a else if and remove the semicolon after that line and you'll have what you what.

As far as the system("pause") thing goes, this might be a good read:
http://www.cplusplus.com/articles/j3wTURfi/
Feb 23, 2014 at 5:07pm
It works thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    int num;
    cin >>num;
if (num == 1){
        cout<<"1 works. ";
        }
        
        else if (num == 2){
             cout<<"2 works. "<<endl;
             }
    

    return main();
}
Feb 23, 2014 at 5:15pm
closed account (zybCM4Gy)
NO NO NO NO!!!!

Do not return main!!!!
Feb 23, 2014 at 5:22pm
By the way, it's forbidden to call the main function from within your program.
If you need to loop back to the beginning, use a loop:
1
2
3
4
5
6
7
8
9
10
int main()
{
    while (true) // Loop forever
    {
        int num;
        cin >> num;
        // ...
    }
    return 0;
}
or maybe something like
1
2
3
4
5
6
7
8
9
10
int main()
{
    int num;
    do
    {
        cin >> num;
        // ...
    } while (num != 0); // Loop until user enters 0
    return 0;
}
Feb 23, 2014 at 5:28pm
closed account (zybCM4Gy)
or even...

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    start:
    int num = 0; 

   cin >> num;

    if( num == 0) /*then*/ return 0; //exit

   goto start;
   return 0;
}
Last edited on Feb 23, 2014 at 5:29pm
Topic archived. No new replies allowed.