-
Strange error
Code:
int fin = 0;
while (fin == 0)
{
int choice;
cout << "What do you want to do: " << endl;
cout << "1. Gain strenght\n" << "2. Gain agility\n" << "3. Gain intelligence\n" << "4. Leave city\n";
cout << "Choice: "; cin >> choice;
switch(choice)
{
case 1:
cout << "You have gained 1 strength!" << endl;
s++;
times++;
break;
case 2:
cout << "You have gained 1 agility!" << endl;
ag++;
times++;
break;
case 3:
cout << "You took lessons! Intelligence +1!" << endl;
intel++;
times++;
break;
case 4:
fin = 1;
break;
}
}
This source code doesn't give compiler errors but when I type "4"
It says critical error and the program shuts down. What is wrong? :confused:
-
If I had to guess. It's whatever comes after the loop that's causing it. Looking at that, I didn't see anything wrong. I might be slipping over something, though.
-
Did you try that in isolation in a main() of it's own, or is it part of some much larger program?
If it's the latter, the problem is likely to be elsewhere, because in isolation it works for me and seems OK.
-
Code:
int city(string c, int s, int ag, int intel)
{
int direction = 0;
int times;
int fin = 0;
string *dirs;
while (fin == 0)
{
int choice;
cout << "What do you want to do: " << endl;
cout << "1. Gain strenght\n" << "2. Gain agility\n" << "3. Gain intelligence\n" << "4. Leave city\n";
cout << "Choice: "; cin >> choice;
switch(choice)
{
case 1:
cout << "You have gained 1 strength!" << endl;
s++;
times++;
break;
case 2:
cout << "You have gained 1 agility!" << endl;
ag++;
times++;
break;
case 3:
cout << "You took lessons! Intelligence +1!" << endl;
intel++;
times++;
break;
case 4:
fin++;
break;
default:
cout << "Wrong input!" << endl;
break;
}
}
if(c == "Rivendell")
{
dirs[0] = "Amon Hen";
dirs[1] = "Ashenvale";
dirs[2] = "Mechanica";
dirs[3] = "Lok-tar";
}
cout << "In which direction do you want to leave?\n1." << dirs[0] << "\n2. " << dirs[1] << "\n3. " << dirs[2] << "\n4. " << dirs[3] << endl;
cin >> direction;
return direction;
}
Whole function "city".
-
Your dirs isn't allocated any memory, so you just write to some random place.
-
-
Do you know why you decided to make it a pointer in the first place?
-
I got an error message with something like a pointer in it, so I made it a pointer and the error was gone! :P
-
> if(c == "Rivendell")
What are you expecting to do if this test fails.