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();
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.
// Example program
#include <iostream>
#include <string>
int main()
{
usingnamespace 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);
}
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.
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 : ";