Total C++ n00b

Hey everybody, Total newbie here with c++. Just kinda messing around with the language to get a grasp on it all. And this is a really really simple process and I'm probably missing some fundamental programming rule that I'm unaware of, but i'm a little curious as to why this code won't work. Any tips or pointers are greatly appreciated!

edit: whoops I didn't really tell you guys what's not working. I would like the numbers to works as a multiple condition, meaning if you enter any of the incorrectly, it will either fail or go to a user specified message about failure. Preferably, I'd like it to reset if any numbers are entered incorrectly. If that makes sense? However, I don't want it to fail until the last number, meaning if you enter the first "4" incorrectly, but did the rest, it'd continue forward and fail after you have entered all numbers.



#include <iostream>

using namespace std;

int main()
{
int numbers4, numbers8, numbers15, numbers16, numbers23, numbers42;
numbers4=0;
numbers8=0;
numbers15=0;
numbers16=0;
numbers23=0;
numbers42=0;
cout << "Please enter the numbers...\n";
cin >> numbers4;
if (numbers4 ==4); {
cin >> numbers8;
if (numbers8 ==8); {
cin >> numbers15;
if (numbers15 ==15); {
cin >> numbers16;
if (numbers16 ==16); {
cin >> numbers23;
if (numbers23 ==23); {
cin >> numbers42;
if (numbers42 ==42); {
cout << "Good Job!!";
}
}
}
}
}
}
}
return 0;
}
Last edited on
I would use the while loop to validate the user input.
This link provides a tutorial about loops and control flow:
http://www.cplusplus.com/doc/tutorial/control/

Just to give you an idea:

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
#include <iostream>

int main()
{
    // Declare and initialize the variables.
    double num1{ 0 }, num2{ 0 };

    // Ask the user for their input.
    std::cout << "Enter two positive numbers and I will add them.\n";
    std::cout << "Number 1: ";
    std::cin >> num1;

    // Use the while loop to validate the user input is valid.
    // Ask the user to try again with their input.
    while (num1 < 0)
    {
	   std::cout << "The number " << num1 << " is not positive number.\n";
	   std::cout << "Try again.\n";
	   std::cout << "Number 1: ";
	   std::cin >> num1;
    }

    std::cout << "Number 2: ";
    std::cin >> num2;

    // Use the while loop to validate the user input is valid.
    // Ask the user to try again with their input.
    while (num2 < 0)
    {
	   std::cout << "The number " << num2 << " is not positive number.\n";
	   std::cout << "Try again.\n";
	   std::cout << "Number 2: ";
	   std::cin >> num2;
    }

    // Output the result.
    std::cout << num1 << " + " << num2 << " = " << num1 + num2 << std::endl;

    return 0;
}
Last edited on
Reformatting your code so it is more obvious what is going on:

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
#include <iostream>
using namespace std;

int main()
{
    int numbers4, numbers8, numbers15, numbers16, numbers23, numbers42;
    numbers4 = 0;
    numbers8 = 0;
    numbers15 = 0;
    numbers16 = 0;
    numbers23 = 0;
    numbers42 = 0;

    cin >> numbers4;
    if (numbers4 == 4)
        /* do nothing */ ; 
    
    {
        cin >> numbers8;
        
        if (numbers8 == 8)
            /* do nothing */ ; 
        
        {
            cin >> numbers15;
            
            if (numbers15 == 15)
                ; 
            
            {
                cin >> numbers16;
                if (numbers16 == 16)
                    ; 
                
                {
                    cin >> numbers23;
                    if (numbers23 == 23)
                        ; 
                    
                    {
                        cin >> numbers42;
                        if (numbers42 == 42)
                            ; 
                        
                        {
                            cout << "Good Job!!";
                        }
                    }
                }
            }
        }
    }
}


Be more careful where you place your semi-colons.
Last edited on
First you have to get rid of the semi colons after the if statements like so: if (numbers4 == 4) {. Having a semicolon after an if statement tells it that the if statement is done. The short version is remove the semicolons after the if statements.

Examples:
if (4 == 4) cout << "4 equals 4";
This works however don't write it like this.

1
2
if (4 == 4)
    cout << "4 equals 4";

This works exactly the same as the above however is correctly formatted. As you can see both if statements end with the semicolon.

If you have more than one line of code related to an if statements you have to surround it with { } as you have done with your program.


Getting on to entering all the numbers before it tells you if you've failed you can simply move all the cin << numbers above the first if statement so you enter all your numbers then it goes through the chain of ifs to see if you entered the numbers correctly.
Thanks everyone, very helpful! I appreciate all the advice!
Topic archived. No new replies allowed.