Thread: Very new to this.

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    4

    Very new to this.

    I've only been experimenting with programming for about 3 days, so please be kind. I am just playing around trying to make a program to learn stuff, but this doesn't do what I want it to. Can someone point me in the right direction, of what I am doing wrong?

    I basically want the user to input the correct answer before moving on.
    Sorry if this seems stupid.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
       // My first attempt at a program
       char  ans1[6]="";
       char  ans2[6]="";
    
    
       cout<<"Here is my first program. Answers are one word, and lowercase.\n Enjoy!",endl;
       cout<<"  \n";
    
       cin.get();
    
       do{
    
       cout<<"What has a bed, but doesn't sleep?\n A mouth, but doesn't talk? \n", endl;
       cout<<"and a head, but doesn't think?\n", endl;
       cout<<"\n\n";
       cout<< "Answer: ";
    
       cin>> ans1;
    
    
              if (ans1 != " river ")
                 cout<<"\nPlease Try Again\n", endl;
              }
    
    
        while (ans1 != " river ") ;
    
           cout<<"CORRECT!", endl;
    
    
          cin.get();
    
      //Next question
    
      do{
    
      cout<<"I can only do my job when my head is struck,\n","What am I?\n", endl;
      cout<<"Answer: " ;
    
      cin.getline(ans2,10);
          if (ans2 != "match")
          cout<<"Please Try Again\n", endl;      }
    
        while(!(ans2 == "match"));
               cout<<"CORRECT!", endl;
    
            cin.get();
    
        cout<<"Thank you for playing!", endl;
    
        cin.get();
    }
    Thanks.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    It would help to say what is going wrong and where you think it is happening, or any error messages that you have.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    4
    Right, sorry.

    Right now, no matter what answer is typed, the program outputs
    "Try again." displays the question again. Even if you type the correct answer.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Your answer has whitespace which would rarely or never be a part of the answer string. Change " river " to "river" and it should work just fine.

    Also be careful with your logic
    Code:
    while(!(ans2 == "match"));
               cout<<"CORRECT!", endl;
    This essentially means that when we don't guess match, we were told we were wrong and right. Over and over.
    Last edited by whiteflags; 06-18-2006 at 04:53 PM.

  5. #5
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    First, you are using a depricated header,
    Code:
    #include <iostream.h>
    should be
    Code:
    #include <iostream>
    secondly, your not declaring
    Code:
    using namespace std;
    which I believe you didn't have to set with with old iostream.h. Next your cout's are improperly syntax'd.
    Code:
    cout<<"What has a bed, but doesn't sleep?\n A mouth, but doesn't talk? \n", endl;
    should be
    Code:
    cout<<"What has a bed, but doesn't sleep?\n A mouth, but doesn't talk? \n"<<endl;
    notice the << instead of , . you separate strings/other stuff in a cout with << .

    It is a wonder your program is even compiling, if I may ask, what compiler are you using?

    Something else, endl flushes the output buffer and also inserts a \n, you do not need it at the end of every cout but at the end of a block of couts (even then, it isn't required).

    http://www.cplusplus.com/ref/iostrea...eam/_endl.html
    Last edited by Wraithan; 06-18-2006 at 05:02 PM.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    4
    Thanks for all the help.
    I'm using Bloodshed DEV-C++.

  7. #7
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    What version of it? If you are not running 4.9.9.2, you should go download that, I also use Dev-C++ and that is how I noticed all those syntax errors when I went to compile.

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    4
    Thanks, I will download the newer version.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> First, you are using a depricated header, #include <iostream.h>.

    Something deprecated by the standard will still work with all standards compliant compilers. But <iostream.h> is not deprecated, and is not part of the standard at all. It already doesn't work on some modern compilers.

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    2
    One other thing:

    Code:
       cin>> ans1;
    
    
              if (ans1 != " river ")
                 cout<<"\nPlease Try Again\n", endl;
              }
    
    
        while (ans1 != " river ") ;
    
           cout<<"CORRECT!", endl;
    should be

    Code:
       cin>> ans1;
    
    
              if (ans1 != " river ")
                 cout<<"\nPlease Try Again\n", endl;
              }
    
    
        while (ans1 == " river ") ;
    
           cout<<"CORRECT!", endl;
    if ans1 != river; while ans1 == river

    You had both as !=

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by atla
    One other thing:

    Code:
       cin>> ans1;
    
    
              if (ans1 != " river ")
                 cout<<"\nPlease Try Again\n", endl;
              }
    
    
        while (ans1 != " river ") ;
    
           cout<<"CORRECT!", endl;
    should be

    Code:
       cin>> ans1;
    
    
              if (ans1 != " river ")
                 cout<<"\nPlease Try Again\n", endl;
              }
    
    
        while (ans1 == " river ") ;
    
           cout<<"CORRECT!", endl;
    if ans1 != river; while ans1 == river

    You had both as !=
    No it is correct as it is. jsmagus wants to print out "Please Try Again" and restart the loop if the answer is wrong.

    The indenting in this code is horrible though.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe that part is better rewritten as:
    Code:
    for (;;)
    {
    	cin >> ans1;
    	if (ans1 == "river")
    	{
    		cout << "CORRECT!" << endl;
    		break;
    	}
    	else
    	{
    		cout << "\nPlease Try Again\n" << endl;
    	}
    }
    Of course, this assumes that ans1 is actually a std::string instead of a char[6]. If not, strcmp() should be used... and there's the issue of what happens if the user (wrongly) guesses a word more than 5 characters.
    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

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    903
    Of course, this assumes that ans1 is actually a std::string instead of a char[6]. If not, strcmp() should be used... and there's the issue of what happens if the user (wrongly) guesses a word more than 5 characters.
    I was about to point that. It is amazing that nobody noticed that the OP used the == operator to compare C-style strings.

    Use the strcmp() function as pointed by laserlight.

Popular pages Recent additions subscribe to a feed