Thread: what is wrong with this prgm?

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    2

    what is wrong with this prgm?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int a,c,guess,number=45;
      char play;
      cout<<"hya. Lets play guess my number";
      cout<<"\nIf you are too high, i will tell you so, and if you are too low, I will tell you so";
      cout<<"\nBUT ONLY IF YOU ARE MORE THAN FIVE NUMBERS AWAY!!";
      cout<<"So, do you want to play? (y or n)\n";
      cin>> play;
      cin.ignore();
      if(play=="y")
      {
          cout<<"Great. Im so excited, aren't you!!\n";
          cout<<"ok, i am thinking of a number between one and one hundred...\n";
          cout<<"okay, guess away!\n";
          cin>> guess;
                if (guess > number || guess < number && !(guess + 5==number || guess - 5==number)
                      cout<< "nice try, but not correct";
                      if (guess > number && guess < 100)
                      cout<< "you were too high";
                      if (guess < number && guess > 0)
                      cout<< "you were too low";
                      else ;
                else 
                      if (guess > (answer - 6) && guess < (answer + 6))
                      cout<< "wow, you were within six integers of my number, nice job.";
                      else
                      cout<< "Please enter a number between zero and one hundred."
                  }
              }
    i know that there are things lacking at the end, but other than that, what is wrong? btw, i am kind of a code newbie, so i am still learning this stuff.

    thanx

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    What is wrong as in, "There's an error?"
    Code:
    if (guess > number || guess < number && !(guess + 5==number || guess - 5==number)
    You're missing a closing bracket



    or "Why doesn't it guess properly?"
    In the code line posted above, check the section after the "&&". Are you sure you want the condition to be (guess + 5==number)? Just with a quick overview, it seems like you want to make sure they're not within 5 digits of the number, which would require < or > signs to ensure not only 5 numbers away, but the range of 4, 3, 2, and 1 number away as well.



    or "Why am I posting on the wrong board?"
    cin and cout are C++ functions.


    You'll have to be a touch more specific.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by darkmessenger88
    Code:
                if (guess > number || guess < number && !(guess + 5==number || guess - 5==number)
                      cout<< "nice try, but not correct";
                      if (guess > number && guess < 100)
                      cout<< "you were too high";
                      if (guess < number && guess > 0)
                      cout<< "you were too low";
                      else ;
                else 
                      if (guess > (answer - 6) && guess < (answer + 6))
                      cout<< "wow, you were within six integers of my number, nice job.";
                      else
                      cout<< "Please enter a number between zero and one hundred."
    This If sintax is a bit confusing. Where how it should be
    Code:
    if( expression ){
        to do here
    }else{
        to do here
    } 
    //Or for multiple cases
    if( expression ){
        to do here
    }else if( expression ){
        to do here
    }else if( expression ){
        to do here
    }else{
        to do here
    }
    So
    1# Be careful with each if-else pair. Make sure they have the right match.
    2# Use brackets to make sure what's your intention.

    Now I think you meant this:
    Code:
    if (guess > number || guess < number && !(guess + 5==number || guess - 5==number)){
        cout<< "nice try, but not correct";
        if (guess > number && guess < 100)
            cout<< "you were too high";
        else if (guess < number && guess > 0)
            cout<< "you were too low";
        else ;
    }else{ 
        if (guess > (answer - 6) && guess < (answer + 6))
            cout<< "wow, you were within six integers of my number, nice job.";
         else
            cout<< "Please enter a number between zero and one hundred."
    }
    note. If-else-if is more efficient than separate ifs.
    Last edited by xErath; 11-16-2004 at 12:19 AM.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    i know that there are things lacking at the end
    I sure hope "a" and "c" are also used somewhere in the end there (your declared integer variables). Because, I can't seem to spot them in that code. It's like some deformed game of Where's Waldo were you either didn't throw Waldo in, or you hid him inside of a candy cane. (I.e. "hya", "excited")

    Try to ensure that the variables you use aren't just sitting around for nothing.

    [JEEZ-EDIT!]
    Using variable names that have a bit more meaning are a good way to keep track of what's doing what, and will limit useless variables from sprouting when you deleted what they were being used for, but forgot what that exactly was, so you leave them in to be safe.
    [/JEEZ-EDIT!]
    Last edited by Epo; 11-16-2004 at 12:25 AM.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    I also don't think that the compiler will like
    Code:
    int a,c,guess,number=45;
    Very much. You may have to split that up onto a seperate line, or change it to:
    Code:
    int a,c,guess; int number=45;
    Note, that took 5 seconds of testing, so sue me for all I'm worth if I'm wrong.


    Now, I've gotta be off for sleep (if you note the time of these), and I'm sure people have better things to do than error-check. Listening to the errors your compiler spits out to you is one of the best ways to fix problems.

  6. #6
    Banned
    Join Date
    Oct 2004
    Posts
    250
    try using something like this
    Code:
    #include <iostream>
    #include <windows.h>
    #include <ctime>
    using namespace std;
    int main()
    {
    	srand((unsigned)time(0));
    	int the_number = rand()%100;
    	int guess;
    
    	cout <<"The computer has generated a random number try to guess it "<<endl;
    	cin >> guess;
    
    	for(int guesscount = 0; guesscount < 10; guesscount++)
    	{
    		if(guess == the_number)
    		{
    			cout <<"You got it ! Correct "<<endl;
    			Sleep(5000);
    			return 0;
    		}
    		else if(guess > the_number)
    		{
    			cout <<"To High ! try a lower number"<<endl;
    			cout <<"Guess : ";
    			cin >> guess;
    		}
    		else if(guess < the_number)
    		{
    			cout <<"To Low ! try a higher number"<<endl;
    			cout <<"Guess : ";
    			cin >> guess;
    		}
    	}
    	cout <<"To many tries, Game Over "<<endl;
    	Sleep(5000);
    }
    Last edited by cgod; 11-16-2004 at 01:20 AM.

  7. #7
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by Epo
    I also don't think that the compiler will like
    Code:
    int a,c,guess,number=45;
    Very much. You may have to split that up onto a seperate line, or change it to:
    Code:
    int a,c,guess; int number=45;
    i dont think there is anything wrong with that.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I know something else your compiler won't like: C++ code on a C compiler. Wrong forum. Try that one over there...

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Aug 2004
    Posts
    7
    yeah true also in C u do printf instead of cout

  10. #10
    Registered User
    Join Date
    Nov 2004
    Posts
    2
    Thank you everybody, you have answered my questions very thoroughly. i will also make sure to use the C++ forum next time. remember, I AM A NEWBIE. sorry if i made sum dumb mistakes.

    thanx

    darkmessenger88

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM