Unable to check for non-integer input
The else statement in this code does not execute, instead, when a character is entered, it would get stuck in a continuous loop, repeating the lines 33 and 35.
I want to check if the user input is not an integer in the else statement and ask the user again to specify the age.
#include <iostream>
#include <windows.h>
using namespace std;
class User
string name;
public:
string getName()
return name;
void setName(string newName)
name = newName;
;
int main()
/** Initialise Variables */
string name;
int age;
User u1;
bool running = true;
while(running)
/** Welcome Message w/ User's Name */
cout << "What is your name?: " << endl;
cin >> name;
u1.setName(name);
cout << "Welcome, " << u1.getName() << "!" << endl;
returnToAge:
Sleep(1000);
cout << u1.getName() << ", to continue, you have to be over 18." << endl;
Sleep(1000);
cout << "Please enter your age: " << endl;
cin >> age;
if (age >= 18)
cout << "You may continue..." << endl;
Sleep(1000);
//Enter rest of questionnaire here
else if(age < 18)
cout << "You are underage! Please try again!" << endl;
Sleep(1500);
goto returnToAge;
else
cout << "INVALID INPUT!" << endl;
goto returnToAge;
c++
|
show 1 more comment
The else statement in this code does not execute, instead, when a character is entered, it would get stuck in a continuous loop, repeating the lines 33 and 35.
I want to check if the user input is not an integer in the else statement and ask the user again to specify the age.
#include <iostream>
#include <windows.h>
using namespace std;
class User
string name;
public:
string getName()
return name;
void setName(string newName)
name = newName;
;
int main()
/** Initialise Variables */
string name;
int age;
User u1;
bool running = true;
while(running)
/** Welcome Message w/ User's Name */
cout << "What is your name?: " << endl;
cin >> name;
u1.setName(name);
cout << "Welcome, " << u1.getName() << "!" << endl;
returnToAge:
Sleep(1000);
cout << u1.getName() << ", to continue, you have to be over 18." << endl;
Sleep(1000);
cout << "Please enter your age: " << endl;
cin >> age;
if (age >= 18)
cout << "You may continue..." << endl;
Sleep(1000);
//Enter rest of questionnaire here
else if(age < 18)
cout << "You are underage! Please try again!" << endl;
Sleep(1500);
goto returnToAge;
else
cout << "INVALID INPUT!" << endl;
goto returnToAge;
c++
You will probably find that not many people like the use of the goto. There tend to be better ways of structuring code so that they're not necessary (they're messy, and hard to control, especially as code grows.) Can I suggest you use a loop around the age entry, and only exit the loop when the conditions are met? Also, might be good to put that sort of operation into a function, as it can be separate from the main flow then, and easier to see/understand.
– Rags
Nov 12 at 11:29
A very general comment: don't use labels andgoto
statements. They are never necessary, and create hard to read code (and bugs as a result of that).
– Henning Koehler
Nov 12 at 11:30
1
@Rags I know there are better ways of using something other than goto. It is currently the most understandable code to me, to use at this moment in time. I will use something better as the code grows. I'll have a go with what you have said.
– Leon185
Nov 12 at 11:32
2
A classic in Computer Science history: "goto statements considered harmful" by Edgar Dijkstra homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.pdf
– Christian G
Nov 12 at 11:40
1
@ChristianG Not only that - there's a lot by Dijkstra that's worth reading. Some of it is quite unpalatable to some people - but his insight was amazing.
– Rags
Nov 12 at 11:50
|
show 1 more comment
The else statement in this code does not execute, instead, when a character is entered, it would get stuck in a continuous loop, repeating the lines 33 and 35.
I want to check if the user input is not an integer in the else statement and ask the user again to specify the age.
#include <iostream>
#include <windows.h>
using namespace std;
class User
string name;
public:
string getName()
return name;
void setName(string newName)
name = newName;
;
int main()
/** Initialise Variables */
string name;
int age;
User u1;
bool running = true;
while(running)
/** Welcome Message w/ User's Name */
cout << "What is your name?: " << endl;
cin >> name;
u1.setName(name);
cout << "Welcome, " << u1.getName() << "!" << endl;
returnToAge:
Sleep(1000);
cout << u1.getName() << ", to continue, you have to be over 18." << endl;
Sleep(1000);
cout << "Please enter your age: " << endl;
cin >> age;
if (age >= 18)
cout << "You may continue..." << endl;
Sleep(1000);
//Enter rest of questionnaire here
else if(age < 18)
cout << "You are underage! Please try again!" << endl;
Sleep(1500);
goto returnToAge;
else
cout << "INVALID INPUT!" << endl;
goto returnToAge;
c++
The else statement in this code does not execute, instead, when a character is entered, it would get stuck in a continuous loop, repeating the lines 33 and 35.
I want to check if the user input is not an integer in the else statement and ask the user again to specify the age.
#include <iostream>
#include <windows.h>
using namespace std;
class User
string name;
public:
string getName()
return name;
void setName(string newName)
name = newName;
;
int main()
/** Initialise Variables */
string name;
int age;
User u1;
bool running = true;
while(running)
/** Welcome Message w/ User's Name */
cout << "What is your name?: " << endl;
cin >> name;
u1.setName(name);
cout << "Welcome, " << u1.getName() << "!" << endl;
returnToAge:
Sleep(1000);
cout << u1.getName() << ", to continue, you have to be over 18." << endl;
Sleep(1000);
cout << "Please enter your age: " << endl;
cin >> age;
if (age >= 18)
cout << "You may continue..." << endl;
Sleep(1000);
//Enter rest of questionnaire here
else if(age < 18)
cout << "You are underage! Please try again!" << endl;
Sleep(1500);
goto returnToAge;
else
cout << "INVALID INPUT!" << endl;
goto returnToAge;
c++
c++
asked Nov 12 at 11:25
Leon185
5711
5711
You will probably find that not many people like the use of the goto. There tend to be better ways of structuring code so that they're not necessary (they're messy, and hard to control, especially as code grows.) Can I suggest you use a loop around the age entry, and only exit the loop when the conditions are met? Also, might be good to put that sort of operation into a function, as it can be separate from the main flow then, and easier to see/understand.
– Rags
Nov 12 at 11:29
A very general comment: don't use labels andgoto
statements. They are never necessary, and create hard to read code (and bugs as a result of that).
– Henning Koehler
Nov 12 at 11:30
1
@Rags I know there are better ways of using something other than goto. It is currently the most understandable code to me, to use at this moment in time. I will use something better as the code grows. I'll have a go with what you have said.
– Leon185
Nov 12 at 11:32
2
A classic in Computer Science history: "goto statements considered harmful" by Edgar Dijkstra homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.pdf
– Christian G
Nov 12 at 11:40
1
@ChristianG Not only that - there's a lot by Dijkstra that's worth reading. Some of it is quite unpalatable to some people - but his insight was amazing.
– Rags
Nov 12 at 11:50
|
show 1 more comment
You will probably find that not many people like the use of the goto. There tend to be better ways of structuring code so that they're not necessary (they're messy, and hard to control, especially as code grows.) Can I suggest you use a loop around the age entry, and only exit the loop when the conditions are met? Also, might be good to put that sort of operation into a function, as it can be separate from the main flow then, and easier to see/understand.
– Rags
Nov 12 at 11:29
A very general comment: don't use labels andgoto
statements. They are never necessary, and create hard to read code (and bugs as a result of that).
– Henning Koehler
Nov 12 at 11:30
1
@Rags I know there are better ways of using something other than goto. It is currently the most understandable code to me, to use at this moment in time. I will use something better as the code grows. I'll have a go with what you have said.
– Leon185
Nov 12 at 11:32
2
A classic in Computer Science history: "goto statements considered harmful" by Edgar Dijkstra homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.pdf
– Christian G
Nov 12 at 11:40
1
@ChristianG Not only that - there's a lot by Dijkstra that's worth reading. Some of it is quite unpalatable to some people - but his insight was amazing.
– Rags
Nov 12 at 11:50
You will probably find that not many people like the use of the goto. There tend to be better ways of structuring code so that they're not necessary (they're messy, and hard to control, especially as code grows.) Can I suggest you use a loop around the age entry, and only exit the loop when the conditions are met? Also, might be good to put that sort of operation into a function, as it can be separate from the main flow then, and easier to see/understand.
– Rags
Nov 12 at 11:29
You will probably find that not many people like the use of the goto. There tend to be better ways of structuring code so that they're not necessary (they're messy, and hard to control, especially as code grows.) Can I suggest you use a loop around the age entry, and only exit the loop when the conditions are met? Also, might be good to put that sort of operation into a function, as it can be separate from the main flow then, and easier to see/understand.
– Rags
Nov 12 at 11:29
A very general comment: don't use labels and
goto
statements. They are never necessary, and create hard to read code (and bugs as a result of that).– Henning Koehler
Nov 12 at 11:30
A very general comment: don't use labels and
goto
statements. They are never necessary, and create hard to read code (and bugs as a result of that).– Henning Koehler
Nov 12 at 11:30
1
1
@Rags I know there are better ways of using something other than goto. It is currently the most understandable code to me, to use at this moment in time. I will use something better as the code grows. I'll have a go with what you have said.
– Leon185
Nov 12 at 11:32
@Rags I know there are better ways of using something other than goto. It is currently the most understandable code to me, to use at this moment in time. I will use something better as the code grows. I'll have a go with what you have said.
– Leon185
Nov 12 at 11:32
2
2
A classic in Computer Science history: "goto statements considered harmful" by Edgar Dijkstra homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.pdf
– Christian G
Nov 12 at 11:40
A classic in Computer Science history: "goto statements considered harmful" by Edgar Dijkstra homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.pdf
– Christian G
Nov 12 at 11:40
1
1
@ChristianG Not only that - there's a lot by Dijkstra that's worth reading. Some of it is quite unpalatable to some people - but his insight was amazing.
– Rags
Nov 12 at 11:50
@ChristianG Not only that - there's a lot by Dijkstra that's worth reading. Some of it is quite unpalatable to some people - but his insight was amazing.
– Rags
Nov 12 at 11:50
|
show 1 more comment
3 Answers
3
active
oldest
votes
The problem is that cin >> age
will read an integer into age
(as that's the only thing age
can store), and no matter what integer that is (it's 0 for non-numbers), it will be either bigger or smaller than 18, hence why your else case is never reached.
To detect invalid input, you need to read in a string and then check whether that string represents a valid integer.
Ah! I understand now. Thanks :)
– Leon185
Nov 12 at 11:34
add a comment |
You need to check if the input operation succeeded. The easiest way is:
if (cin >> age)
// now use age
else
// error
Another way which is equivalent:
cin >> age;
if (!cin)
// error
add a comment |
Use std::cin::fail to check wether the user input is an integer.
Secondly, avoid maximum practice using goto
s, as it produces Spaghetti code structure. You can replace the goto
using a while
loop as follows:
while (true)
// code
std::cin >> age;
if (!std::cin.fail() && age >= 18) // if cin not failed && the codition
/* do something */
break; // break the loop!
else if (!std::cin.fail() && age < 18) // if cin not failed && the codition
/* do something goto removeed */
else std::cout << "INVALID INPUT!" << std::endl; /* goto removeed */
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261179%2funable-to-check-for-non-integer-input%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is that cin >> age
will read an integer into age
(as that's the only thing age
can store), and no matter what integer that is (it's 0 for non-numbers), it will be either bigger or smaller than 18, hence why your else case is never reached.
To detect invalid input, you need to read in a string and then check whether that string represents a valid integer.
Ah! I understand now. Thanks :)
– Leon185
Nov 12 at 11:34
add a comment |
The problem is that cin >> age
will read an integer into age
(as that's the only thing age
can store), and no matter what integer that is (it's 0 for non-numbers), it will be either bigger or smaller than 18, hence why your else case is never reached.
To detect invalid input, you need to read in a string and then check whether that string represents a valid integer.
Ah! I understand now. Thanks :)
– Leon185
Nov 12 at 11:34
add a comment |
The problem is that cin >> age
will read an integer into age
(as that's the only thing age
can store), and no matter what integer that is (it's 0 for non-numbers), it will be either bigger or smaller than 18, hence why your else case is never reached.
To detect invalid input, you need to read in a string and then check whether that string represents a valid integer.
The problem is that cin >> age
will read an integer into age
(as that's the only thing age
can store), and no matter what integer that is (it's 0 for non-numbers), it will be either bigger or smaller than 18, hence why your else case is never reached.
To detect invalid input, you need to read in a string and then check whether that string represents a valid integer.
answered Nov 12 at 11:33
Henning Koehler
1,129610
1,129610
Ah! I understand now. Thanks :)
– Leon185
Nov 12 at 11:34
add a comment |
Ah! I understand now. Thanks :)
– Leon185
Nov 12 at 11:34
Ah! I understand now. Thanks :)
– Leon185
Nov 12 at 11:34
Ah! I understand now. Thanks :)
– Leon185
Nov 12 at 11:34
add a comment |
You need to check if the input operation succeeded. The easiest way is:
if (cin >> age)
// now use age
else
// error
Another way which is equivalent:
cin >> age;
if (!cin)
// error
add a comment |
You need to check if the input operation succeeded. The easiest way is:
if (cin >> age)
// now use age
else
// error
Another way which is equivalent:
cin >> age;
if (!cin)
// error
add a comment |
You need to check if the input operation succeeded. The easiest way is:
if (cin >> age)
// now use age
else
// error
Another way which is equivalent:
cin >> age;
if (!cin)
// error
You need to check if the input operation succeeded. The easiest way is:
if (cin >> age)
// now use age
else
// error
Another way which is equivalent:
cin >> age;
if (!cin)
// error
answered Nov 12 at 11:35
John Zwinck
150k16175286
150k16175286
add a comment |
add a comment |
Use std::cin::fail to check wether the user input is an integer.
Secondly, avoid maximum practice using goto
s, as it produces Spaghetti code structure. You can replace the goto
using a while
loop as follows:
while (true)
// code
std::cin >> age;
if (!std::cin.fail() && age >= 18) // if cin not failed && the codition
/* do something */
break; // break the loop!
else if (!std::cin.fail() && age < 18) // if cin not failed && the codition
/* do something goto removeed */
else std::cout << "INVALID INPUT!" << std::endl; /* goto removeed */
add a comment |
Use std::cin::fail to check wether the user input is an integer.
Secondly, avoid maximum practice using goto
s, as it produces Spaghetti code structure. You can replace the goto
using a while
loop as follows:
while (true)
// code
std::cin >> age;
if (!std::cin.fail() && age >= 18) // if cin not failed && the codition
/* do something */
break; // break the loop!
else if (!std::cin.fail() && age < 18) // if cin not failed && the codition
/* do something goto removeed */
else std::cout << "INVALID INPUT!" << std::endl; /* goto removeed */
add a comment |
Use std::cin::fail to check wether the user input is an integer.
Secondly, avoid maximum practice using goto
s, as it produces Spaghetti code structure. You can replace the goto
using a while
loop as follows:
while (true)
// code
std::cin >> age;
if (!std::cin.fail() && age >= 18) // if cin not failed && the codition
/* do something */
break; // break the loop!
else if (!std::cin.fail() && age < 18) // if cin not failed && the codition
/* do something goto removeed */
else std::cout << "INVALID INPUT!" << std::endl; /* goto removeed */
Use std::cin::fail to check wether the user input is an integer.
Secondly, avoid maximum practice using goto
s, as it produces Spaghetti code structure. You can replace the goto
using a while
loop as follows:
while (true)
// code
std::cin >> age;
if (!std::cin.fail() && age >= 18) // if cin not failed && the codition
/* do something */
break; // break the loop!
else if (!std::cin.fail() && age < 18) // if cin not failed && the codition
/* do something goto removeed */
else std::cout << "INVALID INPUT!" << std::endl; /* goto removeed */
answered Nov 12 at 11:46
JeJo
3,9493625
3,9493625
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261179%2funable-to-check-for-non-integer-input%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You will probably find that not many people like the use of the goto. There tend to be better ways of structuring code so that they're not necessary (they're messy, and hard to control, especially as code grows.) Can I suggest you use a loop around the age entry, and only exit the loop when the conditions are met? Also, might be good to put that sort of operation into a function, as it can be separate from the main flow then, and easier to see/understand.
– Rags
Nov 12 at 11:29
A very general comment: don't use labels and
goto
statements. They are never necessary, and create hard to read code (and bugs as a result of that).– Henning Koehler
Nov 12 at 11:30
1
@Rags I know there are better ways of using something other than goto. It is currently the most understandable code to me, to use at this moment in time. I will use something better as the code grows. I'll have a go with what you have said.
– Leon185
Nov 12 at 11:32
2
A classic in Computer Science history: "goto statements considered harmful" by Edgar Dijkstra homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.pdf
– Christian G
Nov 12 at 11:40
1
@ChristianG Not only that - there's a lot by Dijkstra that's worth reading. Some of it is quite unpalatable to some people - but his insight was amazing.
– Rags
Nov 12 at 11:50