Thread: How to accept only certain text?

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    11

    Question How to accept only certain text?

    Here is my code so far. I just can't get the last 3 lines. I need to accept only text that says Total or Gas. How would I do that? I've looked up and read a lot of stuff but everything I tries doesn't work haha. any help would be great.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int main() {
    
    
    int miles_per_year;
    double gallon_gas;
    double hybrid_cost;
    int hybrid_mpg;
    double hybrid_resale;
    double cost_nonhybrid;
    int non_hybrid_mpg;
    double nonhybrid_resale;
    int buying_crit;
    
    
    
    
        cout <<"Please answer the following questions.\n" << endl;
        
    do
    {
        cout <<"The estimated miles driven per year?" << endl;
        cin >> miles_per_year;
    if(miles_per_year < 0)
        cout <<"Please enter a positive value." << endl;
    } while(miles_per_year < 0);
    
    
    do
    {
        cout <<"The estimated price of a gallon of gas?" << endl;
        cin >> gallon_gas;
    if(gallon_gas < 0)
        cout <<"Please enter a positive value." << endl;
    } while(gallon_gas < 0);
    
    
    do
    {
        cout <<"The cost of a hybrid car?" << endl;
        cin >> hybrid_cost;
    if(hybrid_cost < 0)
        cout <<"Please enter a positive value." << endl;
    } while(hybrid_cost < 0);
    
    
    do
    {
        cout <<"The efficiency of the hybrid car in miles per gallon?" << endl;
        cin >> hybrid_mpg;
    if(hybrid_mpg < 0)
        cout <<"Please enter a positive value." << endl;
    } while(hybrid_mpg < 0);
    
    
    do
    {
        cout <<"The estimated resale value for a hybrid after 5 years?" << endl;
        cin >> hybrid_resale;
    if(hybrid_resale < 0)
        cout <<"Please enter a positive value." << endl;
    } while(hybrid_resale < 0);
    
    
    do
    {
        cout <<"The cost of a non-hybrid car?" << endl;
        cin >> cost_nonhybrid;
    if(cost_nonhybrid < 0)
        cout <<"Please enter a positive value." << endl;
    } while(cost_nonhybrid < 0);
    
    
    do
    {
        cout <<"The efficiency of the non-hybrid car in miles per gallon?" << endl;
        cin >> non_hybrid_mpg;
    if(non_hybrid_mpg < 0)
        cout <<"Please enter a positive value." << endl;
    } while(non_hybrid_mpg < 0);
    
    
    do
    {
        cout <<"The estimated resale value for a non-hybrid after 5 years?" << endl;
        cin >> nonhybrid_resale;
    if(nonhybrid_resale < 0)
        cout <<"Please enter a positive value." << endl;
    } while(nonhybrid_resale < 0);
        
    
    
        cout <<"Would you like results sorted by lowest gas consumption(\"gas\")\n or lowest total cost(\"total\")?" << endl;
        cout <<"Please enter either \"gas\" or \"total\" for how results are to be sorted." << endl;
        cin >> buying_crit;
    
    
    
    
    system("pause");
    return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I'm a 'C' person, and don't know anything about C++ other than what I've read on here - but I would think you should start by using the 'string' data type in lieu of an integer. If I'm wrong, someone here will correct me.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    1) you can't store "gas" or "total" in a int variable.

    2) Use std::string Don't forget to include <string>
    Code:
    std::string test;
    std::cout << "Enter 'gas' or 'total'\n";
    std::getline(std::cin, test);
    for ( std::string::iterator it = test.begin(); it != test.end(); ++it )
    {
     (*it) = tolower((*it));
    }
     
    if ( test.compare("gas") == 0 )
    {
     std::cout << "GAS!!!\n";
    }
    else if ( test.compare("total") == 0 )
    {
     std::cout << "Total!!!\n";
    }
    else
    {
     /* Did not match */
    }
    The for loop changes the string to all lowercase, since the std::string.compare() function is case sensitive.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    for ( std::string::iterator it = test.begin(); it != test.end(); ++it )
    {
     (*it) = tolower((*it));
    }
    Or just
    Code:
    std::transform(test.begin(), test.end(), test.begin(), &std::tolower);
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. accept()
    By ~Kyo~ in forum Networking/Device Communication
    Replies: 11
    Last Post: 11-28-2009, 08:57 PM
  2. could not accept socket
    By Elkvis in forum Linux Programming
    Replies: 3
    Last Post: 02-26-2008, 08:42 AM
  3. accept() fails
    By Desolation in forum Networking/Device Communication
    Replies: 3
    Last Post: 05-16-2006, 07:37 AM
  4. accept() Hangs
    By Epo in forum Networking/Device Communication
    Replies: 14
    Last Post: 09-09-2005, 11:53 AM
  5. accept()
    By char in forum C Programming
    Replies: 0
    Last Post: 05-30-2002, 02:22 PM