Problem with loop/function

Hello, i am fairly new at programming, I wanted to create a program using loops and functions seeing as how i just learned them. I have gotten it to do what i want the program to do, except when the program completes, instead of ending, it repeats the message "That is correct, congrats!" over and over again. Was looking for some help to see what i am doing wrong.
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 <string>
using namespace std;

int n;

int ifElse()
{   
    for(;;)
    {
    if(n<7)
    {
        cout << "pick a higher number\n";
        cin >> n;
    }
    else if(n>7)
    {
        cout << "pick a lower number\n";
        cin >> n;
    }
    else
    {
    cout << "That is correct, congrats!\n";
}
    
}

}

int main()
{
    cout << "Try to guess the correct number\n" << "please insert a number" << "\n";
    cin >> n;
    ifElse();
    
    

system("pause");
return 0;
}
Last edited on
please use [code][/code] tags.
In your function you have an infinite loop for(;;) so you have to call break to stop its execution try adding it in the last else block:
1
2
3
4
5
else
{
    cout << "That is correct, congrats!\n";
    break;
}
Last edited on
Topic archived. No new replies allowed.