Loop not "looping" and if always true etc.

I'm trying to create a simple while() -loop and keep it running as long as the user does not enter '0'. I have succeeded with this task before, but now I can't remember how.

What I'm trying to do... (this is just an example)

Main function:
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
#include <iostream>
#include <wiondows.h>
using namespace std;

void myMeny(int x);
void menyOne();

int main()
{
    int x;
    while(x!=0)    //while 'x' is not equal to '0'
    {
        myMeny(x);

        if(x = 1)
        {
            system("cls");
            menyOne();

        } //END if

        // .... more if-statements not shown in this example

        else
        {
            cout << "Wrong number. Try again.";
            Sleep(1000);
            system("cls");

        } //END else

    } //END while

} //END main 


myMeny function:
1
2
3
4
5
6
7
8
9
10
void myMeny(int x)
{
    cout << "Show meny one    : 1\n";
    cout << "Exit application : 0\n";

    //.... more options not shown in this example

    cin >> x;

} //END myMeny 


menyOne function:
1
2
3
4
5
6
7
8
void menyOne()
{
    cout << "This is meny one.";
    Sleep(1000);

    // How do I return to the main function??

} //END menyOne 


I suppose there is more than one problem with my code.

*Key question:
No matter what I put in, the if(x=1) always return true. The else-statement is never shown. I tried changing it to if(x==1) but when I put in 1, the else is shown. And the loop breaks after the else statement is shown. Why?

And how do I return to my main function from my menyOne -function?

Thanks in advance!
I will continue to update this topic to give as much information as possible.

EDIT:
I was having a hard time to figure out what to title this topic. If you have a better suggestion, please tell me. =)
Last edited on
Here

1
2
3
4
    int x;
    while(x!=0)    //while 'x' is not equal to '0'
    {
        myMeny(x);


you did not initialize x before entering the loop. And x did not change its value by calling myMeny(x); because the parameter of the function is not a reference.
Also it would be better to substitute the while loop for do-while loop.
Last edited on
Where should I put the '&' symbol to make it a reference?

In myMeny function:
 
void myMeny(int &x)


or

In main function:
1
2
3
4
    int x;
    while(x!=0)    //while 'x' is not equal to '0'
    {
        myMeny(&x);


Also it would be better to substitute the while loop for do-while loop.

I tried to do a do-while loop. Is this correct?
1
2
3
4
5
6
7
    do

    int x;
    myMeny(x);

    while(x!=0)    //while 'x' is not equal to '0'
    {


EDIT: If the do-while loop is set up like the example shown above, i get the following error:
4|error: expected 'while' before 'myMeny'|
4|error: expected '(' before 'myMeny'|
4|error: 'x' was not declared in this scope|
4|error: expected ')' before ';' token|
Last edited on
The parameter of the function shall be declared as a reference to int.

1
2
3
4
5
6
7
8
9
10
void myMeny(int &x)
{
    cout << "Show meny one    : 1\n";
    cout << "Exit application : 0\n";

    //.... more options not shown in this example

    cin >> x;

} //END myMeny  


However it is better to define such a function in another way

1
2
3
4
5
6
7
8
9
10
11
12
13
int myMeny()
{
    int choice = 0;

    cout << "Show meny one: 1\n";
    cout << "Exit application : 0\n";

    //.... more options not shown in this example

    cin >> choice;

    return ( choicce );
} //END myMeny  


Then in the main it must be written

x = myMeny();

The do-while loop can look the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
    do    //while 'x' is not equal to '0'
    {
        x = myMeny();

        if(x == 1)
        {
            system("cls");
            menyOne();

        } //END if

        // .... more if-statements not shown in this example

        else if ( x != 0 )
        {
            cout << "Wrong number. Try again.";
            Sleep(1000);
            system("cls");
        }
    }  while ( x != 0 );
Last edited on
Quote vlad from moscow:
Then in the main it must be written

x = myMeny();

The do-while loop can look the following way
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    do    //while 'x' is not equal to '0'
    {
        x = myMeny();

        if(x == 1)
        {
            system("cls");
            menyOne();

        } //END if

        // .... more if-statements not shown in this example

        else if ( x != 0 )
        {
            cout << "Wrong number. Try again.";
            Sleep(1000);
            system("cls");
        }
    }  while ( x != 0 ); 


It gives me following error:

3|error: 'x' was not declared in this scope|


20|error: 'x' was not declared in this scope|


I declared 'x' after the first '{' in my do, and did put the while(x != 0) after the last '}'.

Am I suposed to declare 'x' outside the scope? In main?
You should insert your code that exists before the loop. I showed only the loop and one function definition.
Can you please give an example? I do not know where or what...

Thanks for the help =)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <wiondows.h>


using namespace std;

int myMeny();
void menyOne();

int main()
{
    int x;

    do    //while 'x' is not equal to '0'
    {
        x = myMeny();

        if(x == 1)
        {
            system("cls");
            menyOne();

        } //END if

        // .... more if-statements not shown in this example

        else if ( x != 0 )
        {
            cout << "Wrong number. Try again.";
            Sleep(1000);
            system("cls");
        }
    }  while ( x != 0 ); 

    return 0;
}

int myMeny()
{
    int choice = 0;

    cout << "Show meny one: 1\n";
    cout << "Exit application : 0\n";

    //.... more options not shown in this example

    cin >> choice;

    return ( choicce );
} //END myMeny

void menyOne()
{
    cout << "This is meny one.";
    Sleep(1000);

    // How do I return to the main function??

} //END menyOne     
Thank you very very much! You're great!

Just one question remains... How do I return to the main function from menyOne? Is there some way to call the main? Is it as simple as putting int main(); after the Sleep(1000) in function menyOne?
closed account (28poGNh0)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <windows.h>
using namespace std;

void myMenu(int &);
int menuOne();
int menuTwo();

int main()
{
    int x=9999;                    // Initialisation is good
	int nbrReturnedFromMenuOne = 0;
	int nbrReturnedFromMenuTwo = 0;

    while(x!=0)// x=9999 so we can enter the loop 
    {
        myMenu(x);
		// if you have ".... more if-statements not shown in this example" ,then use switch
		
		switch(x)
		{
			case 0:
				x = 0;
				system("cls");
			break;
			case 1:
				system("cls");
				nbrReturnedFromMenuOne = menuOne();
				cout << "\nAh ,this is the number returned from menu one : " << nbrReturnedFromMenuOne << endl ;
				system("pause");system("cls");
			break;
			case 2:
				system("cls");
				nbrReturnedFromMenuTwo = menuTwo();
				cout << "\nAh ,this is the number returned from menu two : " << nbrReturnedFromMenuTwo << endl ;
				system("pause");system("cls");
			break;
			default : cout << "Wrong number. Try again." << endl; break;
		}
	}cout << "End of program " << endl;
	return 0;
}

void myMenu(int &a)
{
	cout << "Exit application : 0\n";
    cout << "Show menu one    : 1\n";
    cout << "Show menu two    : 2\n";

    //.... more options not shown in this example

    cin >> a;

}

int menuOne()
{
    cout << "This is menu one." << endl;
    Sleep(1000);

    // One way to return to the main function ,is by changing the type menuOne from void to int 
	// Then return the number or the string .. here I return just 
	return 11;	
}

int menuTwo()
{
    cout << "This is menu two." << endl;
    Sleep(1000);

	return 22;	
}
1
2
@Krusing 
Just one question remains... How do I return to the main function from menyOne? Is there some way to call the main? Is it as simple as putting int main(); after the Sleep(1000) in function menyOne?


menuOne will passes the control to the main as soon as the closed '}' will be reached during execution of the body of the function. You need not to use a return statement because the function returns nothing to the main.
Topic archived. No new replies allowed.