Hi,
I have some basic code:
cout << "HOW MANY BOTTLES IN THE BASKET? ";
cin >>bottles;
I want to put an if statement to make sure that no letters are entered and only numbers are?
How would i do this?
Thanks guys!
Hi,
I have some basic code:
cout << "HOW MANY BOTTLES IN THE BASKET? ";
cin >>bottles;
I want to put an if statement to make sure that no letters are entered and only numbers are?
How would i do this?
Thanks guys!
Sorry, what would the code look like for this?
>I want to put an if statement to make sure that
>no letters are entered and only numbers are?
You can't control what the user types without delving pretty far into non-standard stuff. You'd essentially need to write your own shell code for handling raw input and the restrict echoing (and storage as well) based on the scan code.
Normally we stick to the standard error handling loop:
#include <ios>
#include <iostream>
#include <limits>
int main()
{
int bottles;
bool done = false;
for ( ; ; ) {
std::cout<<"How many bottles in the basket? ";
if ( std::cin>> bottles )
break;
// Clean up the mess
std::cin.clear();
std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
std::cerr<<"Invalid input\n";
}
std::cout<<"You entered "<< bottles <<'\n';
}
int main()
{
// L33t code goes here
return 0;
}
In other words: Show some effort first. No free homework here
[edit]
Except from our super-moderator apparently :-O
>No free homework here
The homework policy should not be used as a synonym for "I'm too lazy to give a code example". It should only be used when people post an obvious homework assignment and ask for a complete solution without offering any attempt as proof of effort.
Notice that the OP posted a correct and presumably working snippet. Generally homework assignments won't require robust or advanced I/O, so I would be surprised if the assignment consisted completely of "Write a program that restricts the user to typing digits". More likely this is a small part of a larger assignment that the OP has already completed and wants to extend (possibly for extra credit).
Also note that my code technically didn't answer the question. Rather than answering the question as stated, I gave a suitable standard alternative. If my assessment is wrong and this is a complete assignment where the OP was tasked with converting the basic input into something that actually restricts the user to typing digits, the code I provided won't suffice as a solution.
The homework policy should not be used as a synonym for "I'm too lazy to give a code example".
I wouldn't consider myself lazy. The link I posted had an example in it, I could give another example, but that would just silly right?
Also note that my code technically didn't answer the question. Rather than answering the question as stated, I gave a suitable standard alternative. If my assessment is wrong and this is a complete assignment where the OP was tasked with converting the basic input into something that actually restricts the user to typing digits, the code I provided won't suffice as a solution.
Yes yes, I know. I just like messing with you because you always take the bait ;)
>The link I posted had an example in it
An example of what? The link you posted was the homework announcement (and expecting someone to follow the links in that post is akin to expecting someone to search the forum before posting). The code you posted was about as close to worthless as it gets.
>Yes yes, I know.
Suuuure you do. :icon_rolleyes:
I would like to politely disagree. I think that the isdigit is a distraction.
First problem is simple:
if you write THIS: cin>>digits
then you enter 23d you will get a success with 23 in digits and a very difficult time finding out that you entered 23d.
You first want to read the keyboard input into a string. e.g.
std::string str;
getline(std::cin,str);
Then you need to test what you have read to see if it is ok.
// returns 0 on success -1 on failure
int
getNumber(const std::string& A,int& N)
{
std::istringstream testStrm;
testStrm.str(A);
testStrm>>N;
if (testStrm.fail()) return -1;
// make sure whe check the remaining spaces:
char xc=testStrm.get();
while(isspace(xc) && !testStrm.fail())
xc=testStrm.get()
// was the stream completely consumed or did we reach a space.
return (!testStrm.fail()) ? -1 : 0;
}
This code is designed (I hope) to fail on input like 23d5
, 23 45
,but not fail on -394
but to fail on --2323.
This code readily can be put in template form, which works for all types with an operator>> defined.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.