Results 1 to 9 of 9
im really sorry to be this guy
but for some reason I cant figure out why im getting a Segmentation fault
#include <iostream>
#include <string>
using namespace std;
int main ...
- 04-13-2011 #1Just Joined!
- Join Date
- Apr 2011
- Posts
- 1
Segmentation fault C++
im really sorry to be this guy
but for some reason I cant figure out why im getting a Segmentation fault
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string name[28];
int count[0];
name[0] = "Rehgar Earthfury";
count[0] = 2;
name[1] = "Lich King";
count[1] = 2;
name[2] = "The Circle of Blood";
count[2] = 3;
name[3] = "Arena Grandmaster";
count[3] = 4;
name[4] = "Arena Master";
count[4] = 4;
name[5] = "Extract of Necromantic Power";
count[5] = 3;
name[6] = "Ring of Invincibility";
count[6] = 2;
name[7] = "Warmace of Menethil";
count[7] = 2;
name[8] = "Scourgeborne Battlegear";
count[8] = 2;
name[9] = "Icy Torment";
count[9] = 3;
name[10] = "Chains of Ice";
count[10] = 3;
name[11] = "Entomb";
count[11] = 3;
name[12] = "Strangulate";
count[12] = 3;
name[13] = "Unholy Ground";
count[13] = 2;
name[14] = "Army of the Undead";
count[14] = 3;
name[15] = "Vixton Pinchwhistle";
count[15] = 3;
name[16] = "Gladiator Katianna";
count[16] = 4;
name[17] = "Short John Mithril";
count[17] = 2;
name[18] = "Gladiator Keward";
count[18] = 3;
name[19] = "Krixel Pinchwhistle";
count[19] = 3;
name[20] = "Gladiator Meganna";
count[20] = 2;
name[21] = "Lo'gosh";
count[21] = 2;
cout << "There are the following copies of cards in the deck."<< endl;
cout << "Number" << " Name" << endl;
int sum = 0;
for (i=0; i < 22; ++i)
{
cout << count[i] << " " << name[i] << endl;
sum = sum + count[i];
}
cout << "Card count = " << sum << endl;
}Last edited by nubquestionftw; 04-13-2011 at 07:10 AM. Reason: Just making the title more specific
- 04-13-2011 #2
when posting code, please please PLEASE use code tags, it is much easier to read
that being said, i don't see where you declared the variable i
also, you should explore the differences between ++i and i++
- 04-13-2011 #3Just Joined!
- Join Date
- Nov 2009
- Location
- Sweden
- Posts
- 31
- 04-14-2011 #4Linux User
- Join Date
- Jan 2005
- Location
- Saint Paul, MN
- Posts
- 262
- 04-14-2011 #5Just Joined!
- Join Date
- Nov 2009
- Posts
- 53
Moi,
Check the length of your longest string constant. You fix the storage at 28. All strings must be null-terminated.
- 04-14-2011 #6Just Joined!
- Join Date
- Nov 2009
- Posts
- 53
Moi again,
Whoops! Ignore some of prev post - too early in the morning.
- 04-14-2011 #7Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
- 04-14-2011 #8Just Joined!
- Join Date
- Jan 2011
- Location
- Bangalore
- Posts
- 2
Hello,
The segmentation fault usually comes when you are accessing the wrong memory or if you do not have permission to access the memory or accessing the memory outside the array index etc. In your case you defined a array as count[0] and you are trying to access the array outside that index. This is causing the segmentation fault.
- 04-14-2011 #9


Reply With Quote
