Results 1 to 7 of 7
  1. #1
    samlarenskoog's Avatar
    Join Date
    Apr 2016
    Gender
    male
    Posts
    104
    Reputation
    25
    Thanks
    57
    My Mood
    Cheerful

    Rock, Paper, Scissor game. Random number generator not working.

    Hello.
    I have a problem with my code. The error tells me that "time" is undefined. If I remove srand(time(0)); it only generates 3 //paper.
    Code:
    #include "stdafx.h"
    #include <Windows.h>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	int myChoice;
    	int computerSelection;
    	int computerRandNumb;
    	
    	srand(time(0));
    
    	cout << "Welcome to rock, paper, scissors." << endl;
    	do
    	{
    		cout << "What is your choice?\n" << "1) rock\n 2) scissors\n 3) paper\n";
    		cin >> myChoice;
    	} while (myChoice < 1 || myChoice > 3);
    
    	//-----------------------------------------computer generator---------------------------------------------------------------------
    	computerRandNumb = rand() % 3 + 1;
    	if (computerRandNumb == 1)
    		computerSelection = 1; //rock
    	else if (computerRandNumb == 2)
    		computerSelection = 2; //scissors
    	else
    		computerSelection = 3; //paper
    
    							   //-----------------------------------------end of computer generator----------------------------------------------------------------
    
    	if (myChoice == 1)
    	{
    		if (computerSelection == 1)
    			cout << "You chose rock, your opponent chose rock. Its a draw!\n";
    		else if (computerSelection == 2)
    			cout << "You chose rock, your opponent chose scissors. Rock crushes scissors. You win!\n";
    		else
    			cout << "You chose rock, your opponent chose paper. Paper win against rock. Your lose!\n";
    	}
    	else if (myChoice == 2)
    	{
    		if (computerSelection == 1)
    			cout << "You chose scissors, your opponent chose rock. Rock smashes scissors. You lose!\n";
    		else if (computerSelection == 2)
    			cout << "You chose scissors, your opponent chose scissors. Its a draw!\n";
    		else
    			cout << "You chose scissors, your opponent chose paper. Scissors win against paper. You win!\n";
    	}
    	else
    	{
    		if (computerSelection == 1)
    			cout << "You chose paper, your opponent chose rock. Paper win against rock. You win!\n";
    		else if (computerSelection == 2)
    			cout << "You chose paper, your opponent chose scissors. Scissors win against paper. You lose!\n";
    		else
    			cout << "You chose paper, your opponent schose paper. Its a draw!\n";
    	}
    	cout << "Thank you for playing!\n";
    	system("PAUSE");
    }

  2. #2
    Biesi's Avatar
    Join Date
    Dec 2011
    Gender
    male
    Posts
    4,993
    Reputation
    374
    Thanks
    8,808
    My Mood
    Twisted
    Code:
    #include <ctime>
    std::time

  3. #3
    samlarenskoog's Avatar
    Join Date
    Apr 2016
    Gender
    male
    Posts
    104
    Reputation
    25
    Thanks
    57
    My Mood
    Cheerful
    [QUOTE=Biesi;11537137][FONT="Lucida Console"][COLOR="DimGray"]
    Code:
    #include <ctime>


    it lets me start the program but it doesnt randomize a computer number. Always 3 //paper. Is something wrong with the computer generator?

  4. #4
    Biesi's Avatar
    Join Date
    Dec 2011
    Gender
    male
    Posts
    4,993
    Reputation
    374
    Thanks
    8,808
    My Mood
    Twisted
    Quote Originally Posted by samlarenskoog View Post
    it lets me start the program but it doesnt randomize a computer number. Always 3 //paper. Is something wrong with the computer generator?
    make sure to add
    Code:
    srand(time(0));
    to your source.

    Read this for more information.

  5. #5
    Sammy's Avatar
    Join Date
    Mar 2016
    Gender
    male
    Location
    Vaero
    Posts
    1,102
    Reputation
    224
    Thanks
    228
    What Biesi said. You need srand (time (0)); to get different numbers. And to have no errors when using that you will have to include ctime

  6. #6
    QLK's Avatar
    Join Date
    Mar 2016
    Gender
    male
    Location
    Crete
    Posts
    580
    Reputation
    156
    Thanks
    347
    My Mood
    Amused
    Quote Originally Posted by Biesi View Post
    Code:
    #include <ctime>
    std::time
    isn't it in c++ #include <c.time>?

  7. #7
    Sammy's Avatar
    Join Date
    Mar 2016
    Gender
    male
    Location
    Vaero
    Posts
    1,102
    Reputation
    224
    Thanks
    228
    Quote Originally Posted by QLK View Post
    isn't it in c++ #include <c.time>?
    No it is not. every c++ header does not use a dot. Neither ".h" that would be a c header.

Similar Threads

  1. [Help Request] Random Number generator, and info save
    By Tarthna in forum Visual Basic Programming
    Replies: 7
    Last Post: 12-11-2012, 06:04 PM
  2. [Tutorial] Rand() Function In QT/C++ [Random Numbers Generator]
    By Null::Void in forum C++/C Programming
    Replies: 7
    Last Post: 02-28-2012, 09:50 PM
  3. Rock paper scissor lizard spock
    By Woods in forum General
    Replies: 2
    Last Post: 10-27-2011, 07:05 PM
  4. rock, paper, scissors
    By ace76543 in forum General
    Replies: 5
    Last Post: 12-01-2007, 04:31 PM
  5. Random Number Generator
    By Iam"iDude" in forum Visual Basic Programming
    Replies: 5
    Last Post: 10-02-2007, 04:16 PM