[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vip /vp / vr / w / wg / wsg / wsr / x / y ] [Search | Home]
4Archive logo
C++ funtion not working proper
If images are not shown try to refresh the page. If you like this website, please disable any AdBlock software!

You are currently reading a thread in /sci/ - Science & Math

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 (3 KB, 547x38) Image search: [iqdb] [SauceNao] [Google]
Untitled.png
3 KB, 547x38
>>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 replies: 18
Thread images: 3
Thread DB ID: 513488



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

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

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 shown content originated from that site. This means that 4Archive shows their content, archived. If you need information for a Poster - contact them.
If a post contains personal/copyrighted/illegal content, then use the post's [Report] link! If a post is not removed within 24h contact me at [email protected] with the post's information.