![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 | |
|
Professional Programmer
|
i added your code to that, but it didnt work.. was hoping someone could show me how i would change the segment i posted above to work with what you posted.
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
|
|
|
|
|
|
#12 |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
I'm just a newb so don't mind me if I'm wrong, but shouldn't the above look more like:
int i;
while (1)
{
cout<<"number?\n"<<endl;
cin>>i;
if (i<0 || i >18)
{
break;
}
} |
|
|
|
|
|
#13 | |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
Quote:
And now I've just quoted myself trying to edit my original post too..lol.:o |
|
|
|
|
|
|
#14 |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
OK, in a bid to make up for my last two ridiculous posts I've come up with the folowing:
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
char input[10];
int i;
while(1)
{
cout<<"number?\n"<<endl;
cin.getline(input,10);
i = atoi(input);
if ((i<=0) || (i>=20))
{
cout << "\nERROR: Invalid selection\n";
cout << "Press any key to retry. . .\n\n";
cin.get();
continue;
} else
{
break;
}
}
cout<<"\nyou entered "<<i<<"\n"<<endl;
system("pause");
return 0;
}Note you will have to include 'cstdlib' for atoi() to work. |
|
|
|
|
|
#15 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
i thought my solution was stupid. i'm sure there's a better way to check for type compatibility.
cache's most recent solution seems to work the best.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#16 |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
One problem I have noticed though, atoi() seems to convert all non integer values (a, b, c, ect) to 0. So you wouldn't be able to use anything that =0.
|
|
|
|
|
|
#17 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
There is a lot of voodoo and bullshit being recommended in this thread, which is a Bad Thang. The perils of input have been addressed on the forum, but let's cover them again, rather than recommend a nasty search.
Incidentally, Andro is on the right track, with the right ideas. Kilo, you should not recommend using non-portable code when such isn't necessary. You rarely can control what the user provides. You only have the chance to validate it and clean it up. As an aid in that process, most input functions provide return values. One checks these to see if the user has managed to break the input mechanism. It's easy to do. If you're asking for input and not checking status you are either new (the most common case) or a fool (not uncommon enough). Cin is a collection of methods. The extraction operator (>>) doesn't return status directly (it returns the stream), so one must use additional methods. Among these are .good (), .bad () and .fail (). Some input functions, among them cin, offer to do more for you than merely gather keystrokes. They offer conversion. They know how to convert, every bit as well as atoi knows how to convert, and most likely, better. Some things aren't convertible; in a conversion situation they will either break something or produce garbage. One must check for these things. The example procedure illustrates some of this. If one is really picky, one distinguishes between bad (severe), fail, eof, and such. #include <iostream>
using namespace std;
int getStrength ()
{
unsigned int strength;
unsigned int tries = 0;
cout << endl << "Enter strength: ";
while (true)
{
if (tries > 3)
{
cout << "You just can't get it, can you?" << endl;
return -1;
}
// The following clears the error bits for the stream,
// in case some usage broke it.
cin.clear ();
// Adjust the stream related pointers. For istreams,
// this effectively clears content. It's possible for
// it to fail. Some istreams might not have buffers;
if (cin.sync () == -1) return -1;
cin >> strength;
if (!cin.good ()) cout << "Invalid input; try again: ";
else if (strength > 20) cout << "Invalid number, try again: ";
else return strength;
tries++;
}
return -1;
}
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#18 |
|
Programmer
Join Date: Oct 2005
Location: Portugal
Posts: 53
Rep Power: 3
![]() |
What the hell...
Just do this: int main()
{
int x;
while ( cin >> x )
cout << x << endl;
cout << "You not entered a number..."<< endl;
system("PAUSE");
return 0;
} |
|
|
|
|
|
#19 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Because that doesn't get the job done....
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#20 |
|
Programmer
Join Date: Oct 2005
Location: Portugal
Posts: 53
Rep Power: 3
![]() |
Why is that?
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|