HOW TO BREAK A LOOP FROM A FUNCTION?

closed account (ivDwAqkS)
Id like to know how to break a loop of the main function with another function, I read a little of answers but none of them tells me if it is possible to break from a function outside the main loop. its supposed if you return 0 you end the program, but here it just still going and asking for the number, it prints the message, though.

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

int functionbreak(int);

int main(){
    int n;

    while(true){

        cout << "First Input: ";
        cin >> n;
        functionbreak(n);

        cout << "Second Input: ";
        cin >> n;
        functionbreak(n);
    }
    return 0;
}

int functionbreak(int n){
    if(n == 9){
        cout << "You lose" << endl;
        return 0;//supposes to break/end
    }
}
Last edited on
I don't really know a way of doing it without gotos, but since they're bad, you could do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
//your functionbreak
while(true)
{
cout<< "First Input: ";
cin>> n;
if( functionbreak(n) == 0)
   break;

cout<< "Second Input: ";
cin>> n;
if( functionbreak(n) == 0)
   break;
}


Cheers!
why not simply put:
1
2
if(n == 9)
    break;
its supposed if you return 0 you end the program,
no its not. return just ends the function you are currently in, by returning a value (unless the function returns void then it is just return). in main(), if you return a number (it doesnt have to be zero) then the program ends
closed account (ivDwAqkS)
coz I am making a program that has many possibilities to finish the program and I was trying to put it in another function, that's why.
closed account (ivDwAqkS)
THANKS MatthewRock and THANKS Little Bobby Tables for the clarification. :D and Little Bobby Tables can you put an example please?
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
#include <stdio.h>

void func1(); //returns void or nothing
int func2(); //returns an int (integer)
char func3(); //returns a char (character)

int main(int argc, char *argv[])
{
    printf("calling func1\n");
    func1(); //pass control to func1
    printf("out of func1\n"); //return control to main

    printf("calling func2\n");
    printf("func2's value: %d\n", func2()); //pass control to func2 and print the value
    printf("returned from func2\n"); //return from func2

    printf("calling func3\n");
    printf("func3's value: %c\n", func3()); //same idea
    printf("out of func3");

    return 1; //just return some random int
    printf("You will never see this since we return'ed from main, thus exiting the program");
}

void func1()
{
    printf("In func1\n");
    return; //the value is omitted since it doesnt return a value
    printf("this line will never be reached\n");
}

int func2()
{
    printf("In func2\n");
    return 1; //we return from the function, but it returns an int, so we return a 1
    printf("You will never see this since we are out of the function\n");
}

char func3()
{
    printf("In func2\n");
    return 'a'; //same as func2, except this time we return a char ('a')
    printf("You will never see this since we are out of the function\n");
}


edit: forgot to have variables catch the return values
Last edited on
closed account (ivDwAqkS)
thanks, bro.
no problem
Topic archived. No new replies allowed.