![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#21 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Perhaps you should actually compile and run it and see what happens if you enter many things, some of which are not numbers, and some of which are less than zero or greater than 20. This thread doesn't need more horseshit, it reeks as it stands. A disappearing window or reaching "pause" doesn't necessarily represent a good solution, incidentally
__________________
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 |
|
|
|
|
|
#22 |
|
Programmer
Join Date: Oct 2005
Location: Portugal
Posts: 53
Rep Power: 3
![]() |
My example is not the solution to the problem, but can be easily adapted...
|
|
|
|
|
|
#23 | |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
Quote:
. |
|
|
|
|
|
|
#24 | ||
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
Quote:
__________________
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 |
||
|
|
|
|
|
#25 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
The C++ FAQ suggests you do it this way:
while ((std::cout << "Enter a number: ") && !(std::cin >> integer))
{
std::cout << "OK, that's not a number, genius." << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
} |
|
|
|
|
|
#26 | |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#27 | |
|
Professional Programmer
|
This works when i change "int getStrength" to "int main", but when i try to add this in my code it compiles, but still doesnt fix the input problem.. im pretty sure im doing something wrong, would be nice if someone could apply this code below, to the example of my program i am trying to fix a few posts up.
#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;
}
__________________
▄▄▄▄ 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! ▄▄▄▄ |
|
|
|
|
|
|
#28 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Well, what's the problem?
|
|
|
|
|
|
#29 | |
|
Professional Programmer
|
problem
just ignore the backround sounds ..once a letter or any number following "-", this problem starts happening. for example, if i type abc, it will start the problem, or if i type 252- or 1- or 124-, ect, it will start the problem.
__________________
▄▄▄▄ 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! ▄▄▄▄ |
|
|
|
|
|
|
#30 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Where does it say: "you must spend at least one point"?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|