The program crashes as soon as you input 2nd choice in a question
Sep 12, 2013 at 7:41am UTC
The problem I am experiencing is that the program crashes whenever I input the 2nd choice in any question. Any help will be appreciated.
Thanks in advance....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
#include<iostream>
#include<string>
#include<map>
using namespace std;
class Choices
{
protected :
int type;
string answer;
//int no;
public :
Choices()
{
cout<<"Aaa" <<endl;
//no=0;
}
int get_type()
{
return type;
}
void set_type(int typ)
{
type=typ;
}
virtual string get_answer()
{
//cout<<answer<<endl;
return answer;
}
virtual void set_answer(string str)
{
// no++;
answer=str;//no +". " + str +"\n";
}
};
class TextBoxes :public Choices
{
string state;
public :
void set_answer(string str)
{
answer=str;
cout<<"\nt\n" ;
}
string get_answer()
{
//cout<<answer<<endl;
return answer;
}
};
class CheckBoxes :public Choices
{
string state;
public :
void set_answer(string str)
{
// no++;
answer=str;//no +". " + str +"\n";
cout<<"\nc\n" ;
}
string get_answer()
{
//cout<<answer<<endl;
return answer;
}
};
class RadioBoxes:public Choices
{
string state;
public :
void set_answer(string str)
{
// no++;
cout<<"\nr\n" ;
answer=str;//no +". " + str +"\n";
}
string get_answer()
{
//cout<<answer<<endl;
return answer;
}
};
class Questions
{
string StatementText;
int Weightage;
int choices;
Choices * cho;
public :
~Questions()
{
//delete [] cho;
}
int get_choices()
{
return choices;
}
void set_choices(int choice)
{
choices=choice;
}
Choices*& get_cho()
{
return cho;
}
void set_cho(Choices*& choa)
{
cho=choa;
}
string get_StatementText()
{
return StatementText;
}
void set_StatementText(string str)
{
StatementText=str;
}
int get_Weightage()
{
return Weightage;
}
void set_Weightage(int weight)
{
Weightage=weight;
}
};
class Survey{
string headingText;
string PurposeOftest;
int countOfPeople;
int no_of_questions;
public :
map<int ,Questions> questions;
string get_headingText()
{
return headingText;
}
string get_PurposeOftest()
{
return PurposeOftest;
}
int get_countOfPeople()
{
return countOfPeople;
}
void set_countOfPeople(int count)
{
countOfPeople=count;
}
int get_no_of_questions()
{
return no_of_questions;
}
void set_no_of_questions(int count)
{
no_of_questions=count;
}
void set_PurposeOftest(string str)
{
PurposeOftest=str;
}
void set_headingText(string str)
{
headingText=str;
}
};
int main()
{
string temp;
int count=0;
Survey sur;
float weight;
int i=0;
cout<<"Please Enter the Name of the survey: " ;
getline(cin,temp);
sur.set_headingText(temp);
cout<<"Please Enter the Purpose Of the Survey:" ;
getline(cin,temp);
sur.set_PurposeOftest(temp);
cout<<"Please Enter the Number of People on Whom Survey is conducted:" ;
cin>>count;
sur.set_countOfPeople(count);
cout<<"Please Enter The Number of Questions in the Survey:" ;
cin>>count;
sur.set_no_of_questions(count);
getline(cin,temp);
for (int i=0;i<sur.get_no_of_questions();i++)
{
Questions* q1=new Questions;
cout<<"Enter Question " <<i+1<<": " ;
getline(cin,temp);
//sur.questions[i][0]=*q1;
// sur.questions[i][0]
q1->set_StatementText(temp);
cout<<"Enter The Weightage of Question " <<i+1<<": " ;
cin>>weight;
//sur.questions[i][0].
q1->set_Weightage(weight);
int temper=0;
cout<<"Please Enter the Type of Choice: " ;
cout<<"\n1. Radio Button\n2. Check Box\n3. Text Box\n" ;
cout<<"Enter the Choice: " ;
cin>>temper;
if (temper!=3)
{
cout<<"Please Enter the Number of choices: " ;
cin>>count;
getline(cin,temp);
}
if (temper==1)
{
Choices* choic=new RadioBoxes[count];
//cout<<&choic[0]<<endl;
//cout<<&choic[1]<<endl;
q1->set_cho(choic);
// sur.questions[i][0].
//cout<<q1->get_cho()->get_type();
q1->set_choices(count);
sur.questions[i]=*q1;
for (int i=0;i<count;i++)
{
q1->get_cho()[i].set_type(temper);
cout<<"Please enter the text for Choice " <<i+1<<":" ;
getline(cin,temp);
cout<<q1->get_cho()+i<<endl;
choic[i].set_answer(temp);
cout<<"aaaa*" <<endl;
// cout<<q1->get_cho()[i].get_answer();
//cout<<&q1->get_cho()[0]<<endl;
//cout<<&q1->get_cho()[1]<<endl;
}
}
else if (temper==2)
{
Choices* choic=new CheckBoxes[count];
q1->set_cho(choic);
// sur.questions[i][0].
q1->get_cho()->set_type(temper);
//cout<<q1->get_cho()->get_type();
q1->set_choices(count);
sur.questions[i]=*q1;
for (int i=0;i<count;i++)
{
cout<<"Please enter the text for Choice " <<i+1<<":" ;
getline(cin,temp);
q1->get_cho()[i].set_answer(temp);
}
}
else if (temper==3)
{
Choices* choic=new TextBoxes;
q1->set_cho(choic);
// sur.questions[i][0].
q1->get_cho()->set_type(temper);
//cout<<q1->get_cho()->get_type();
q1->set_choices(0);
sur.questions[i]=*q1;
}
}
cout<<"Survey Name: " <<sur.get_headingText()<<endl;
cout<<"Purpose: " <<sur.get_PurposeOftest()<<endl;
cout<<"No. of People: " <<sur.get_countOfPeople()<<endl;
cout<<"Total Number Of Questions: " <<sur.get_no_of_questions()<<endl;
for (int i=0;i<sur.get_no_of_questions();i++)
{
cout<<"Q" <<i+1<<" :" <<sur.questions[i].get_StatementText()<<" ( Weightage=" <<sur.questions[i].get_Weightage()<<" )" <<endl;
if (sur.questions[i].get_cho()->get_type())
{
for (int j=0;j<sur.questions[i].get_choices();j++)
cout<<sur.questions[i].get_cho()[j].get_answer();
}
}
system("pause" );
return 0;
}
Sep 12, 2013 at 12:38pm UTC
what are the exact steps that leads to the crash? Where does it crash (line)?
the use of map<int ,Questions> questions
is questionable. At least you generate a memory leak.
Sep 12, 2013 at 1:07pm UTC
I suspect there may be problems due to mixing of cin >>
and getline()
.
These can be used together, but care needs to be taken over the sequence in which they are used, and ensure that any trailing newline has been cleared from the input buffer before attempting a getline.
Sep 12, 2013 at 2:08pm UTC
No idea why it crashes just pointing out where it does.
Can array indexing be used for choic?
What is returned by get_cho()?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
if (temper==1)
{
Choices* choic=new RadioBoxes[count];
//cout<<&choic[0]<<endl;
//cout<<&choic[1]<<endl;
q1->set_cho(choic);
// sur.questions[i][0].
//cout<<q1->get_cho()->get_type();
q1->set_choices(count);
sur.questions[i]=*q1;
for (int i=0;i<count;i++)
{
q1->get_cho()[i].set_type(temper);
cout<<"Please enter the text for Choice " <<i+1<<":" ;
getline(cin,temp);
cout<<q1->get_cho()+i<<endl;
choic[i].set_answer(temp);////////////////crashes here for i>0
cout<<"aaaa*" <<endl;
// cout<<q1->get_cho()[i].get_answer();
//cout<<&q1->get_cho()[0]<<endl;
//cout<<&q1->get_cho()[1]<<endl;
}
}
else if (temper==2)
{
Choices* choic=new CheckBoxes[count];
q1->set_cho(choic);
// sur.questions[i][0].
q1->get_cho()->set_type(temper);
//cout<<q1->get_cho()->get_type();
q1->set_choices(count);
sur.questions[i]=*q1;
for (int i=0;i<count;i++)
{
cout<<"Please enter the text for Choice " <<i+1<<":" ;
getline(cin,temp);
q1->get_cho()[i].set_answer(temp);///////////crashes here for i>0
}
}
Last edited on Sep 12, 2013 at 2:09pm UTC
Sep 12, 2013 at 3:36pm UTC
I have identifed the problem the problem is with polymorphism
If I replace this line
</
1 2
Choices* choic=new RadioBoxes[count];
with this one
Choices* choic=new Choices[count];
then the problem is not anymore for Radioboxes which means that there is something wrong with it.
Sep 12, 2013 at 7:50pm UTC
which means that there is something wrong with it.
Nope
Topic archived. No new replies allowed.