Check for bad user input in c++ program

i need a little help checking for bad user input, anyone care to help?

Page 1 of 1

7 Replies - 7961 Views - Last Post: 19 March 2010 - 12:37 PM Rate Topic: -----

#1 pawel453   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 25-November 08

Check for bad user input in c++ program

Posted 18 March 2010 - 06:13 PM

Good evening everybody, I am attempting to write a program that multipies fractions.
to do this i need the user to input a fraction in an n/d format, the problem is that i don't
know HOW to read the number(s) Before the "/" and put them into an int value numA, and store
everything AFTER the "/" into int value numB... What kind of function would i have to create
in order to look for "/" and seperate the two numbers.
#include <iostream>
using namespace std;

void main(){
        int numA, numB;
        cout << "Please type in a fraction in the n/d format";
        cin >> //I don't know what kind of data type i should put this in( a char array?)
        cout << "PLease type in the second fraction again, in the same format";
        cin >> //Again, I am unsure what to use here
        // This is where the action would take place and i need help here and no where further.
        // Any help or hints or clues or very much apreciated, THANK YOU!





Is This A Good Question/Topic? 0
  • +

Replies To: Check for bad user input in c++ program

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,235
  • Joined: 14-September 07

Re: Check for bad user input in c++ program

Posted 18 March 2010 - 06:24 PM

Option 1: Ask for two values w/o the slash

cout << "Enter two numbers (ex. 1 2): ";
cin >> numA >> numB; //this will ignore whitespace



Option 2: Create a Fraction class


Option 3: Read in a whole line/string and parse



I can give you more detail, just need to know what path you want to take.
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Check for bad user input in c++ program

Posted 18 March 2010 - 06:25 PM

You can read the user's input as a string and use the string::find_first_of function to locate the '/' character. Having found that, you can use the string::substr function to get substrings consisting of the characters before and after the '/', and then you can use the standard C function atoi (in <cstdlib>) to convert those strings into ints.

Here, you can read about the C++ string functions, and here you can read about the C Library. Click on the link to cstdlib to read about atoi.
Was This Post Helpful? 0
  • +
  • -

#4 taylorc8   User is offline

  • B&

Reputation: 150
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Check for bad user input in c++ program

Posted 18 March 2010 - 06:28 PM

Well, maybe using the string class, (#include <string>) have a look here: http://www.cplusplus.../string/string/

And you can use stringstream to parse the string to an integer. (#include <sstream>)

have a look here: http://www.cplusplus...m/stringstream/
and: http://www.fredosaur...am-example.html

or use atoi() to convert a C string to an integer.

Also, cin and cout are objects, you can test to see if they are good or bad.

int input=0;

while(cin)
{
cout << "Enter: ";
cin >> input; // while cin is good..
cout << endl;
}



If you use the getline function on a string class object I don't think cin will ever encounter an error, at least under normal conditions:
like so:
string str;
getline(cin,str); // get from cin



and I have an example of parsing using stringstream somewhere, I would hope you can google it though..
Was This Post Helpful? 0
  • +
  • -

#5 Zer0pain   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 55
  • Joined: 13-March 10

Re: Check for bad user input in c++ program

Posted 18 March 2010 - 06:36 PM

Hmm this should help. You can't type in integers in N/D format. You'll have to enter them in seperately.

#include <iostream>
#include <string>
using namespace std;

void main(){
float numA;
float denA; //den being the denominator
float numB;
float denB;
float fractionA;
float fractionB;
float Answer;

        cout << "Please type in the numerator for the first fraction" << endl;
        cin >> numA;
        cout << "Please type in the denominator for the first fraction" << endl;
        cin >> denA;
        cout << "Please type in the numerator for the second fraction" << endl;
        cin >> numB;
        cout << "Please type in the denominator for the second. fraction" << endl;
        cin >> denB;
// Then it should be a simple arithmetic problem

fractionA = numA / denA;
fractionB = numB / denB;
Answer = fractionA * fractionB;

cout << "The answer is " << Answer << endl;



I haven't checked it out and I'm sure there are some errors but it'd be something down that alley. Definitely some errors in there, but you get the idea :)

EDIT: Fixed the code, all the variables must be float or else they cannot multiply. Hope I helped!

This post has been edited by Zer0pain: 18 March 2010 - 06:44 PM

Was This Post Helpful? 0
  • +
  • -

#6 pawel453   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 25-November 08

Re: Check for bad user input in c++ program

Posted 19 March 2010 - 12:18 PM

I have to create a fraction class (f1, f2, f3 for answer)
and the input method has to be so the user types in something
such as 12/54 and then a second number as 1/2 and the output has
to be 12/108, in that format. The only problem i have is how to
get the input (12/54) into numA=12 and numB=54.
(12 is any numerator and 54 is any denopinator)
Was This Post Helpful? 0
  • +
  • -

#7 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Check for bad user input in c++ program

Posted 19 March 2010 - 12:34 PM

First of all, start by including <string>, as we are going to accept the input as a std::string type. The second #include that you need, is <sstream>. We will be needing this for std::istringstream (needed to convert string to integer). Here's the snippet on how to convert a string to integer, using stringstream: http://www.dreaminco.../snippet694.htm . Wrapped into a function, that snippet could look like this:
int str_to_int(const std::string &input)
{
    std::istringstream istr(input);
    int ret_val;
    istr >> ret_val;

    return ret_val;
}


This is the only function (besides main()) that you need. Moving to main() function: you have to prompt the user to enter the expression (in format X/Y) as you want. Use "my_input.find('/')" to find the position of the slash '/' in your input string. If the slash couldn't be found, "find" function returns "-1", which is equivalent with "std::string::npos" or "my_input.npos". If the return value is "npos", then the expression format was not good, so you post an error message and exit the program (or prompt the user to enter another expression). Now, there's two more things to do: using "my_input.substr()", you have to extract the data before that '/' and after it. Assuming that the position of your '/' is stored in an integer variable called "slash_pos", the .substr methods should look like this:
// Creates a substring starting with position 0 -> slash_pos - 1
int X = str_to_int(my_input.substr(0, slash_pos));
// Creates a substring starting with position slash_pos + 1 -> end of string
int Y = str_to_int(my_input.substr(slash_pos + 1));


Lastly, print X and Y. You should be able to "translate" what I said, into valid C++ code.
More about std::string functions: http://www.cplusplus.../string/string/

This post has been edited by sarmanu: 19 March 2010 - 12:37 PM

Was This Post Helpful? 0
  • +
  • -

#8 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Check for bad user input in c++ program

Posted 19 March 2010 - 12:37 PM

What you could do is read a string for input and parse the string.

Something like this:
string input = "";
cout << "Enter a fraction: ";
getline(cin, input);

string temp = "";

for(int i = 0; i < input.length(); i++)
{
  if(isdigit[i])
  {
    temp += input[i];
  }
  else
  {
    temp += " ";
  }
}

// now temp = NUMBER <SPACE> NUMBER
stringstream ss;
ss.str(temp); // store temp into the stream

int a, b;
ss >> a >> b;

// Now store a and b into the fraction class


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1