🔒 Locked
Data-type restriction?!
Advertisement
Original Post
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!
Oh!
sorry, since MOST people here use C/C++, I forgot to post that..
anyway, I use C++.
sorry, since MOST people here use C/C++, I forgot to post that..
anyway, I use C++.
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 );
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 );
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!!
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!!
Heh!
that''s much simpler for me!
I''ll use that now and recompile!
Thanks for the help
that''s much simpler for me!
I''ll use that now and recompile!
Thanks for the help
sheesh!!
that keeps on displaying "Please enter an integer!" forever!
Here''s how I put your code in mine:
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;
}
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement