Thread: Making sure only one data type is inputted

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    1

    Making sure only one data type is inputted

    Hi, im new to c++ and im trying to better myself..

    i am trying to make a simple calculator and wanted to be able to make sure the user only can enter a int at some points and double at others. I've tried a few different If statements however they haven't worked so far. Here's some of my code

    Code:
    int quad()
    {
    .. .. //local variables
    .. .. double a,b,c,d,e,f,g;
    .. .. 
    .. .. char cLoop;
    
    
    
    
    
    
    
    
    //function
    .. .. do {cout << "This is the page for the quadratic formula (aX2+bX+c)";
    .. .. cout << "\n Please enter a: ";
    .. .. cin >> a;                                         //Want it to check here
    .. .. cout << "\n Please enter b: ";
    .. .. cin >> b;
    .. .. cout << "\n Please enter c: ";
    .. .. cin >> c;
    .. .. d = pow(b,2) - 4 * a * c;
    .. .. if (d < 0)
    .. .. .. .. {
    .. .. .. .. .. .. cout << "\nNo Real Roots!";
    .. .. .. .. }
    .. .. else if (d = 0)
    .. .. .. .. {
    .. .. .. .. .. .. cout << "One Real Root: ";
    .. .. .. .. .. .. e = -b + ((sqrt(d)) / 2 * a);
    .. .. .. .. .. .. cout << "\n Your Root Is" << e;
    .. .. .. .. }
    .. .. else
    .. .. .. .. {
    .. .. .. .. .. .. cout << "Your Roots Are: ";
    .. .. .. .. .. .. f = -b + ((sqrt(d)) / 2 * a);
    .. .. .. .. .. .. g = -b - ((sqrt(d)) / 2 * a);
    .. .. .. .. .. .. cout << "\nX = " << f;
    .. .. .. .. .. .. cout << "\nX = " << g;
    .. .. .. .. }
    .. .. .. .. cout << "Do you wish to do this again? Y or N.\n";
    .. .. .. .. cin >> cLoop;
    
    
    
    
    .. .. } while (cLoop == 'Y' || cLoop == 'y');
    
    
    
    
    .. .. return 0;
    }
    thanks for any help.
    Last edited by Kinley Adams; 03-16-2012 at 02:08 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You could read the entire line as a string
    Code:
    string line;
    getline(cin, line);
    and then see if it contains a decimal point or not.

    What's with all the dots in your code listing? Try to get rid of them if you post more code.
    Last edited by oogabooga; 03-16-2012 at 03:20 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by oogabooga View Post
    ..
    and then see if it contains a decimal point or not.
    ..
    Do that with stringstreams, btw; not manually.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    2
    double number;
    cin >> number;
    (number == (int)number); //will be true if number is an int, otherwise it will be false.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    That's too brittle. When an operation on cin fails, it will not change the operand.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       double number = 0.0;
       cin >> number;
       if (number == int(number))
          cout << "good\n";
       else
          cout << "bad\n";
       return 0;
    }
    The suggestion only works if the number is not initialized.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    The suggestion only works if the number is not initialized.
    To be fair, you can check to see if the number has been modified:

    Code:
    if(cin >> number) // ...
    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-14-2011, 09:26 PM
  2. Making a Hangman game without using <string> Data type
    By Blazefury5 in forum C++ Programming
    Replies: 3
    Last Post: 04-19-2011, 06:27 AM
  3. store string data as a short or other data type
    By robin2aj in forum C Programming
    Replies: 5
    Last Post: 04-07-2010, 11:02 AM
  4. Help with making a Battle.net type interface...
    By FleX_NuTz in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-31-2002, 09:59 AM
  5. Replies: 0
    Last Post: 10-22-2001, 12:02 AM