Switch case but need to loop the question back and forth

May 31, 2020 at 1:15pm
Basically I am having trouble with this code since it does not loop the question back. It automatically proceed to the next question. Need help here since Im jz a beginner.
switch (choice)
{
case 1:
cout <<endl<< "Please enter the detail of the ownership."; //Detail of the owner
cout << endl <<"Please enter name your name : ";
getline(cin,name);
std::string name;
if(std::getline(std::cin, name))
{
if(name == "") //alternative form: if(name.empty())
{
std::cout << "You pressed enter without typing anything" << std::endl;
}
}
cin.ignore();
May 31, 2020 at 2:11pm
It's not looping because you aren't using any loops.
What should cause the program to loop, or what should cause it to stop looping? You need to write the logic for this.

Scroll down to the "Loops" section of the Control Flow tutorial:
http://www.cplusplus.com/doc/tutorial/control/

Also,
1
2
getline(cin,name);
std::string name;
You're either using name before it's declared, or you're shadowing a variable in an outer scope. Either way, it makes your the code harder to reason.

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
// Example program
#include <iostream>
#include <string>

int main()
{
    using namespace std;
    
    int choice = 42;
    do
    {
        cout << "Enter choice (42 to quit):\n";
        cin >> choice;
        
        switch (choice)
        {
          case 1:
            cout <<endl<< "Please enter the detail of the ownership."; //Detail of the owner
            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 != 42);
}
Last edited on May 31, 2020 at 2:12pm
May 31, 2020 at 3:03pm
actually i wanted to loop the question of asking the user's name inside the switch case. The code that you have done, is almost the same thing as my code but in terms of looping i have already loop the codes b4 the switch case. Sorry since i just cut my codes only but here is the full version of my code. Sorry for the confusion and wasting your time.


do
{

cout << "1. Register new vehicle ownership " << endl;
cin >> choice;

while (choice < 1 || choice > 4)
{
cout << "Please enter a valid choice" << endl;
cout << "Enter 1, 2, 3, or 4: " << endl;
cin >> choice;
}


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

return 0;
}
Last edited on May 31, 2020 at 3:05pm
Topic archived. No new replies allowed.