Newbie Help please

This is a discussion on Newbie Help please within the C++ Programming forums, part of the General Programming Boards category; These programs are exercises in a book I am using. The 1st is a reverse guess my number program where ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    5

    Newbie Help please

    These programs are exercises in a book I am using.


    The 1st is a reverse guess my number program where the comp. guesses my stored number. My main problem is that it never jumps out of my do loop even though I know for a fact it guessed my number. Also if the argument time(0) is placed in the function srand() it just gives me numbers increasing by 3.

    Code:
    #include<iostream>
    #include<ctime>
    #include<cstdlib>
    
    using namespace std;
    
    int main()
    {
        int myNumber,tries=0;
        int guess = rand() %100 +1;
    
    
        cout<<"What's your secret number 1-100  : ";
        cin>>myNumber;
    
    do
    {
        srand(tries);
         int guess = (rand() %100) +1;
    
        cout<<guess<<endl;
        ++tries;
    
    }
    
    while (myNumber!=guess);
    
                    cout<<"\nDang it  YOU GUESSED IT\n";
                    cout<<"It took you "<<tries<<" tries.";
    
    
    
    
    
    
        return 0;
    }

    The second is menu chooser type. If I try to build it I get the Error
    " no match for 'operator>>' in 'std::cin>>myDifficulty' "
    From what I have read an enumeration associates numbers with strings so if the user in my program typed in 1,2,3 it should run through my if statements as usual.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
        int number;
        cout << "Difficulty Levels\n\n";
        cout << "1 - Easy\n";
        cout << "2 - Normal\n";
        cout << "3 - Hard\n\n";
    
    enum difficulty  {Easy=1,Normal,Hard};
    difficulty  myDifficulty=Easy;
    
    cout<<"\nWhat Difficulty Would You like ? \n";
    cin>>myDifficulty;
    
    
    
    
    if (myDifficulty==Easy)
            cout<<"Easy for You";
    if (myDifficulty==Normal)
            cout<<"Normal, Just Right";
    
    else
        cout <<"Hardcore";
    
    
    return 0;
    }
    I'm about 2 weeks new to this so Please let me know what I have done wrong. Thanks

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    458
    for the first one, you need to cin>>mynumber; to be inside your do-while loop. Thi's the fixed version. you need to use srand() to seed before you use rand().
    Code:
    #include<iostream>
    #include<ctime>
    #include<cstdlib>
    
    using namespace std;
    
    int main()
    {
      srand(time(0));
        int myNumber,tries=0;
        int guess = rand() %100 +1;
    
    
        cout<<"What's your secret number 1-100  : ";
        cin>>myNumber;
    
    do
    {
        cout<<guess<<endl;
        ++tries;
        cout << "Guess again: ";
        cin>>myNumber;
    }
    
     while (myNumber != guess);
     
     cout<<"\nDang it  YOU GUESSED IT\n";
     cout<<"It took you "<<tries<<" tries.";
    
    
    
    
    
    
        return 0;
    }
    for the second code, you need to define an operator>> function that take in type enum.
    Code:
    istream & operator>>(istream& is,  difficulty & en)
    i gave you the prototype, write the body of the function yourself
    Last edited by nimitzhunter; 01-11-2011 at 12:00 AM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    Thanks for your help but in the 1st program I am trying to get the Computer to guess the number not another person. The program you just wrote ask for a number but then creates its own number for me to guess.

    and the 2nd suggestion I have no clue what your talking about so I guess I am still a little too new to this.

    thanks again for you help

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    The main problem with your first program was that you declared int guess at the beginning of main, and then you declared another int guess inside the do-while loop. The second one is a local variable that exists only inside that loop and is completely independent of the first "guess" which is controlling the loop. So the program would have worked if you simply eliminated the keyword "int" inside the loop. But also, as nimitzhunter said, you should only call srand once, and do it before calling rand. Also, if you seed with 0, you will always get the same series of "random" numbers. That's why you should seed with time(NULL). Like this:

    Code:
    #include<iostream>
    #include<ctime>
    #include<cstdlib>
    
    using namespace std;
    
    int main()
    {
        int myNumber,tries=0, guess;
        srand(time(NULL));
        
        cout<<"What's your secret number 1-100  : ";
        cin>>myNumber;
    
        do
        {
            guess = (rand() %100) +1;
    
            cout<<guess<<endl;
            ++tries;
    
        }
    
        while (myNumber!=guess);
    
        cout<<"\nDang it  YOU GUESSED IT\n";
        cout<<"It took you "<<tries<<" tries.";
    
        return 0;
    }
    The second error is because cin doesn't "know how" to handle your enum. But since the enumerated values are just aliases for ints, you should read the input into an int and use that in the comparisons, like this:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int number;
        cout << "Difficulty Levels\n\n";
        cout << "1 - Easy\n";
        cout << "2 - Normal\n";
        cout << "3 - Hard\n\n";
    
        enum difficulty  {Easy=1,Normal,Hard};
        difficulty  myDifficulty=Easy;
    
        cout<<"\nWhat Difficulty Would You like ? \n";
        cin>>number;
    
    
        if (number==Easy)
            cout<<"Easy for You\n";
        else if (number==Normal)
            cout<<"Normal, Just Right\n";
    
        else
            cout <<"Hardcore\n";
    
    
        return 0;
    }
    Notice also that you needed "else" before the second "if"; otherwise, the code
    Code:
    if (number==Normal)
            cout<<"Normal, Just Right\n";
    
        else
            cout <<"Hardcore\n";
    would be executed regardless of the result of the first "if" statement.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    thanks for you help R.Stiltskin. I haven't gotten to NULL so that makes more since. I knew it would be something simple but I just couldn't see the 2nd initialized guess. Hopefully I get my debugger eyes soon lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 09:00 AM
  2. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 07:46 AM
  3. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 02:17 PM
  4. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 12:27 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 03:02 PM

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