Thread: Making sure only one data type is inputted

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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