HELP WITH A BASIC CODE

Jun 27, 2012 at 1:05am
Okay so I'm trying to do one of the beginner programs which tells a user not to type a certain number. The user then types a number and if it isn't that certain number they have to try again. They do this until they actually type the number they were told not to type. Here is what I have so far and I face two problems: The very first input the user gives, for some reason isn't registering or something.. second, when the for loop is ran for the tenth time, the program terminates. This is fine but I also wanted it to print a message on screen before it terminated.. Please help and sorry that I'm such a newbie!

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>

using namespace std;

int main()
{
    int input = 0;
    cout << "Do not type the number 5.\n";
    cin >> input;

    for (int i = 1; i != 10; i++)
        {
            cin >> input;
            if (input != 5)
            {
                cout << "Try Again.\n";
            }
            else if (input == 5)
            {
                cout << "Disobedience is Frowned Upon.";
            }

        }

    return 0;
}
Last edited on Jun 27, 2012 at 1:16am
Jun 27, 2012 at 1:35am
you have two "cin >> input" in a row. get rid of the one on line 9.

You could print to screen during the last iteration of the loop.

1
2
if (i == 9)
        cout << "Game over";



Jun 27, 2012 at 4:13am
Or put your final message after and outside the loop.

1
2
3
4
5
for
{
}
cout << "final message";
return 0;
Jun 27, 2012 at 5:51am
try this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
    int input = 0;
    cout << "Do not type the number 5.\n";
    cin >> input;
    for (int i = 1; i != 10; i++)
        {
            if (input != 5)
            {
                cout << "Try Again.\n";
            }
            else if (input == 5)
            {
                cout << "Disobedience is Frowned Upon.\n";
            }
            cin >> input;
        }
        cout << "bye.\n";
        return 0;
}
Last edited on Jun 27, 2012 at 5:56am
Jun 27, 2012 at 5:57am
this works nicely and it also finishes if user has entered 5
if the games is finished he also gets a message 'your message'

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>

using namespace std;

int main()
{
    int input = 0;
    cout << "Do not type the number 5.\n";

    while (input!=5)
        {
            cin >> input;
            if (input != 5)
            {
                cout << "Try Again.\n";
            }
            else if (input == 5)
            {
                cout << "Disobedience is Frowned Upon.\n";
            }
		
        }
	cout << "your message" <<endl;
	system("pause");
	return 0;
}
Jun 27, 2012 at 9:32am
This :

for (int i = 1; i != 10; i++)

is normally written like this:

1
2
3
4
for (int i = 0; i < 10; i++)  {
//do some thing 10 times
}





Jun 27, 2012 at 10:10am
Yeah, what TheIdeasMan said. It's safer, in case you change i within the loop or something like that.

Consider the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// This loop will run three times, as at the end of the third iteration, i is 11.
for(int i=0; i < 10; i++)
{
	cout << "First i: " << i << endl;
	i+=3;
	cout << "Second i: " << i << endl;
}

// This loop will run a lot more than three times. 
for(int i=0; i != 10; i++)
{
	cout << "First i: " << i << endl;
	i+=3;
	cout << "Second i: " << i << endl;
}
Topic archived. No new replies allowed.