Thread: help vvith rand and svvitching players

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    37

    help vvith rand and svvitching players

    for (int x = 1; x < 100; x++) {
    cout << 1 + (rand() % 100) << endl; this gives me a lot of numbers but hovv do i get only one random number

    hovv did i random player 1 and player 2 start order to guess a number

    cout << "Player 1 = Abel\n";
    cout << "Player 2 = Moore\n";

    can anyone help me im nevv to c++

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You went out of your way to change all your Ws to Vs... I can respect that.
    Devoted my life to programming...

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The for loop repeats that useful cout statement many times. I vvonder vvhat happens vvhen you remove the looping part and leave the cout statement. :thinking:

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    37
    vvhere should i put the for loop

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include <ctime>
    #include <stdlib.h>
    using namespace std;
    int main()
    {
        cout << "Player 1 = Abel\n";
        cout << "Player 2 = Moore\n";
    
        int random number
    
        srand(time(0));
    
        for (int x = 1; x < 100; x++) {
            cout << 1 + (rand() % 100) << endl;
        }
        system("pause");
        return 0;
    }

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    How about putting the loop nowhere?

    Code:
     int random_number =  1 + (rand() % 100); 
    cout << random_number << endl;
    See how that printed one number only.

    Now can you code the rest of the guessing game around such a number? (Don't worry if it's actually always the same random number yet, you can seed rand() when the game works)

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    37
    hovv do i sometimes make player 1 go first and sometimes player 2 go first my teacher said that it involves storing the current player name in a variable

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes, you would store the current player's name in a variable and then use that variable to make output. No matter whose name you chose to store first in the variable, you have to switch names every turn. It might be easier for player 1 to go first until you get the name switching down. After that, you can make it more complicated by asking the user who should go first.

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    37
    does it have to do vvith currentPlayerturn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MP3 Players
    By Dark_Phoenix in forum Tech Board
    Replies: 5
    Last Post: 12-03-2006, 03:22 PM
  2. Many players...
    By Queatrix in forum Windows Programming
    Replies: 4
    Last Post: 11-27-2006, 10:37 PM
  3. Difference in Unseeded rand() and seeded rand()
    By relientk_man in forum C++ Programming
    Replies: 5
    Last Post: 11-09-2005, 07:32 AM
  4. MP3 Players
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2004, 11:37 AM
  5. dvd players
    By metsman20 in forum Tech Board
    Replies: 1
    Last Post: 12-26-2003, 09:56 PM

Tags for this Thread