Skip to main content
gamedev.net
🔒 Locked

Data-type restriction?!

Started by nectron101 May 1, 2004 at 5:46 PM 7 replies 750+ views
Original Post
nectron101
nectron101
Hello again my faithful friends who always help me! LOL I''m one step away from making my first "serious" program working perfectly.. the program lasks you for something like this: 3 x 2 = ? if you enter an int, it''s alright.. But how can I prevent the user from entering letters or a space or a return key?? Thank you VERY much guys!
SiCrane
SiCrane
Do you want to mention what programming language you''re using?
nectron101
nectron101
Oh!
sorry, since MOST people here use C/C++, I forgot to post that..

anyway, I use C++.
fyhuang
fyhuang
Well, first you will have to get the string that the user inputs from stdin. Let''s say you stored this in a char * array:

char input[64];

Now, to check if the user actually entered something:

if ( input[0] == ''\n'' || strlen( input ) == 0 )
{
// Blank string
}

To check for letters:

index = 0;
while ( index < 64 )
{
if ( !( input[index] >= ''0'' && input[index] <= ''9'' ) && input[index] != ''\n'' )
{
// Something besides number and return entered
}
index++;
}

And then, finally:

int number;
sscanf( input, "%d", number );
- fyhuang [ site ]
nectron101
nectron101
Damn!

it''s simple, and also a little long.. "guess I''ll leave that for the GUI version!

but that really helped me understand how to do it, THANKS MAN!!
Zipster
Zipster
If you ask for an integer, and the user fails to enter an integer, then cin will set it''s failure. You simply loop until cin doesn''t fail:


int myInt = 0;

cin >> myInt;

while(cin.fail())
{
cout << "You bastard, enter an integer! ";
cin.clear();
cin >> myInt;
}
nectron101
nectron101
Heh!
that''s much simpler for me!

I''ll use that now and recompile!

Thanks for the help
nectron101
nectron101
sheesh!!

that keeps on displaying "Please enter an integer!" forever!

Here''s how I put your code in mine:


/********************************
* Name: Multiplication Test *
* Author: Amer Al-Sadiq *
* Email: nectron101@hotmail.com *
* Date: April-26th-2004 *
********************************/

#include <iostream> //for cout and cin.
#include <cstdlib> //for system commands.
#include <fstream>
using namespace std;

int main()
{
int main_number, ans;
int counter = 0;
char name[20];
cout << "\t\t\t Multiplication Test" << endl; //header
cout << "\t\t\t By: Amer Al-Sadiq." << endl << endl; //header

cout << "Please enter your name: ";
cin.getline (name, 20); //captures the name entered
cout << "Now, let''s beggin! " << endl << endl;

cout << "\nChoose a multiplication number:" << endl << endl;
cout << "(2),(3),(4),(5),(6),(7),(8),(9)" << endl;
cin >> main_number;

for ( int n=2; n<10; n++)
{
cout << main_number << "x" << n << "= ";
cin >> ans;
while(cin.fail()) //HERE IS THE CODE BLOCK
{
cout << "Please enter an integer!";
cin.clear();
cin >> ans;
}
if (ans==(main_number*n))
{
counter++;
cout << "Correct! " << endl << endl;
}
else
{
cout << "Wrong! " << endl;
cout << "The correct answer is: " << (main_number*n) << endl << endl;
}
}
cout << "Operations are done!" << endl;
cout << "Check your scores sheet!" << endl;

ofstream file("myscore.txt", ios::app);
file << "This is your score sheet " << name << ":" << endl;
file << "Multiplication Number: " << main_number << endl;
file << "Correct answers: " << counter << endl << endl;
file.close();
cout << "Have a nice day " << name << endl;

system ("pause");
return 0;
}
Zipster
Zipster
Oops, I forgot, you might also need to call cin.sync() after you call cin.clear().

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.