Verry new at programing.

This is a discussion on Verry new at programing. within the C++ Programming forums, part of the General Programming Boards category; OK I just started my very first C++ class and I am working on a lab. Here is the code ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    17

    Verry new at programing.

    OK I just started my very first C++ class and I am working on a lab. Here is the code I wrote and a break down of what I'm trying to do.

    My code is doing what I want it to do. However what I am trying to figure out is if the users enters in anything other then a number I want it to spit out an error that say's "invalid input"

    I've tried doing it with an else statemnt but can't get it it to work. Here is my code.
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        //declare variables
        int Temp1 = 0;
        int Temp2 = 0;
        int AverageTemp = 0;
        
        cout << "Follow the steps to find the average temp between two cities." <<endl;
        cout <<endl;
        cout << "Enter temp for 1st city: ";
        cin >> Temp1;
        cout << "Enter temp for 2nd city: ";
        cin >> Temp2;
       
        
          if(Temp1 >= 120)
    {
        cout << "Error Temp1 is WAY TO HOT! Temp can not exceed or be equal to 120." <<endl;
        cout << "Please re-enter temp for 1st city: ";
        cin >> Temp1;
        }
        
        if(Temp2 >= 120)
    {
        cout << "Error Temp2 is WAY TO HOT! Temp can not exceed or be equal to 120." <<endl;
        cout << "Please re-enter temp for 2nd city: ";
        cin >> Temp2;
        
        }
        if (Temp1 < 120 || Temp2 < 120)
        
    {
        AverageTemp = (Temp1 + Temp2) / 2;
        cout << "Average temp: " << AverageTemp <<endl;
            }
    
        if (Temp1 > Temp2)
        cout <<"City 1 is warmer then city 2." <<endl;
        if (Temp1 < Temp2)
        cout <<"City 2 is warmer then City 1." <<endl;
        if (Temp1 == Temp2)
        cout <<"Both temps are equal." <<endl;
        
        
    
    
    
        
        system("pause");
        return 0;
        
        
    }
    Thanks in advance.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    It ain't rocket science.
    Code:
    if (such-and-such-is-true)
    {
       // Do code in this block
    }
    else
    {
       // Do code in this block
    }

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    17
    Thank you for your response.

    I'm having a hard time wraping my head around how to tell it to say "invalid input" for anything other then a number.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    There are several ways. My favorite is:
    Code:
    std::string input;
    std::getline(input, std::cin);
    int x;
    try
    {
        x = boost::lexical_cast<int>(input);
    }
    catch(boost::bad_lexical_cast&)
    {
        // User entered bad input
    }
    It isn't the easiest thing to do this, and this example assumes you are somewhat familiar with calling template functions (you may have used such syntax in std::vector<T>, for example) and exceptions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Banned
    Join Date
    Jan 2009
    Posts
    30
    With all due respect to Elysia, perhaps if you describe your help as not very useful without a deeper understanding of a moderately advanced subject in C++ programming, perhaps it is unwise advice to give to a beginner. Also, be careful using RTTI. It can often be avoided.

    To gec5741, you may also wish to investigate switch statements.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    As I said, it is not the most trivial task.
    It is my favorite solutions - and there are others, but they may involve complexity, as well. In their own way.

    Another solution might be:
    Code:
    int x;
    if (!(std::cin >> x))
    {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<int>::max());
        // User entered bad input
    }
    Not sure if this is 100% correct.
    Last edited by Elysia; 01-15-2009 at 01:13 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    17
    Thank you for your suggestions. I am as I said a begginer so what you suggest may be a bit advanced for this project. It's my second lab in this class

    I will look into using a switch statements.

    thanks.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,112
    Quote Originally Posted by sphynxter
    Also, be careful using RTTI. It can often be avoided.
    Yes, but where is RTTI used? :|

    Quote Originally Posted by Elysia
    Not sure if this is 100% correct.
    I think cin.clear() should come before the cin.ignore().

    EDIT:
    Quote Originally Posted by gec5741
    I will look into using a switch statements.
    I do not think that switch statements are an appropriate solution to the problem of checking if the user entered invalid input that is not a number. Elysia's first suggestion requires a useful but non-standard library, and the second suggestion uses only the standard library and is on the right track.
    Last edited by laserlight; 01-15-2009 at 01:16 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    You also may want to improve on your indentation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    17
    Im unlcear on what the rules for indentations are really?

    Is this any better?

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        //declare variables
        int Temp1 = 0;
        int Temp2 = 0;
        int AverageTemp = 0;
        
        
        cout << "Follow the steps to find the average temp between two cities." <<endl;
        cout <<endl;
        cout << "Enter temp for 1st city: ";
        cin >> Temp1;
        cout << "Enter temp for 2nd city: ";
        cin >> Temp2;
       
         
        if(Temp1 >= 120)
    {
        cout << "Error Temp1 is WAY TO HOT! Temp can not exceed or be equal to 120." <<endl;
        cout << "Please re-enter temp for 1st city: ";
        cin >> Temp1;
        }
        
        if(Temp2 >= 120)
    {
        cout << "Error Temp2 is WAY TO HOT! Temp can not exceed or be equal to 120." <<endl;
        cout << "Please re-enter temp for 2nd city: ";
        cin >> Temp2;
        }
        else
    {
        AverageTemp = (Temp1 + Temp2) / 2;
        cout << "Average temp: " << AverageTemp <<endl;
            }
    
        if (Temp1 > Temp2)
        cout <<"City 1 is warmer then city 2." <<endl;
        if (Temp1 < Temp2)
        cout <<"City 2 is warmer then City 1." <<endl;
        if (Temp1 == Temp2)
        cout <<"Both temps are equal." <<endl;
        
        
    
    
    
        
        system("pause");
        return 0;
        
        
    }
    Last edited by gec5741; 01-15-2009 at 01:43 PM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,112
    An example:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
        //declare variables
        int Temp1 = 0;
        int Temp2 = 0;
        int AverageTemp = 0;
    
        cout << "Follow the steps to find the average temp between two cities." << endl;
        cout << endl;
        cout << "Enter temp for 1st city: ";
        cin >> Temp1;
        cout << "Enter temp for 2nd city: ";
        cin >> Temp2;
    
        if (Temp1 >= 120)
        {
            cout << "Error Temp1 is WAY TO HOT! Temp can not exceed or be equal to 120." << endl;
            cout << "Please re-enter temp for 1st city: ";
            cin >> Temp1;
        }
    
        if (Temp2 >= 120)
        {
            cout << "Error Temp2 is WAY TO HOT! Temp can not exceed or be equal to 120." << endl;
            cout << "Please re-enter temp for 2nd city: ";
            cin >> Temp2;
        }
        else
        {
            AverageTemp = (Temp1 + Temp2) / 2;
            cout << "Average temp: " << AverageTemp << endl;
        }
    
        if (Temp1 > Temp2)
            cout << "City 1 is warmer then city 2." << endl;
        if (Temp1 < Temp2)
            cout << "City 2 is warmer then City 1." << endl;
        if (Temp1 == Temp2)
            cout << "Both temps are equal." << endl;
    
        system("pause");
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C programing for DNA...
    By S16 in forum C Programming
    Replies: 9
    Last Post: 04-29-2009, 10:25 AM
  2. Programing microcontrollers in c
    By gorabhat in forum C Programming
    Replies: 2
    Last Post: 09-09-2008, 11:40 AM
  3. studying c++ programing ... concern
    By azsquall in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-03-2008, 05:30 PM
  4. Human brain and programing
    By avgprogamerjoe in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 08-27-2007, 05:48 PM
  5. Database programing in MS Visual
    By sunburnbyRA in forum C++ Programming
    Replies: 4
    Last Post: 05-23-2003, 05:44 PM

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