Click here to Skip to main content
15,914,820 members
Please Sign up or sign in to vote.
3.29/5 (3 votes)
See more:
What Have I Done Wrong?
C++
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;


void chance(int whatItem){

	srand(static_cast<unsigned int>(time(0)));
	int itemChance = rand();

	int Item = (itemChance % 10) +1;
	int whatItem = Item;	//This Is the Line With The ERROR!

}



void tutorial(){
	cout<<"Welcome to the tutorial";
	cout<<"\nHere we will teach you how to play this text adventure! (OMFG, Right?!)";


}
 

int main(){
	char action [6];
	int playTutorial;
	int age;
	string name;


	

	cout<<"################################################################################";
	cout<<"#                                                                              #";
	cout<<"#                           Beast's Text Adventure                             #";
	cout<<"#                                                                              #";
	cout<<"################################################################################";

	cout<<chance;
	cout<<"\n\n\n\n";
	cout<<"Do you want to play Beast's Text Adventure tutorial?\n";
	cout<<"1) Yes\n";
    cout<<"2) No\n\n";


	cin>>playTutorial;


	if (playTutorial == 1){


		tutorial();
	}else{system("CLS");



	cout<<"################################################################################";
	cout<<"#                                                                              #";
	cout<<"#                           Beast's Text Adventure                             #";
	cout<<"#                                                                              #";
	cout<<"################################################################################";
	/*Story One
	Will Be Deleted (Quick Note To Myself */

	cout<<"\n\nYou are in a dark room, you cant see anything except some light shining from \nsome doors.\nThere is two doors. One to the north. One to the south.";
    cout<<"\n\nWhich way do you want to go ";
		 cin>>action;


	//First room north
	  if ( std::strcmp( action, "north" ) == 0 ){
		  cout<<"\nIt leads to another room. This room is lighter then the last. You can make out alightswitch to the left of you.";
		  cout<<"\n\nWhat do you want to do ";
		  cin>>action;
		

		//First room south
	  }else if ( std::strcmp( action, "south" ) == 0 ){
		  cout<<"\nThis room looks the same as the last except there some light breaking through\nsome red velvet curtains.\n";
	  }
	}
		system("pause");
	return 0;
}
Posted
Updated 2-Aug-13 2:06am
v4

In your code, you have:
C++
cout << chance;

You should change that line to:
C++
cout << chance();

and re-write your function without an argument, and return the random value:
C++
int chance()
{
    srand(static_cast<unsigned int>(time(0)));
    int itemChance = rand();
    
    return (itemChance % 10) + 1;
}
 
Share this answer
 
v3
In C and C++ parameters are passed by value, except you declare them as reference parameters, which only works in C++. Hence
C++
void chance(int whatItem){
 
	srand(static_cast<unsigned int="">(time(0)));
	int itemChance = rand();
 
	int Item = (itemChance % 10) +1;
	int whatItem = Item;	//This Is the Line With The ERROR!

}</unsigned>

whatItem is a value parameter that is passed into the function, but which cannot transfer a value back. On top of this fatal mistake you are re-declaring whatItem inside your function, which is not allowed.

Change your function to:
C++
void chance (int& whatItem)
{
	srand(static_cast<unsigned int="">(time(0)));
	int itemChance = rand();
 
	int Item = (itemChance % 10) +1;
	whatItem = Item;
}
</unsigned>

and things will run the way you intended.
 
Share this answer
 
What is the output of
cout << mystery ( 6, 2, 5 ) << endl;
assuming the following function definition of mystery?
int mystery( int x, int y, int z )
{
int value = x;
if ( y > value )
value = y;
if ( z > value )
value = z;
return value;
} // end function mystery

C++

 
Share this answer
 
Comments
Member 10450035 6-Dec-13 14:41pm    
can you help me out on the above program
Member 10450035 6-Dec-13 14:42pm    
produce a mystery number
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;


void chance(int whatItem){

srand(static_cast<unsigned int="">(time(0)));
int itemChance = rand();

int Item = (itemChance % 10) +1;


}



void tutorial(){
cout<<"Welcome to the tutorial";
cout<<"\nHere we will teach you how to play this text adventure! (OMFG, Right?!)";


}


int main(){
char action [6];
int playTutorial;
int age;
string name;




cout<<"################################################################################";
cout<<"# #";
cout<<"# Beast's Text Adventure #";
cout<<"# #";
cout<<"################################################################################";

cout<<chance;
cout<<"\n\n\n\n";
cout<<"Do you want to play Beast's Text Adventure tutorial?\n";
cout<<"1) Yes\n";
cout<<"2) No\n\n";


cin>>playTutorial;


if (playTutorial == 1){


tutorial();
}else{system("CLS");



cout<<"################################################################################";
cout<<"# #";
cout<<"# Beast's Text Adventure #";
cout<<"# #";
cout<<"################################################################################";
/*Story One
Will Be Deleted (Quick Note To Myself */

cout<<"\n\nYou are in a dark room, you cant see anything except some light shining from \nsome doors.\nThere is two doors. One to the north. One to the south.";
cout<<"\n\nWhich way do you want to go ";
cin>>action;


//First room north
if ( std::strcmp( action, "north" ) == 0 ){
cout<<"\nIt leads to another room. This room is lighter then the last. You can make out alightswitch to the left of you.";
cout<<"\n\nWhat do you want to do ";
cin>>action;


//First room south
}else if ( std::strcmp( action, "south" ) == 0 ){
cout<<"\nThis room looks the same as the last except there some light breaking through\nsome red velvet curtains.\n";
}
}
system("pause");
return 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900