Executing a for loop statement help!

Write your question here.
I have to write a C++ program that picks a random number between 0 and 49. If the number is even lets say 30, then the computer will display 30, 32, 34, 36... all the way till 100, if its odd lets say 17, then the computer will display 17, 19.. till 99. I got the computer picking a random number, I just can't figure out how to display every other number using a for loop statement. Here's my code for random number generator:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
        {

        srand((unsigned)time(0));

        int random_integer;

        random_integer = (rand()%49);

        cout << random_integer << endl;

        }

        return 0;
}
1
2
for( ; random_integer < 101; random_integer += 2)
    std::cout << random_integer << '\n';
It said random_integer was not declared in this scope!

So i declared int random_integer;

And when i ran it the computer picked a random number but it didn't display every other number.
It said random_integer was not declared in this scope!
That strange because in your code it is declared.

Show what you got.
This is what I have so far, and how is the computer going to know if its even or odd i don't understand how to implement that and how to display every other number.

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

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()

{

        {

        srand((unsigned)time(0));

        int random_integer;

        random_integer = (rand()%49);

        cout << random_integer << endl;

        }
	
	for( ; random_integer < 101; random_integer += 2)
	cout << random_integer << '\n';
	

        return 0;
}
remove braces at lines 12 and 22

how is the computer going to know if its even or odd
It does not need to know that. What you do will not change anyway.

and how to display every other number.
This is what loop do. Notice that it is displaying every other number.
another thing, random_integer = (rand()%49); gives 0 to 48
Thanks guys it works! Now if I want to ask the user if he wants to go again here is what I did but idk how to restart the whole thing:

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

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()

{
        srand((unsigned)time(0));

        int random_integer;
	char re_do;

        random_integer = (rand()%50);

        cout << random_integer << endl;

	
	for( ; random_integer < 101; random_integer += 2)
	cout << random_integer << '\n';

	break;

	cout << "Would you like to go again?" << endl;
	cin >> re_do;

	if (char = 'y')
	

        return 0;
}
Last edited on
Wrap code you want to repeat in a loop:

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

using namespace std;

int main()
{
    srand((unsigned)time(0));

    char re_do = 'n';
    do {
        int random_integer = (rand()%50);
        cout << random_integer << endl;
	
        for( ; random_integer < 101; random_integer += 2)
            cout << random_integer << '\n';

        cout << "Would you like to go again?" << endl;
        cin >> re_do;
    } while (re_do == 'y');
}
Im sorry you may be right but I dont understand what you mean by wrapping it in a loop. Soory im new to c++
Make code you want to reapead the body of while loop which checks if repeat is needed:
http://www.cplusplus.com/doc/tutorial/control/
I tried using the do while loop to run it again but it gave me this error:

1
2
3
4
5
6
7

test4.cpp: In function ‘int main()’:
test4.cpp:20: error: expected ‘;’ before ‘)’ token
test4.cpp:24: error: expected ‘;’ before ‘cin’
test4.cpp:34: error: ‘re_do’ was not declared in this scope
test4.cpp:35: error: ‘re_do’ was not declared in this scope


Here is 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

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()

{
        char redo;

        srand((unsigned)time(0));

        int random_integer;

        random_integer = (rand()%49);

        cout << random_integer << endl;

        for(random_integer < 101; random_integer += 2)
        cout << random_integer << '\n';

	cout << "Would you like to go again?"
	cin >> re_do;

	do {
        int random_integer = (rand()%50);
        cout << random_integer << endl;
	
        for( ; random_integer < 101; random_integer += 2)
            cout << random_integer << '\n';

        cout << "Would you like to go again?" << endl;
        cin >> re_do;
    } while (re_do == 'y');

        return 0;
}
Line 21
My code: for( ; random_integer < 101; random_integer += 2)  
Your code: for(random_integer < 101; random_integer += 2)  
Spot the difference

Line 24: Missing semicolon;

Lines 35 and 36: look at the line 11.
Topic archived. No new replies allowed.