#include<iostream>
#include<time.h>
#include<vector>
usingnamespace std;
void stats (int& p ){
int nd = 0;
//unfinished
cout << "Number of the most common number: " << nd;
}
int main (){
char yorn; //character datatype to hold the users input as to whether they want to see their stats.
srand(time(NULL));//seed rand() to be random using the system time. dice.
vector <int> myvector(1); //vector (automatically resizing array) to hold all results of rolls for finding the most common number.
myvector[0] = (rand() % 6 + 1) + (rand() % 6 + 1);;//roll
cout << "myvector[0]: " << myvector[0] << "\n";
switch (myvector[0])
{
case 2:
case 4:
case 12:
//shooter wins
cout << "Shooter one wins!";
break;
case 7:
case 11:
//Shooter loses.
cout << "Shooter one loses!";
break;
default:
//shooter has a point to match.
int i = 0; //iterator and point checker.
do {
myvector.resize(1); //add a element to the vector for the next dice roll.
myvector[i] = (rand() % 6 + 1) + (rand() % 6 + 1); //roll dice.
cout << "Shooter one's roll is: " << myvector[i] << "\n";
i += 1;
}
while (myvector[i]!= 7 || myvector[i] == myvector[0]); //if my vector is seven, then the rule states shooter loses. If it equals the first roll then the shooter wins. Not sure how to do this part.
if (myvector[0] == myvecor[i]) cout << "Shooter one gets the point, and wins!"; //this if statement checks to see if the first roll is the last rolled number. If it is then shooter one wins.
else cout << "SHOOTER TWO WINS!"; //self explanatory.
break;
}
cout <<"Would you like to see your stats? Types yes if so, no if not.";
cin >> yorn;
if (yorn == 'y') stats(myvector()); //here's where I try and pass the vector into a function. IDK how. I've searched and haven't found much.
else {}
cin >> yorn; //easy way of making the program stop so you can see the results. I do know this can be messed up if you enter the wrong type of data, but its the end of the program, so who cares, amirite?
return 0;
}
myvector.resize(1); //add a element to the vector for the next dice roll. Nope. Resize will make the size of the vector equal to 1.
You need to use push_back there.
¿why do you think that it will be different with vector that with any other kind of variable?
IDK the vector information on this site and elsewhere was really confusing. I tried wikipedia, I tried this site, I tried various blogs and other tutorials, one or a few of those must be wrong because it really confused me. I just want to increase its size by one. I think my compiler may be corrupt or something, because its forgetting that I've written something, intellisense is even highlighting my comments as wrong. Really freakin' frustrating. I did change a few things right after that, and one of the things I did was what you were advising about. I put: myvector.resize(myvector.size + 1). . I haven't gotten it to build in a whole day, even though everything should be right, syntactically, and I've hit "Build" many, many times. I think I'll re download the compiler again.
I'm not sure the difference between IDE and compiler, as painfully embarrassing it is to say that.
I read it but I just thought it was messed up. A little story: for a long time now I've been right/left clicking extremely fast for no reason, and pressing buttons. Its some kind of tick, I don't have Tourettes. I really can't help it. But its so fast I am highlighting code, and copying it without noticing what just happened. So basically I copied my Do...While loop once, and didn't read the code. That's what it was complaining about, it kept saying "expected 'while' loop " but I just thought it was screwed up. Once I removed the extra, it built. But I got an error with vectors I've also been getting for a while, and it said "vector subscript out of range". I'm guessing thats like a array out or range exception, and I think its my resizing code.
By instance myvector() is illegal.
I knew this, only I changed it from using the blocky parantheses, to that, while trying different things to see if it would compile. I did that because I couldn't find anything on the subject of passing vectors as arguments. That's probably because its painfully obvious, herpy derp.
Do you know what could be causing that "vector subscript out of range" problem?
#include<iostream>
#include<time.h>
#include<vector>
usingnamespace std;
void stats ( ){
int nd = 0;
//unfinished.
cout << "Number of the most common number: " << nd;
}
int main ()
{
char yorn;
srand(time(NULL)); //seed rand() to be random using the system time. dice.
vector <int> myvector(1); //vector (automatically resizing array) to hold all results of rolls for finding the most common number.
myvector[0] = (rand() % 6 + 1) + (rand() % 6 + 1);//store the first dice roll in the first element of myvector, for later.
cout << "myvector[0]: " << myvector[0] << "\n";
switch (myvector[0])
{
case 2:
case 4:
case 12:
//shooter wins
cout << "Shooter one wins!";
break;
case 7:
case 11:
//Shooter loses.
cout << "Shooter one loses!";
break;
default:
int myroll = 0;
bool exitbool = 1;
//shooter has a point to match.
do {
myroll = (rand() % 6 + 1) + (rand() % 6 + 1); //roll dice and put it into a placeholder.
myvector.push_back(myroll); //put the variable containing the dice roll onto the end of myvector.
cout << "Shooter one's roll is: " << myroll << "\n";
if (myvector.back() == myvector.front()) {
cout << "Shooter one gets the point and wins! \n";
break;
}
elseif (myvector.back() == 7)
{
cout << "SHOOTER TWO WINS!\n";
break;
}
else {
//do nothing, just for correctness.
}
} while (exitbool != 0); //Not sure how to make this loop until one of the if statements is true unless I use this dummy variable.
cout <<"Would you like to see your stats? Types yes if so, no if not.";
cin >> yorn;
if (yorn == 'y') stats();
else cin >> yorn;
return 0;
}
}
Thanks bob, just in time! But how do I write the syntax for the actual passing? I've got: if (yorn == 'y') stats(myvector[0]) but I get an intellisense error saying "expecting an expression".
Ok, I got it to pass, but I've never worked with pointers before. They are really confusing. I'm getting an intellisense error saying "expression must have class type" with my myvector.size code in the stats function. That's odd because I specified the type of object that the pointer is, a vector, I really don't know whats wrong.
Plus the syntax for using pointers escapes me, I really don't know how to access an element from the pointer, or increase the pointers memory location. Since its a vector, I assumed I could use .back(), .front(), and .size, but I don't think that works. My code so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void stats (vector <int> *myvector){
int nd = 0;
int tp = 0;
//plan for finding the most common roll: a nested for loop goes through the vector, if the vectors outer loop (the element being tried against the other elements) finds a match anywhere, it increases the nd variable by one and stores the number in variable tp. At the next outer loop iteration, if another number is found to have a higher count then that number should be stored in tp, and the nd variable will be increased again.
//if the two loop iterators are the same, then skip that iteration because then they will be comparing the same container.
for (int i = 0; i < myvector.back() ; i++) {
for (int t = 0; t < myvector.size - 1; t++ ) {
if (*myvector == *myvector ) {} //not sure how to compare to different places in the vector using this pointer.
else
}
}
cout << "Number of the most common number: " << nd;
}
Edit: I did that and it didn't get higlighted as an error again. But the main question still needs answering, how do I do the part where the parameter or argument (the difference I do not fully understand) is written to be passed into the function? I've written the receiving parameter, but I don' know the syntax for passing the vector from main(). if (yorn == 'y') stats(myvector[], myvector.size - 1 ); How would I do that? I've tried passing a single element, removing the brackets, and just writing the name in.
Bump. Edit: I finally found out how to do it. I don't know why you guys told me to use a pointer, it never worked. I found my solution here: http://cplusplus.com/forum/beginner/21957/ . You didn't answer my main question either, how to pass a vector as an argument. When I finally got it to build, it wouldn't shut down after that. I had to force shut down!
One last question: can you mess up your computer permanently if you're just debugging? Does the compiler insulate your computer from you?