#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string fname;
string lname;
string prefadd;
int age;
char agever;
cout << "For the purpose of this project, I am going to collect" << endl;
cout << "some information from you" << endl;
cout << "\n";
system("PAUSE");
while(true)
{
cout << "\nWhat is your first name?" << endl;
cin >> fname;
cout << "\nWhat is your last name?" << endl;
cin >> lname;
cout << "\nWhat is your age?" << endl;
cin >> age;
cout << "\nFirst Name: " << fname << endl;
cout << "Last Name: " << lname << endl;
cout << "Age: " << age << endl;
cout << "\n";
cout << "Is this correct?" << endl;
cout << "(y) or (n)" << endl;
cout << "\n";
cin >> agever;
if (agever == 'y' || agever == 'Y') {
cout << "\nok then I got your info right" << endl;
break;
}
else
{
cout << "\nSorry, I will try again." << endl;
}
cout << "\n";
cout << "Mind if I call you Mr. " << lname << "?";
system("PAUSE");
}
}
So on line 41 if they say "y" that the information is correct..the program just hands and says press any key to continue..then when you press a key the program closes. However if they say "n" then it loops like its supposed to and asks them for their information again.
How can I get the program to continue to line 51 after it says "ok then I got ur info right" rather than ending the program there?
If you want the loop to keep going, don't break out of it. The break command is what's exiting the loop. Since you break after you print "ok then I got your info right", that's where the loop exits, and then the program exits because there's nothing after the loop.
if (dataver == 'y' || dataver == 'Y') {
cout << "\nok then I got your info right" << endl;
}
else
{
cout << "\nSorry, I will try again." << endl;
}
cout << "\n";
cout << "Mind if I call you Mr. " << lname << "?";
system("PAUSE");
}
}
yeah but then when I remove the break, it seems to work. It says "Mind if I call you mr. <whatever last name you put>" and says press any key to continue (like it should), but then when you press any key to continue, it loops back to the beginning of the app.
Also if you say "n" when it asks if the information is correct. it still goes ahead and says "mind if I call you mr. <last name here>?" so something isnt right.
if(dataver == 'y' || dataver == 'Y')
{
// your program will do the stuff in here when the user inputs Y
}
else
{
// otherwise (they input something else), the stuff here will be done
}
// code here will be done after above code, and regardless of
// what the user pressed
Move the code to be where you want it to be.
---
I don't need to download it and run it -- I can see what it's doing by reading the code ;P (you'll get there too, after a while)
I just want the loop to run and start over when they enter "n" but I want to keep on movin with the program if they pick "y" without it closing out the program after you "any key to continue"
if (dataver == 'y' || dataver == 'Y') {
cout << "\nok then I got your info right" << endl;
break;
}
else
{
cout << "\nSorry, I will try again." << endl;
}
cout << "\n";
}
cout << "Mind if I call you Mr. " << lname << "?";
system("PAUSE");
}
okay -- step back and ask yourself these questions:
1) what do you want the program to do when the user presses Y?
2) what do you want the program to do when the user presses something else?
Once you have the answer to the above questions, figure out the code required to do that... and put it in the appropriate places in your program.
You're using a simple if/else here, so the logic isn't complicated:
1 2 3 4 5 6 7 8
if(dataver == 'y' || dataver == 'Y')
{
// code to do #1 goes here
}
else
{
// code to do #2 goes here
}
Exiting the loop can be done with a break command, or you can use a loop condition. Right now you have an infinite loop while(true). Perhaps change that to something like: