#include <vector>
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
usingnamespace std;
class RandomWheel
{
private:
int currentDice;
int numberInDice;
int numberOfDice;
//const int WHEELSIZE;
vector < int > Dicevalues ;
vector <string> labels ;
public:
///Constructor
RandomWheel();
///Mutators
void Spin();
///Accessors
int GetCurrentValue() const;
string GetCurrentLabel() const;
};
RandomWheel::RandomWheel()
{
srand(time(0));
numberInDice = 0;// dEFAULT
///numberOfDice = length;
currentDice= 0;
constint WHEELSIZE = 6;
vector <int> Dicevalues(WHEELSIZE);
vector <string> labels (WHEELSIZE);
labels[0] = "Butterfly";
labels[1] = "Cockroach";
labels[2] = "Cinderella";
labels[3] = "Pikachu";
labels[4] = "Shrek";
labels[5] = "Scareface";
for (int i = 0; i< 6; i ++ )
{
Dicevalues[i] = i + 1;
}
}
void RandomWheel::Spin()
{
numberInDice = rand()% 5;
}
string RandomWheel:: GetCurrentLabel() const
{
return (labels[numberInDice]) ;
}
int RandomWheel:: GetCurrentValue() const
{
return (Dicevalues[numberInDice]);
}
int PlayOneRound(int VectorLength)
{
vector <RandomWheel* > dicelist (VectorLength) ;
for (int i = 0; i <VectorLength ; i++)
{
dicelist[i] = new RandomWheel;
}
for (int i = 0; i < VectorLength; i++)
{
dicelist[i]->Spin();
//****This line is the problem!!***
//cout <<"Dice"<<i<<" is a "<<dicelist[i]->GetCurrentLabel()<<" : ";
}
}
THe problem has something to do with accessing a piece of memory i do not own, or have not initialized, something like that, but i have checked throughout and cannot find the fix.
Help would be greatly appreciated
Thankyou.
What are you compiling this with? It's a remarkably lax compiler, given that your function PlayOneRound claims to return an int but in fact returns nothing. Does it not even warn you about this?
RandomWheel::RandomWheel()
{
srand(time(0));
numberInDice = 0;// dEFAULT
///numberOfDice = length;
currentDice= 0;
constint WHEELSIZE = 6;
vector <int> Dicevalues(WHEELSIZE);
vector <string> labels (WHEELSIZE); // Here you create a local variable named labels.
labels[0] = "Butterfly"; // And here you do things with that local variable...
labels[1] = "Cockroach";
labels[2] = "Cinderella";
labels[3] = "Pikachu";
labels[4] = "Shrek";
labels[5] = "Scareface";
for (int i = 0; i< 6; i ++ )
{
Dicevalues[i] = i + 1;
}
// And here the function ends. What happens to local variables when a function ends?
}
Here is how I found your error. It took about sixty seconds once I'd written the main function as above.
j@j-desktop:~/badCode$ gdb ./a.out
GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/j/badCode/a.out...done.
(gdb) run
Starting program: /home/j/badCode/a.out
Dice0 is a What is the current size of labels? 0
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b72018 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) () from /usr/lib/libstdc++.so.6
(gdb) bt
#0 0x00007ffff7b72018 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) ()
from /usr/lib/libstdc++.so.6
#1 0x0000000000401b13 in RandomWheel::GetCurrentLabel (this=0x608030)
at 097.cpp:60
#2 0x0000000000401d61 in PlayOneRound (VectorLength=1) at 097.cpp:78
#3 0x0000000000401e4b in main () at 097.cpp:85
(gdb) list
76 dicelist[i]->Spin();
77 //****This line is the problem!!***
78 cout <<"Dice"<<i<<" is a "<<dicelist[i]->GetCurrentLabel()<<" : ";
79 }
80 }
81
82
83 int main()
84 {
85 PlayOneRound(1);
(gdb) up
#1 0x0000000000401b13 in RandomWheel::GetCurrentLabel (this=0x608030)
at 097.cpp:60
60 return (labels[numberInDice]) ;
(gdb) up
#2 0x0000000000401d61 in PlayOneRound (VectorLength=1) at 097.cpp:78
78 cout <<"Dice"<<i<<" is a "<<dicelist[i]->GetCurrentLabel()<<" : ";
(gdb) down
#1 0x0000000000401b13 in RandomWheel::GetCurrentLabel (this=0x608030)
at 097.cpp:60
60 return (labels[numberInDice]) ;
(gdb) list
55 }
56
57 string RandomWheel:: GetCurrentLabel() const
58 {
59 cout << "What is the current size of labels? " << labels.size() << endl;
60 return (labels[numberInDice]) ;
61 }
62
63 int RandomWheel:: GetCurrentValue() const
64 {
(gdb) print labels
$3 = {<std::_Vector_base<std::string, std::allocator<std::string> >> = {
_M_impl = {<std::allocator> = {<__gnu_cxx::new_allocator<std::string>> = {<No data fields>}, <No data fields>}, _M_start = 0x0, _M_finish = 0x0,
_M_end_of_storage = 0x0}}, <No data fields>}
(gdb)
Every line beginning (gdb) is something I typed.
Yikes, look at that! labels has <No data fields>!
Learning to use a debugger will save you so much time.
string names[6] = {"Butterfly, Cockroach, Cinderella, Pikachu, Shrek, Scareface"};
What is this for? You create it and then never use it.
int points[6] = {1,2,3,4,5,6};
Likewise.
labels[0] = "Butterfly";
At this point, what size is labels? Size zero. So what happens when you try to change the value of labels[0], which does not exist? Bad things.
why do labels[0 - 5] dont exist?? Do you mean the labels are not inside the label vector even though i went one by one = "a string", why size is 0.
Also i am not trying to change what i am going to put inside each element of the labels vector.
SO how do you put those labels in the label vector correctly please???!
vector < int > Dicevalues ;
vector <string> labels ;
This creates two vectors, of size zero. If you want to start adding elements to them, you can use push_back, which will add a new element to the end, or you can resize them yourself to some suitable size: