Stuck at do while loop

I am currently having trouble on doing the do while loop. I am not sure why since I have been following the tutorial. I have manage to loop the menu b4 the switch case but Im trying to loop the question inside the switch case and having technical error.

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
  switch (choice)
        {
          case 1:
            cout <<endl<< "Please enter the detail of the ownership."; //Detail of the owner
            do{
			
		cout << endl <<"Please enter name your name : ";
    
            	std::string name;
           	getline(cin,name);
    
            		if(std::getline(std::cin, name))
            		{
                		if(name == "") //alternative form: if(name.empty())
                		{
                    	std::cout << "You pressed enter without typing anything" << std::endl;
                
                		}
            		}
            while(name == ""){   //trying to loop the question using do while loop when user did not enter his/her name. 
			
            	cout << endl <<"Please enter name your name : ";
    
            	std::string name;
           		getline(cin,name);
    
            		if(std::getline(std::cin, name))
            		{
                		if(name == "") //alternative form: if(name.empty())
                		{
                    	        std::cout << "You pressed enter without typing anything" << std::endl;
                
                		}
            		}
            	}
            }
            
		}while (choice <= 3 && choice >= 1); //its from the menu which i didnt posted in but manage to loop the menu instead. here 
Last edited on
You can't mix do while with switch like this

your second while needs to be inside case statement, but you attempt to to jump/repat to case which is illegal.

in short case label can be executed only once in each switch statement.

not even goto can help you to circumvent this.
so basically i cannot repeat the question? If so thx for the help
I'm not sure if I can help you because your choice variable doesn't tell it's purpose in the code which is needed to redesign code.

However your code can be simplified to following snippet, notice that case code is put into local scope which is needed for string variable to be constructed!,

if you can provide declaration and meaning of choice variable we could make a better design.

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
	switch (choice)
	{
	case 1:
	{
		std::cout << endl << "Please enter the detail of the ownership.";

		std::string name;

		while (name.empty())
		{
			std::cout << endl << "Please enter name your name : " << std::endl;

			if (std::getline(std::cin, name))
			{
				if (name.empty())
				{
					std::cout << "You pressed enter without typing anything" << std::endl;
					continue;
				}

				break;
			}
		}
	}

        break;
	}

Last edited on
@malibor,

If you did not see this you might want to look at http://www.cplusplus.com/forum/beginner/270924/#msg1167948 It is not complete, but might give you a better idea.

Andy
so basically i cannot repeat the question? If so thx for the help


you can repeat the question but you cannot jump ship on the case you are in.
this is valid:
switch(choice)
case 1:
while(...) {..}

but you can never change choice in that while loop and then have it execute case:2 instead it is 'stuck' in case 1 now.

if you want to do choice again, the loop goes around the switch:

do

switch(.....) {}

while (choice something)

you can fall thru your cases and have some convolutedness that would 'work' but would not 'make much sense'.

switch()
case 1: case 2: case 3:
while() ... //here you can change choice and act on it, and the case statement is now effectively doing nothing at all for those cases and would need some other case doing some other thing to make it anything more than clutter.

switches are very efficient and fall-through can be very elegant. But they are also very limited. Switch does 2 things well: it makes an if/else block compile into a lookup table that can perform well, and it allows fall-through which takes more juggling to get to the same place with if/else.
Last edited on
Hello rhap123,

As mentioned you are trying to do to much in a switch statement.

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
switch (choice)
{
    case 1:
        cout << endl << "Please enter the detail of the ownership."; //Detail of the owner
        do
        {

            cout << endl << "Please enter name your name : ";

            std::string name;
            getline(cin, name);

            if (std::getline(std::cin, name))
            {
                if (name == "") //alternative form: if(name.empty())
                {
                    std::cout << "You pressed enter without typing anything" << std::endl;

                }
            }
            while (name == "")  //trying to loop the question using do while loop when user did not enter his/her name. 
            {
                cout << endl << "Please enter name your name : ";

                std::string name;
                getline(cin, name);

                if (std::getline(std::cin, name))
                {
                    if (name == "") //alternative form: if(name.empty())
                    {
                        std::cout << "You pressed enter without typing anything" << std::endl;

                    }
                }
            }
        }

}while (choice <= 3 && choice >= 1); //its from the menu which i didnt posted in but manage to loop the menu instead. here  

Line 10 is defined inside the block of the do/while loop. At the closing brace (}) of the do/while loop the variable "name" is lost. If you intend to use this outside the do/while loop you will need to define "name" before the "switch".

Lines 11 and 13. How many times are you planning on asking for a name?

The code is poorly thought out and poorly designed.

This simply set up to deal with the name:
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>
#include <string>

int main()
{
    int choice{};
    std::string name;

    std::cout << "\nPlease enter the detail of the ownership.\n"; //Detail of the owner

    std::cout << "\n Please enter name your name: ";

    std::getline(std::cin, name);

    while (!name.size())  // <--- Returns (0) zero if empty.
    {   //trying to loop the question using do while loop when user did not enter his/her name. 
        std::cout << "\n     You pressed enter without typing anything\n";

        std::cout << std::endl << " Please enter name your name: ";
        std::getline(std::cin, name);

    }

    std::cout << "\n " << name;

    return 0;  // <--- Not required, but makes a good break point.
}


Another option:
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
#include <iostream>
#include <limits>
#include <string>

// <--- Prototypes.
void GetInput(std::string& name);

int main()
{
    int choice{};
    std::string name;

    std::cout << "\n       Menu\n"
        "\n 1. item 1"
        "\n 2. item 2"
        "\n Enter choice: ";
    std::cin >> choice;

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

    switch (choice)
    {
        case 1:
            GetInput(name);
            break;
        default:
            break;
    }


    return 0;  // <--- Not required, but makes a good break point.
}

void GetInput(std::string& name)  // <--- Can add more variables as needed. Should all be passed by reference. Adjust prototype to match.
{
    std::cout << "\nPlease enter the detail of the ownership.\n"; //Detail of the owner

    std::cout << "\n Please enter name your name: ";

    std::getline(std::cin, name);

    while (!name.size())  // <--- Returns (0) zero if empty.
    {   //trying to loop the question using do while loop when user did not enter his/her name. 
        std::cout << "\n     You pressed enter without typing anything\n";

        std::cout << std::endl << " Please enter name your name: ";
        std::getline(std::cin, name);

    }

    std::cout << "\n " << name; // <--- Used for testing. Comment or remove when finished.

// More code for other variables.
}



Not seeing all the code this is a best guess at what you may want.

Andy
Hello rhap123,

While working on the program I realized that you should help your-self and post the requirements of the program, and not your idea of what they mean, but the actual directions given to you.

This will let everyone know what you need to accomplish in the program.

Andy
I realized that I was way too overthinking of doing it but somehow manage to keep it simple. However Im having a small problem on how to limit the length of the string. Moreover, I would like to prompt an error if the user enter less than the number of the characters in the string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
do
	{

    cout << "1..............." << endl;
    cout << "2. ............. "<< endl;
    cin >> choice;

while (choice < 1 || choice > 2)
	{
		cout << "Please enter a valid choice" << endl;
		cout << "Enter 1or 2: " << endl;
		cin >> choice;         //trying to prompt an error if the user did not enter the requirement of the numbers of the characters.
		}
switch (choice){
                case 1:
                cout <<endl<<"Please enter name your IC : ";
		cin >> ic;
                       }
}
}while (choice <= 2 && choice >= 1);
Last edited on
Hello rhap123,

Watch your indenting it is terrible.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
do
{
    std::cout << "1..............." << std::endl;
    std::cout << "2. ............. " << std::endl;
    std::cin >> choice;

    while (choice < 1 || choice > 2)
    {
        std::cout << "Please enter a valid choice" << std::endl;
        std::cout << "Enter 1 or 2: " << std::endl;
        std::cin >> choice;         //trying to prompt an error if the user did not enter the requirement of the numbers of the characters.
    }

    switch (choice)
    {
        case 1:
            std::cout << std::endl << "Please enter name your IC : ";
            std::cin >> ic;
    }
}  // <--- End of do/while loop. No while condition.

} while (choice <= 2 && choice >= 1);  // <--- Outside the do/while loop. Does not go with anything nor does the closing (}) brace. 


I can only guess that you want to limit "ic". The first limit on "ic" is anything that contains a space. The formatted input will stop at the space and leave whatever is left in the input buffer causing a problem with your next input. Using "std::getline" would be a better choice.

Next you need to find the length of the string ic.size(). From that you can deal with the string being to short or use ic.resize(10); to limit the length. The 10 can be any number that you need.

And the case 1 is missing a "break;" unless you intend for it to fall through to case 2.

Andy
Topic archived. No new replies allowed.