[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

C++ funtion not working proper

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 18
Thread images: 3

Hi so I'm hitting a snag with some C++ I've been working on. I'm trying to make a game of Rock paper Scissots, and I made an RNG generate a number from 1-3 and set R, P, or S to the respective number.

The issue is that the RNG is indeed generating 1-3, but the value of R, P, or S isn't getting set to the variable properly. It's just always S.

I have no idea what's going on... but here's my code.

---------------------

#include <iostream>

using namespace std;

int main()
{
const int MIN_PARAM(1);
const int MAX_PARAM(3);

char playerChoice(0);
int rand(void);
int compRand(0);
char computerChoice(0);

cout<<"*~*~*~* Welcome to Rock, Paper, Scissors *~*~*~*"<<endl<<endl;

//This is where the player selects Rock, Paper, or Scissors
//It will repeat unless the player selects R/r, P/p, or S/s
do
{
cout<<"Which will you choose?"<<endl<<endl;
cout<<"R = Rock"<<endl<<"P = Paper"<<endl<<"S = Scissors"<<endl;

//Player enters their choice. Automatically set to upper-case
cin>>playerChoice;
playerChoice=toupper(playerChoice);

//Check for invalid data
if(playerChoice!='R'&&playerChoice!='P'&&playerChoice!='S')
{
cout<<"Come now, pick a viable option, please."<<endl;
}

}while(playerChoice!='R'&&playerChoice!='P'&&playerChoice!='S');

//Start of computer players' turn

//Random number between 1 - 3
srand(time(0));
compRand = MIN_PARAM+rand()%(MAX_PARAM-MIN_PARAM+1);

//Display random integer to make sure it is always random and 1-3
cout<<compRand<<endl;

//Computer choose R, P, or S based on compRand calculation
if(compRand==1);
computerChoice='R';
if(compRand==2);
computerChoice='P';
if(compRand==3);
computerChoice='S';

cout<<playerChoice<<endl<<computerChoice<<endl;
return 0;
}
>>
Consider using a switch statement for your if blocks. Also you forgot to include <ctime.h> I believe.
Also take your function declarations out of main.
Switch example:
switch(compRand)
{
case 1:
computerChoice='R';
break;
case 2:
computerChoice='P';
break;
case 3:
computerChoice='S';
break;
default:
break;
}
>>
>>7851967
>if(compRand==3);

See that semicolon? That is your statement. Basically, your if statements do nothing because they immediately close.

So this is what happens.

It checks comprand==1, then does nothing.
assins 'R' to computer choice.
checks if compRand==2, then does nothing.
assaigns 'P' to computer choice.
checks if comprand==3, then does nothing.
Assigns 'S' to computer choice.

And that is why it is always S. Your if statements are doing nothing.

Delete those semicolons and your if statements will be fine.

t. Pure Mathematics major - so I am always right about everything.
>>
>>7852011
My bad, they aren't function declarations but variable declarations. Ignore that.
>>
>>7852011
This is correct, and faster than doing if/ if else statements, but it doesn't explain OP why his code is not working.
>>
>>7852016
Jesus christ that worked.
I feel like I owe you partial credit and also I should go for a pure mathematics major apparently.

Thanks a ton!
>>
>>7851967
Change your integer named rand to a different name, should work then.
>>
>>7852023
He declared an integer named rand, which is already a function from the STL I believe.
>>
>>7852032

Bait
>>
>>7852035
Everything worked except the if/else statements.

It actually all works perfectly now.
>>
why dont guys like you dont just go to /g/?
you will get plenty of help there if you are not acting like a retard
>>
>>7852040
If that was the problem, either the compiler would refuse to compile, or the smart compiler would assume that he is talking about his own rand and would use it, expecting std::rand for the STL rand.

His program was compiling so it is the second case. The problem he had was that he was immediately ending his if statements.
>>
>>7852054
I actually had no idea what board was best for this type of question.
I figured here was alright because it was a computer science question but I'll be sure to go there next time.
>>
File: Untitled.png (3KB, 547x38px) Image search: [Google]
Untitled.png
3KB, 547x38px
>>7852055
VS dsiplays this when he has the function rand declared as void, he doesn't need to use the variable.
>>
>>7852065
>Computer Science question
>Syntax mistake

I would fault you for that, but then again CS degrees are basically glorified Python coding bootcamps with a bit of discrete math added in.
>>
>>7852080
I mean I'm basically paying to have this fat fuck tell me shit I already read in the book without getting shown exactly how they are supposed to work so I'm left to just guess and check.

I might as well have just watched youtube videos for fuck's sake.
>>
>>7852065
>>>/g/52919643

stupid questions general

also remove the if's and do:
computerChoice -= (compRand*compRand-7*(compRand+22))/2
>>
>>7851967
Replace this line:
compRand = MIN_PARAM+rand()%(MAX_PARAM-MIN_PARAM+1);
With this:
compRand = rand() % MAX_PARAM + 1;
Thread posts: 18
Thread images: 3


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.