Restricting data types?

This is a discussion on Restricting data types? within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm new to programming and need some help with a program i wrote. I have written the program as ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Location
    California
    Posts
    1

    Restricting data types?

    Hi,

    I'm new to programming and need some help with a program i wrote.

    I have written the program as follows:

    Code:
    #include <iostream>
    
    using namespace std;
    
    double ftoc ( double f );
    
    int main()
    {
        double f; double c;
    
        for ( int y = 1; y != 0 && y == 1; y )
        {
        cout<<"Enter degrees in Fahrenheit: ";
        cin>> f;
        cin.ignore();
    
        cout<< f <<" degrees Fahrenheit is "<< ftoc ( f ) <<" degrees Celsius\n\n";
    
        cout<<"Do you want to convert again (yes = 1,no = 0): ";
        cin>> y;
        cin.ignore();
        while ( y != 1 && y != 0 )
           {
           cout<<"You need to enter either a 1 or 0\n\n";
           cout<<"Do you want to convert again (yes = 1,no = 0): ";
           cin>> y;
           cin.ignore();
           }
        cout<<"\n";
        }
    }
    
    double ftoc ( double f )
    {
        return (5.0/9.0)* (f - 32.0);
    }
    I noticed that when my program asks to enter in the degrees fahrenheit, if you enter in anything other than a number it goes into an infinite loop. Now, I want to protect against this by adding a loop asking to enter a number, but how do I go about doing this? I assume you would have to restrict data types, but I'm not sure how that is done.

    Also another question I have is if its possible to insert a degree symbol in the program? If so, then how do you insert it?
    Any help is appreciated.

    Thanks
    Last edited by zeno86; 04-30-2006 at 05:11 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    Code:
    double f;
    while (!(cin >> f))
    {
      cin.clear();
      cin.ignore(1000, '\n');
      // Prompt about the bad input and ask for input again.
    }
    If you also want to error out when the user types some good input followed by bad input (e.g. "76.4 is the answer\n"), then this code will do that:
    Code:
    double f;
    while (!(cin >> f) || cin.get() != '\n')
    {
      cin.clear();
      cin.ignore(1000, '\n');
      // Prompt about the bad input and ask for input again.
    }
    >> possible to insert a degree symbol in the program?
    Perhaps:
    Code:
    cout << ''; // alt-167

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extending basic data types.
    By nempo in forum C++ Programming
    Replies: 23
    Last Post: 09-25-2007, 03:28 PM
  2. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Using enumerated data types
    By SXO in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2001, 06:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21