Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 24th, 2005, 10:46 PM   #1
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
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:
Originally Posted by Mohamed Jihad
Durka durka!
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!
jayme is offline   Reply With Quote
Old Nov 24th, 2005, 10:54 PM   #2
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
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
Kilo is offline   Reply With Quote
Old Nov 24th, 2005, 11:00 PM   #3
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
Says cin.sync(); is and undeclared function.. any special headers?
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
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!
jayme is offline   Reply With Quote
Old Nov 24th, 2005, 11:03 PM   #4
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
#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
Kilo is offline   Reply With Quote
Old Nov 24th, 2005, 11:06 PM   #5
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
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:
Originally Posted by Mohamed Jihad
Durka durka!
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!
jayme is offline   Reply With Quote
Old Nov 24th, 2005, 11:12 PM   #6
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
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
Kilo is offline   Reply With Quote
Old Nov 25th, 2005, 12:03 AM   #7
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 310
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
Quote:
Originally Posted by Kilo
try not using cin... try including

#include <conio.h>   //console input output

and using

getch();

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.
andro is offline   Reply With Quote
Old Nov 25th, 2005, 12:41 AM   #8
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
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:
Originally Posted by Mohamed Jihad
Durka durka!
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!
jayme is offline   Reply With Quote
Old Nov 25th, 2005, 12:51 AM   #9
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 310
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
You forgot to add cin.fail() to your while condition.
andro is offline   Reply With Quote
Old Nov 25th, 2005, 12:55 AM   #10
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:24 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC