how i can control that ?
May 17, 2011 at 9:30am UTC
cout<<"\nEnter your <student number>:";
cin>>studentnumber;
if user enter alphabetic values program will want user and will ask again user ?
how i can do that
May 17, 2011 at 10:12am UTC
May 17, 2011 at 3:56pm UTC
Hi malikrgc,
this can be tricky, here's something I came up with.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstddef>
using std::string; using std::isdigit;
using std::endl; using std::cin;
using std::cout; using std::size_t;
bool all_numbers(const string& s)
{
const char * str = s.c_str();
bool ret = true ;
for (size_t i = 0; i != s.size(); ++i)
ret = (ret && isdigit(str[i])) == 0 ? false : true ;
return ret;
}
int main()
{
string studentNumber;
cout << "\nEnter your <student number>: " ;
cin >> studentNumber;
while (!all_numbers(studentNumber))
{
cout << "Sorry, you entered >=1 non-digit, please try again!: " ;
cin >> studentNumber;
}
cout << "Thank You!" << endl;
return 0;
}
With an example output as follows:
Enter your <student number>: asdasda213123
Sorry, you entered >=1 non-digit, please try again!: 231231#
Sorry, you entered >=1 non-digit, please try again!: 123123+
Sorry, you entered >=1 non-digit, please try again!: 123123'
Sorry, you entered >=1 non-digit, please try again!: 23123122?
Sorry, you entered >=1 non-digit, please try again!: 123123~
Sorry, you entered >=1 non-digit, please try again!: 31231
Thank You!
Hope this helps, good luck!
May 19, 2011 at 3:03pm UTC
thank you very much dngr88 that help but i have delivered my project it was a part of my project i
May 19, 2011 at 9:42pm UTC
Here's a more advanced solution to your problem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
template <typename T>
T grab(const char * prompt)
{
T thing;
std::istream::iostate old_state = cin.exceptions();
cin.exceptions(std::istream::failbit);
while (true )
{
try
{
cout << prompt;
cout.flush();
cin >> thing;
cin.exceptions(old_state);
return thing;
} catch (std::istream::failure &e) {
cout << "Invalid input. Try again." << std::endl;
cin.clear();
cin.ignore(1024, '\n' );
}
}
return thing;
}
Then just call the function like this:
int i = grab<int >("Enter a number: " );
May 20, 2011 at 9:30pm UTC
@packetpirate
Yes, I like your solution. Although the last return statement is redundant I think.
May 20, 2011 at 10:31pm UTC
No it isn't. You can't put a return statement in just the conditional block, otherwise it will have a condition in which it does not return anything, which is a syntax error.
May 21, 2011 at 4:16pm UTC
@packetpirate
No it isn't. You can't put a return statement in just the conditional block, otherwise it will have a condition in which it does not return anything, which is a syntax error.
I am not sure I agree. You always enter the loop, and either don't throw an exception, in which case you exit the function via the first return, or throw an exception, in which case you recheck the loop condition, which is always true, and enter the loop again. How can you ever get to the last return statement?
Topic archived. No new replies allowed.