Title: C++ How to loop this thing (beginner)
Tags: c++;loops
Question: I'm figuring out how to loop my code and now it got me confused, any help would be much appreciated, im a beginner here so please don't scold me.
#include <iostream&gt;
#include <string&gt;
using namespace std;
int main()
string str;
char ans;
cout << "CONVERSION\n\n";
cout << "Base 11 to Decimal\n";
a:cout << "Base 11: ";
getline(st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cin, str);
const auto bad_loc = str.find_first_not_of("0123456789aA");
if (bad_loc != st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0string75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0npos)
{
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cerr << "Invalid Input\n";
goto a;
}
unsigned long ul = st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0stoul(str, nullptr, 11);
cout << "Decimal: " << ul << "\n\n";
cout << "Do you want to continue (Y/N)? ";
cin &gt;&gt; ans;
if (ans == 'y')
{
goto a;
}
else if (ans == 'n')
{
cout <<"Name:\tXXXXXXXX\n";
cout <<"Course:\tXXXXXXXX";
cout <<"Section:\tXXXXXXXX\n";
cout <<"Schedule:\tXXXXXXXX\n";
cout <<"Professor:\tXXXXXXXX\n";
}
return 0;
the output should be like this
CONVERSION
Base 11 to Decimal
Base 11: B
Invalid Input!
Base 11: A
//blank
Do you want to continue (Y/N)? X
Invalid Answer! (Y/N)? Y
//blank
Base11:
My problems are
(1)The program stops after looping in "base11:" whenever i answer Y and (2)if I answer any letter from "Do you want..." the program will end. (3) How to fix this thing.
Comment: Put the code inside a `do {} while()` loop and check a sentinel if the input was correct.

Here is the accepted answer: We beginners should help each other should not we?:)
The program can look something like the following
#include <iostream&gt;
#include <string&gt;
#include <limits&gt;
int main()
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << "CONVERSION\n\n";
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << "Base 11 to Decimal\n";
char ans;
do
{
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0string s;
bool valid_input = false;
while ( !valid_input )
{
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << "Base 11: ";
if ( !st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0getline( st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cin, s ) ) break;
if ( !( valid_input = s.find_first_not_of( "0123456789aA" ) == st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0string75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0npos ) )
{
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << "Invalid Input\n\n";
}
}
if ( !valid_input )
{
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << "See you later!" << st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0endl;
break;
}
unsigned long value = st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0stoul( s, nullptr, 11 );
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << "Decimal: " << value << "\n\n";
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << "Do you want to continue (Y/N)? ";
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cin &gt;&gt; ans;
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cin.ignore( st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0numeric_limits<st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0streamsize&gt;75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0max(), '\n' );
} while ( ans == 'y' || ans == 'Y' );
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout <<"Name:\tXXXXXXXX\n";
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout <<"Course:\tXXXXXXXX";
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout <<"Section:\tXXXXXXXX\n";
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout <<"Schedule:\tXXXXXXXX\n";
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout <<"Professor:\tXXXXXXXX\n";
return 0;
If the input looks like
then the output will be
CONVERSION
Base 11 to Decimal
Base 11: B
Invalid Input
Base 11: A
Decimal: 10
Do you want to continue (Y/N)? Y
Base 11: C
Invalid Input
Base 11: AA
Decimal: 120
Do you want to continue (Y/N)? N
Name:   XXXXXXXX
Course: XXXXXXXXSection:    XXXXXXXX
Schedule:   XXXXXXXX
Professor:  XXXXXXXX
Pay attention to statement
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cin.ignore( st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0numeric_limits<st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0streamsize&gt;75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0max(), '\n' );
that skips the new line character entered after inputting ans using operator &gt;&gt;.
Comment for this answer: @JetMortolaJoseph You could use standard function st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0strchr declared in header  the following way if ( !st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0strchr( "YyNn", ans ) ) { /* some message */ } But In my opinion it is better just to repeat the loop if ans is not equal to 'n' or 'N'
Comment for this answer: @JetMortolaJoseph For example instead of the condition while ( ans == 'y' || ans == 'Y' ); you could write another condition while ( ans != 'n' && ans != 'N' );
Comment for this answer: @JetMortolaJoseph Your question is about C++ is not it?
Comment for this answer: @JetMortolaJoseph I have a very slow internet and usually do not take part in chats.
Comment for this answer: Thank you so much! It helps a lot and the new code is on my brain now.
Comment for this answer: how about if i want to allow only letters "yY/Nn" on the "do you want..." question, am i going to add another code on the while thingy?
Comment for this answer: I'm getting error with the 'stoul' and 'nullptr' in visual basic 6.0, and im afraid to put an alternate code because I don't know what's the replacement there :(
Comment for this answer: Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105139/discussion-between-jet-mortola-joseph-and-vlad-from-moscow).
Comment for this answer: I moved to discussion chat, still c++
Comment for this answer: got it on my new question. hehe

Here is another answer: bool run_attempt() {
get some input
if (bad input)
return false;
else {
do something good with the input
return true;
void main() {
bool success = false;
while (!success) {
success = run_attempt();
Comment for this answer: Put your user interaction inside of run_attempt(). Let main() only handle the looping.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:03/02/16', 'FavoriteCount': 0.0, 'PostTypeId': 1, 'Tags': '<c++><loops>', 'Title': 'C++ How to loop this thing (beginner)', 'ViewCount': 61, 'id': 35745118, 'user_AboutMe': None, 'user_AccountId': 7957183.0, 'user_DisplayName': 'Jet Mortola Joseph', 'user_Reputation': 131.0, 'user_Views': 23.0, 'user_id': 6006053.0}}