Problems with string variables in an IF statement

This is a discussion on Problems with string variables in an IF statement within the C++ Programming forums, part of the General Programming Boards category; Hi, I've just stated learning C++ and i've been reading through the tutorials on this site and messing about with ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72

    Problems with string variables in an IF statement

    Hi, I've just stated learning C++ and i've been reading through the tutorials on this site and messing about with a few programs, but I am having a problem with trying to use an IF statement which gives a certain message depending on whether the user enters 'yes' or 'no' for input.

    The program i want to write is like this:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
       string = "input";                  // It won't even compile this part, but some tutorials said it was legal
      
      cout<<"Do you want a cookie.\n";  //Just as an example
      cout << input;                    
      cin.ignore();
      if ( input == 'yes' ) {
           cout<<"Here is your cookie.\n";
    }
      else if ( input == 'no' ) {
           cout<<"O.K, no cookie for you.\n";
    }
      cin.get();              
    }
    Google didn't find anything even after reading a few tutorials on string variables and the FAQ on this site didn't provide any help either as I just got confused with the usage of the "std::" stuff.

    Any help would be greatly appreciated.

    Calef13

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    string is a c++ class, not an object of that class.
    Code:
    string s = "Input"
    Code:
     if ( input == 'yes' ) {
    string literals, like yes, must be enclosed in double quotes, not single quotes. Also, you have not declared variable input.
    Code:
    string input;
    cin >> input;
     if ( input == "yes" ) {
    Last edited by Ancient Dragon; 12-30-2005 at 09:16 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    37
    In addition to what Ancient Dragon said.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main () {
    
            // your code
            cin.get();
            return 0;
    
    }

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    FYI, as long as you specify the int return type in the main prototype, the "return 0;" is assumed in the latest edition of the standard.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    37
    FYI, as long as you specify the int return type in the main prototype, the "return 0;" is assumed in the latest edition of the standard.
    Very interesting... I'll need to get myself a copy... Cheers!

  6. #6
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72

    Re:

    Thanks very much guys it's working, finally

    Calef13

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. string & eof handling problems
    By spudval in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2007, 11:46 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM

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