if array alrdy been printed, print another one, how? help pls <3

+2 Simon Andersson · January 8, 2015
Hi, have used c++ for a couple of weeks and just trying to re-create the game "Never have I ever" for learning purposes.
However, i dont want the same question to be printed more than once. I'm using one main cpp and one header for the questions. Please help <3 <3 <3

Here is the main cpp.


#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include "Questions.h"
using namespace std;



int main()
{
srand(time(0));
string end = "";
int yes = 0;
int no = 0;
int skip = 0;


do
{
cout << "Never have I ever..." << endl;

int x = 1 + (rand() % 30);
question(x);

cout << "Answer \"Yes\" or \"No\".\nTo end the game, type \"End\"." << endl << endl;
printAnswer(yes, no, skip);
cin >> end;

if (end == "yes" || end == "Yes")
yes++;
else if (end == "No" || end == "no")
no++;
else
{
skip++;
}

system("cls");

} while (end != "end" && end != "End");


}

_________________________

And here is the header file
_________________________

#include <iostream>
#include <string>
using namespace std;

void printAnswer(int yes, int no, int skip)
{
cout << "Questions answered Yes " << yes << "." << endl;
cout << "Qeustions answered No " << no << "." << endl;
cout << "Questions skipped " << skip << "." << endl;
}

void question(char x)
{

string quest[100];
quest[0] = "Licked a nasty P.\n\n";
quest[1] = "Used lube.\n\n";
quest[2] = "Smelled my own farts.\n\n";
quest[3] = "Cheated on an ex.\n\n";
quest[4] = "Surfed a pornsite and realised I'm f#cked up.\n\n";
quest[5] = "Got caught masturbaiting.\n\n";
quest[6] = "Pretended to enjoy the food at some dinner.\n\n";
quest[7] = "Peed myself during the last five years.\n\n";
quest[8] = "Sharted (shit-farted).\n\n";
quest[9] = "Threw up in the line to a nightclub.\n\n";
quest[10] = "Tasted my own D or P.\n\n";
quest[11] = "Missed the toilet at a party and left it.\n\n";
quest[12] = "Taken a huge dump before realizing there was no paper.\n\n";
quest[13] = "Caught my friend having sex.\n\n";
quest[14] = "Caught my friend masturbaiting.\n\n";
quest[15] = "Caught my friend having watching porn.\n\n";
quest[16] = "Found nudes of a friend on her/his cellphone.\n\n";
quest[17] = "Filmed myself masturbaiting.\n\n";
quest[18] = "Filmed myself or anyone else have sex.\n\n";
quest[19] = "Done anal.\n\n";
quest[20] = "Had an STD.\n\n";
quest[21] = "Used perfume instead of taking a shower because I was lazy.";
quest[22] = "Stolen a friends hook-up at a nightclub.\n\n";
quest[23] = "Dated two or more at the same time.\n\n";
quest[24] = "Got caught have too many sidebitches.\n\n";
quest[25] = "Tried reading someone's text messages over his/her shoulder.\n\n";
quest[26] = "Drinken so much alcohol I ended up at the hospital.\n\n";
quest[27] = "Ordered a drink at a bar without paying for it.\n\n";
quest[28] = "Ordered drinks for others at a bar to seem richer than I am.\n\n";
quest[29] = "Taken a dump at a broken toilet.\n\n";
quest[30] = "Used something at home to enhance masturbaiting.\n\n";





//if one array has alrdy been printed
//then do something, how?
cout << quest[x];


}

I'd be so happy if i got this solved <3

Post a Reply

Replies

Oldest  Newest  Rating
0 c student · January 9, 2015
maybe if you store the already-answered question's array index into another array, then when you randomize again, loop through the array to see if it has been answered, if yes, re-randomize.
0 c student · January 9, 2015
well, your x value is still the same... you need to re-randomize it, so put it in the loop
0 Simon Andersson · January 9, 2015
Okay i got it to work using an additional array and a Goto statement, thanks bud!
now i have one more question tho, if my testArray 0-99 equals to 1, i want to break the entire do while loop
and continue down below with new code. suggestions?

this is my main() right now


#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include "Questions.h"
using namespace std;



int main()
{
srand(time(0));
string end = "";
int yes = 0;
int no = 0;
int skip = 0;
int testArray[100] = { 0 };

do
{
cout << "Never have I ever..." << endl;

LOOP:
int x = 1 + (rand() % 99);


if (testArray[x] == 0)
{
question(x);
testArray[x] = 1;
}
else if (testArray[x] == 1)
goto LOOP;

//if testArray 0 - 99 equals to 1, break do while loop and continue
//down below with new codeĀ 

cout << "Answer \"Yes\" or \"No\".\nTo end the game, type \"End\"." << endl << endl;

printAnswer(yes, no, skip);
cin >> end;

if (end == "yes" || end == "Yes")
yes++;
else if (end == "No" || end == "no")
no++;
else
{
skip++;
}

system("cls");

} while (end != "end" && end != "End");

//continue hereĀ 
}
0 c student · January 9, 2015
if n arrays are all equal to 1, then sum must be 1+1+1+...+n, therefore a property exists to test for a full n array will simply be the sum from i -> n = 1*n, or n.
0 Simon Andersson · January 9, 2015
Could you give me an example of this? again, been working with c++ for a couple of weeks so im not the sharpest knife yet :)

i tried something like this;


//test
int maxArray = 99;
for (int iii = 0; iii < testArray[maxArray]; iii++)
while (testArray[iii] == 0)
{
break;
//code for game over?
}

also, at this line 
for (int iii = 0; iii < testArray[maxArray]; iii++)


if i am right, this refers to While iii is less than 99,
and not While iii is less than the value of maxArray[99] (aka either 0 or 1) ?
0 Simon Andersson · January 9, 2015
i could of course do this 

if(testArray[0] + testArray[1] + testArray[2] + testArray[3] + testArray[4] ........ testArray[99] == 100)
//game over code here

but i just wonder if there is any easier way to write this code? 
0 c student · January 9, 2015
you can always keep a sum variable and track its sum.  each time you add one to an array, you can just add 1 to the sum variable.
0 Simon Andersson · January 10, 2015
Thanks a lot! got it to work now! didnt even think of that.. 
  • 1

C++

106,940 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator