is this the best way...

This is a discussion on is this the best way... within the C++ Programming forums, part of the General Programming Boards category; IS this the best way to write this piece of code. or is there an easier way. also could someone ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    132

    is this the best way...

    IS this the best way to write this piece of code. or is there an easier way.

    also could someone just post the small function that would work with it cuz i couldnt get it to work even though it looked fine.

    Code:
    // Guessing Game
    
    #include <iostream>
    #include <ctime>
    
    
    using namespace std;
    
    int main()
    {
    int rNumber;  // Random number variable
    int Guess;    // Your input variable
    
    cout << "Welcome to Reece's, Guess the number game...\n\n\n\n\n";
    // random number
    srand(time(NULL));
    rNumber = rand() % 15 + 1;
    
    cout <<"Enter your answer: \n";
    cin >> Guess;
    if (Guess > 15 || Guess < 1)
    cout << "ERROR. Enter number above 0 and below 15 please: \n";
    cin >> Guess;
    
    if(Guess == rNumber)
    cout << "Congrats You Win!\n";
    else
    cout << "You lose!\n";
    
    cout << "The Number is " << rNumber << endl;
    
    system("PAUSE");
    return 0;
    }
    cheers.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    If you wanted too, you could let the user guess the number until he gets it right, by carefully crafting a loop, but you did a good job regardless.

    The reason your random number isn't working is because you forgot to #include <cstdlib>, where the srand and rand functions are declared.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    thanks mate.

    hmm, ill try a loop for it.

    would this work

    while(Guess != rNumber)
    cout << "Wrong, guess again!: \n";
    cin >> Guess;

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >would this work?

    There's only one way to find out!

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    Hmm ok.. lil problem.

    if i type over 15 or below 0, it does what i told it to and replies with error, enter number below 15 and over 0, but when i do it again, it just misses this part out and says nope, wrong answer.
    i want it to keep saying enter number below 15 and over 0 every time i type a number over 15 and under 0 lol.

    Code:
    // Guessing Game
    
    #include <iostream>
    #include <ctime>
    
    
    using namespace std;
    
    int main()
    {
    int rNumber;  // Random number variable
    int Guess;    // Your input variable
    
    cout << "Welcome to Reece's, Guess the number game...\n\n\n\n\n";
    // random number
    srand(time(NULL));
    rNumber = rand() % 15 + 1;
    
    cout <<"Enter your answer: \n";
    cin >> Guess;
    do{
    cout <<"ERROR! Enter a number above 0 but below 15.\n"; break;
    } while(Guess > 15 || Guess < 0);
    
    do
    {
    cout << "Nope, wrong answer... Guess again: \n";
    cin >> Guess;
    }
    while(Guess != rNumber);
    
    cout << "Congrats You Win!\n";
    
    cout << "The Number is " << rNumber << endl;
    
    system("PAUSE");
    return 0;
    }
    can someone please point out what im doing wrong with the first do.. while loop plz.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You have to think again about the logic.
    Your first do while loop actually is no loop.
    it is allwais entered and it will hit the break statment every time. That means not looping at all.
    BTW the two loops have to be nested in some way.
    Kurt
    Last edited by ZuK; 06-05-2006 at 01:55 PM.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I would do it something like this instead.
    Code:
       for ( ;; )
       {
          cout <<"Enter your answer: ";
          cin >> Guess;
          if ( Guess == rNumber )
          {
             break;
          }
          if ( Guess > 0 && Guess < 15 )
          {
             cout << "Nope, wrong answer...\n";
          }
          else
          {
             cout <<"ERROR! Enter a number above 0 but below 15.\n";
          }
       }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    Cheers bud that done it!

    may i just ask, what does for
    Code:
    (;;)
    mean?

    seen it loads but dont know what the ;; means, and not sure on FOR too. im gonna look into for in a min.

    hugo.

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    9

    Infinite loop

    Code:
    for (;;)
    This is just a way to do an infinite loop.

    It has no variables, conditions, or increments.
    The reason he made it an infinite loop was because he had an embedded condition with "break" in it.

    A normal for loop would be

    Code:
    for (var = __  ; var < __   ; var++)
    -if you take out everything, you still must have the 2 ";" left.


    I think another way to do an infinite loop would be
    Code:
    while (1)
    {
    
    }

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    okie dokie.

    cheers bud.

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