[noobquestion]Problem with if and else if

This is a discussion on [noobquestion]Problem with if and else if within the C++ Programming forums, part of the General Programming Boards category; Yesterday I started with c++, everything went well with a few good tutorials but right now I can't go on! ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    13

    Thumbs down [noobquestion]Problem with if and else if -- Second problem

    Yesterday I started with c++, everything went well with a few good tutorials but right now I can't go on! I have a problem I can't fix! Made a little talking programm...
    Here is the code:

    Code:
    // cin with strings
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
      string mystr;
      cout << "Let's have a chat!\n";
      cout << "What's your name?\n";
      getline (cin, mystr);
      cout << "Hello " << mystr << ".\n";
      cout << "How old are you?\n";
      getline (cin, mystr);
      cout << "So you are " << mystr << " years old?\n";
      cout << "Whats the name of the town where you live in?\n";
      getline (cin, mystr);
      cout << "In which country can I find " << mystr << " ?\n";
      getline (cin, mystr);
      cout << "Ah I see, I never in " << mystr << " but I would like to!\n";
      cout << "This little programm is made in c++, do you use c++ to?";
      getline (cin, mystr);
      if (mystr == yes)
      {
    	cout << "Well, that's great, than you should know how this programm works!";
      }
      else if (mystr == no)
      {
    	  cout << "I can recommend you to learn c++, it's really great!";
      }
    
    
      
      return 0;
    }
    And these are the errors:
    #####\visual studio 2005\projects\talkingwithpc\talkingwithpc\source1. cpp(23) : error C2065: 'yes' : undeclared identifier
    #####\visual studio 2005\projects\talkingwithpc\talkingwithpc\source1. cpp(27) : error C2181: illegal else without matching if
    #####\visual studio 2005\projects\talkingwithpc\talkingwithpc\source1. cpp(27) : error C2065: 'no' : undeclared identifier

    Many thanks to the ones who take a look at this topic, and more to the ones who make me solving my problem.

    BTW: Mery chrismas and a happy new year!
    Last edited by HJvK; 12-28-2005 at 03:24 AM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you need to put quotes around yes and no because they are string literals.
    Code:
    if (mystr == "yes")

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    13
    Thanks it's working...
    Wow, I advanced quite fast... Hope I will advance further in programming with c++ with this insane speed

  4. #4
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    besides you shouldn't use "string"s, better to use "char string[xxx] " it will help you understand strings,
    and later use strings, once you know how they work...
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    13
    You mean:
    Code:
    char string [string1];
      cout << "Let's have a chat!\n";
      cout << "What's your name?\n";
      getline (cin, string1);
      cout << "Hello " << string1 << ".\n";

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    besides you shouldn't use "string"s, better to use "char string[xxx] " it will help you understand strings,
    and later use strings, once you know how they work...
    That's debatable. HJvK, since you are already working with some tutorials, it might be better to continue and see where they lead you rather than jumping around and never really learning anything well.
    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

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    besides you shouldn't use "string"s, better to use "char string[xxx] " it will help you understand strings,
    and later use strings, once you know how they work...
    By all means start with string types rather than null terminated char arrays.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    13
    well, I've worked with php, it's a bit different but most thing are almost the same only the declaring of the strings is a bit different.

    Here in c++ you can use what I used:
    Code:
     string mystr;
    Or something like this like ElastoManiac says:
    Code:
    char string [string1];
      cout << "Let's have a chat!\n";
      cout << "What's your name?\n";
      getline (cin, string1);
      cout << "Hello " << string1 << ".\n";
    In php it goes like this:
    You will use $ to let the server now you are starting a string.
    afther the $ you will place the name then a = and than a value.
    Like this:
    Code:
    $mystring = "This is a string in double qoutes which means it will be converted to HTML, so you can placce HTML code in it.. and text will be just text";
    You also stop the sesion with a ';'

    But right now I am a little bit confused how to do it, what should be better to do:
    This:
    Code:
     string mystr;
    Or this
    Code:
    char string [string1];
      cout << "Let's have a chat!\n";
      cout << "What's your name?\n";
      getline (cin, string1);
      cout << "Hello " << string1 << ".\n";

  9. #9
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Since you are using C++ its better to use the string type. Learning how to use character arrays though will be useful but the string type is much easier to learn and use.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    13

    Exclamation

    Ok, I'll keep that in mind but right now I am allready debugging a new one. I was working more with else and if so I learn it.
    Right now I think I have done something wrong with the acolades. Please help me!

    The code:

    Code:
    // cin with strings
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
      string mystr;
      cout << "Let's have a chat!\n";
      cout << "What's your name?\n";
      getline (cin, mystr);
      cout << "Hello " << mystr << ".\n";
      cout << "How old are you?\n";
      getline (cin, mystr);
      cout << "So you are " << mystr << " years old?\n";
      cout << "Whats the name of the town where you live in?\n";
      getline (cin, mystr);
      cout << "In which country can I find " << mystr << " ?\n";
      getline (cin, mystr);
      cout << "Ah I see, I have never been in " << mystr << " but I would like to!\n";
      cout << "This little programm is made in c++, do you use c++ to?\n";
      getline (cin, mystr);
      if (mystr == "yes")
      {
    	cout << "Well, that's great, than you should know how this programm works, do you?\n";
        getline (cin, mystr);
    	if (mystr == "yes")
    	{
    		cout << "That is so cool!. Do you comment while programming?\n";
            getline (cin, mystr);
    		if (mystr == "yes")
    		{
    			cout << "yeah, thats the best to do isn't it?\n";
    		}
    		else if (mystr == "yes I do")
    		{
    			cout << "yeah, thats the best to do isn't it?\n";
    		}
    		else if (mystr == "no")
    		{
    			cout << "you should do, so you don't have to bother for hours\n when you find your script back after 6 months!\n";
    		}
            else if (mystr == "no, I don't")
    		{
    			cout << "you should do, so you don't have to bother for hours\n when you find your script back after 6 months!\n";
    		}
    		else if (mystr == "no I don't")
    		{
    			cout << "you should do, so you don't have to bother for hours\n when you find your script back after 6 months!\n";
    		}
    	}
    	else if (mystr = "no" )
    	{	
    		cout << "Hmm, meabey you should learn how to make such a programm\n don't you think?";
      }
    
      }
      else if (mystr == "no")
      {
    	  cout << "I can recommend you to learn c++, it's really great!\nBut do you programm other scripts?";
    
    
      }
    
      
      return 0;
    }
    And the error:
    error C2451: conditional expression of type 'std::basic_string<_Elem,_Traits,_Ax>' is illegal
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>,
    _Ax=std::allocator<char>
    ]
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Code:
    	else if (mystr = "no" )
    use == operator, not = operator.

  12. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    13
    Haha, lol I only forgot that =, haha, I wasn't even paying attention on the =='s when controlling

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308

    Thumbs up

    Quote Originally Posted by 7stud
    By all means start with string types rather than null terminated char arrays.
    Oh absolutely!
    Null-termination isn't only used for char arrays, but all sorts of arrays (granted, less often), and that can be learnt when someone gets to arrays.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    		else if (mystr == "no")
    		{
    			cout << "you should do, so you don't have to bother for hours\n when you find your script back after 6 months!\n";
    		}
            else if (mystr == "no, I don't")
    		{
    			cout << "you should do, so you don't have to bother for hours\n when you find your script back after 6 months!\n";
    		}
    		else if (mystr == "no I don't")
    		{
    			cout << "you should do, so you don't have to bother for hours\n when you find your script back after 6 months!\n";
    		}
    You should look into the or operator and maybe string searching. You might eliminate a lot of duplicate code.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

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