Void/While/if help. :P!

Elo, need some help.


#include <iostream>
using namespace std;

void print();


int main(){

int x;
cout << "Hello, enter the number 5";
cin >> x;

if(x == 5){
cout << "Nice!" << endl;
}
else
print();



return 0;
}


void print(){

int y;

cin >> y;
while (y > 0){
cout << y << endl;
y++;
}

}


Just run the project. Why after you type in the wrong answer (Not 5) That you have to type in another number to activate the loop?

Thx lots!

(This program is of no help, and im just seeing how stuff works)
Because inside the function before the loop you ask for input.
Uhh?

Sorry, what/which function are u talking about? (totally new at this lmao)

;
It's the print() function. If you put the code in [code][/code] tags, I could give a line 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
31
32
33
34
35
#include <iostream>
using namespace std;

void print();


int main(){

int x;
cout << "Hello, enter the number 5";
cin >> x;

if(x == 5){
cout << "Nice!" << endl;
}
else
print();



return 0;
}


void print(){

int y;

cin >> y;
while (y > 0){
cout << y << endl;
y++;
}

}


There!
And i had void print();

I thought it makes it go first?
? No, the main() function is always run when the program starts, and other functions are run only if you call them. On line 17, you call the print() function. You asked why it waited for input; that's because on line 29 you ask for input before running the loop.
1
2
3
4
while (y > 0){
cout << y << endl;
y++;
}


You realize this will run for a LONG time probably, right?
Zhuge: So what do i have to change??? W/o cin >> y; it does not work.

Firedraco: If you look at my first post, you see i said "This program is not help, just wanting to see how things work. :)
What is it supposed to do?
Like if you get it wrong (Not == to 5) it randomly turns on the loop. If you do get it right, it does nothing.
Last edited on
Topic archived. No new replies allowed.