![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Professional Programmer
|
User input
whenever i use cin >> integer;
and input a letter, i end up having to quit the console and rerun the exe for it to exit the continuous loops of words that are flying down the console screen.. i think ppl know what im talking about. I need a new way for users to input a number, but if the user inputs a letter.. the console will just ask for the number again, instead of having to exit and run the exe again. Someone posted this code before, but i couldnt figure out how it's supposed to work.. this is just one of the ways i would like to know how to get the input, but any other way is still good.. // assumes you have a variable called pointsRemaining that // is properly initialized cout << "Please enter your character's strength: "; strength = getStat(pointsRemaining); // get the strength score pointsRemaining -= strength;// reduce the number of points left
__________________
▄▄▄▄ 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! ▄▄▄▄ |
|
|
|
|
|
|
#2 |
|
Expert Programmer
|
try putting this directly before your cin >> statment.
cin.sync(); This clears the data buffer. If that doesn't work there are a few other options.
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#3 | |
|
Professional Programmer
|
Says cin.sync(); is and undeclared function.. any special headers?
__________________
▄▄▄▄ 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! ▄▄▄▄ |
|
|
|
|
|
|
#4 |
|
Expert Programmer
|
#include <iostream>
using namespace std;
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#5 | |
|
Professional Programmer
|
ok, i found out.. tried using a different cin code, cin.get();.. still no luck, i tried putting the cin.sync(); before, and after.. same problem. Thanks anyways
__________________
▄▄▄▄ 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! ▄▄▄▄ |
|
|
|
|
|
|
#6 |
|
Expert Programmer
|
try not using cin... try including
#include <conio.h> //console input output and using getch();
__________________
"When in Rome, Do as the Romans Do" "Beauty is in the eye of the BEER holder" "Save your breath your going to need it for your blow up doll later" SearchLores.org |
|
|
|
|
|
#7 | |
|
Professional Programmer
|
Quote:
That's not kosher ![]() Try something like this: cin >> iInvoiceNumber;
while (cin.fail() || iInvoiceNumber < 1 || iInvoiceNumber > 20000)
{
if (cin.fail())
{
cin.clear();
cin.ignore(10, '\n');
}
cout << "Must be between 1 and 20000" << endl;
cout << "Invoice number: ";
cin >> iInvoiceNumber;
}Obviously that needs to be slightly modified to fit your needs. I stole it out of some code from a class a few semesters ago. |
|
|
|
|
|
|
#8 | |
|
Professional Programmer
|
tried your little fix, doesnt work either.. not sure why nothing is working but heres what im trying to make work..
do{ // This is the strength loop, if you enter a number larger than 20 or less than 1, you will be taken back to this step.
Points = 20;
cout << "Enter your strength: ";
cin >> Str;
if ( Str < 1 ) { // If the input is less than 1, the string below is executed and you will be asked to input a new integer.
cout << "You Must spend at least one point!\n\n";
}
if ( Str > Points - 2 ){ // If the input is greater than the existing points, the string below will be executed and you will be asked to input a new integer.
cout << "You don't have enough points!\n\n";
}
} while ( Str > Points - 2 || Str <= 0 );
cout << "Strength leveled up to " << Str << "!" << endl; // Lets you know that your selected integer was successfully added.
Points = Points - Str; // Adds the selected points on to strength and reduces that same amount from your remaining skill points.
cout << "You have " << Points << " skill points remaining.\n\n";
__________________
▄▄▄▄ 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! ▄▄▄▄ |
|
|
|
|
|
|
#9 |
|
Professional Programmer
|
You forgot to add cin.fail() to your while condition.
|
|
|
|
|
|
#10 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
this should work for anything you throw at it. it gives stupid output if you enter a semicolon or the letter 'g', but any retard that does that deserves what they get.
#include <iostream>
using namespace std;
int main()
{
int i;
while (1)
{
cout<<"number?\n"<<endl;
cin>>i;
if (i<0 || i >18)
{
continue;
}
break;
}
cout<<"\nyou entered "<<i<<"\n"<<endl;
system("pause");
return 0;
}i used 0 to 18 cuz that's D&D standard.
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|