Thread: What on earth is wrong?

  1. #1
    Registered User stuart_cpp's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    31

    What on earth is wrong?

    What on earth is wrong in this code?
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        int money;
        
        string prompt("Input how much pence without the pence sign you want to input\n"),
               input;
        
    
        cout << "Input how much pence without the pence sign you want to input\n";
        if ( money == 50 ) {
             cout << "Enter the key number of the drink you want" << endl;
             cout << "1 Coke \n 2 Diet Coke \n 3 Fanta \n 4 Sprite" << endl;
             }
             getline( cin, input);
             cout << "Please wait, your product is vending" << endl;
             if ( input > 9 ) {
                  cout << "Invalid input" << endl;
                  }
        else if ( money > 50 ) {
             cout << "Enter a key number of the drink you want" << endl;
             }
             cin >> input;
             }
             if ( input ) {
                  cout << "Invalid input" << endl;
                  {
             if ( input.length() ==  2 ) {
                  cout << "Please wait, your product is vending" << endl;
                  }
             else if ( input.length() < 10 ) {
                  cout << "Please wait, your product is vending and here is your" << input - 50 << " change" << endl;
                  }         
             return 0;
    }
    And the error message was.....
    no match for 'operator>' in 'input > 9'

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Well, since input is a string and 9 is an integer, is it really any surprise?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Code:
    input > 9
    input is a std::string, 9 is an integer. Perhaps you want to write:
    Code:
    input.length() > 9
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Input was declared as a string and you are trying to compare it to a literal int. Both types are not convertible to each other

    Place 9 between double quotes.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Really, the > operator should be the least of your worries. Those brackets are mismatched all over the place.
    Code:
    int main()
    {
        int money;
        
        string prompt("long string that never gets used"),
               input;
        
    
        cout << "Input...";
        if ( money == 50 ) //unlikely, since you haven't set money
             {
             cout << "Enter..." << endl;
             cout << "1 Coke..." << endl;
             }
             getline( cin, input);
             cout << "Please wait..." << endl;
             if ( input > 9 ) {
                  cout << "Invalid input" << endl;
                  } else if ( money > 50 )
             {
             cout << "Enter..." << endl;
             }
             cin >> input;
             } //here's a random bracket ending our main function
             if ( input ) {
                  cout << "Invalid input" << endl;
                  {
             if ( input.length() ==  2 ) {
                  cout << "Please wait..." << endl;
                  }
             else if ( input.length() < 10 ) {
                  cout << "Please wait..." << input - 50 << " change" << endl;
                  }         
             return 0;
    }
    //whoops, we've still got a green bracket
    //and our main function to close
    Last edited by pianorain; 06-12-2006 at 09:27 AM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Registered User stuart_cpp's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    31
    Thanks! What a stupid mistake I made!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  2. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  3. February 1, 2019...
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 98
    Last Post: 08-03-2002, 07:24 AM
  4. true love
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 57
    Last Post: 07-17-2002, 05:04 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM