0

I am trying to read in two words from text file into one char array element. I CANNOT USE std::string. I get a seg fault because my loop is looping in a way that goes out of bounds. I cannot copy in the two words. please help me do this!! My loop for card struct works perfectly. I cannot get the "first last" into the people[].name // deck of cards // below are initializations #include #include #include #include #include

using namespace std;

//globals
const int maxCards = 52;

//Structs
struct card {
char suit[8];
char rank[6];
int cvalue;
char location;
};

struct player {
char name[100];
int total;
card hand[4];
};

//program
int main()
{

//constants

char tempfName[100];
char templName[100];

//create struct array(s)
card deck[52];
card shuffledDeck[52];
player people[4];

//create pointers
card * deckPointer =NULL;
deckPointer = deck;
card * shuffledDeckPointer=NULL;
shuffledDeckPointer = shuffledDeck;

for(int i=0;i<4;i++)
    {
    strcopy(people[i].name,"first last");
    }

//open player names file
ifstream fin2;
string fin2Name;

//get file name from user
cout << "Enter player file name...(Players.txt)" << endl;

getline(cin, fin2Name);

//open file
fin2.open(fin2Name.c_str());

//check if Players.txt opens correctly
if(!fin2.good())
    {
    cout << "Error with player file!" << endl;
    return 0;
    }
else
    {

    int j =0;
    fin2 >> people[j].name;  //prime file
    while(fin2.good())
    {
    //find the length
    int index =0, length=0;
        while(tempfName[length] != '\0')
        {
            length++;
        }
    //now add space after first name
    tempfName[length] = ' ';
    length++;
    while(templName[index] != '\0')
    {
        tempfName[length] = templName[index];
        length++;
        index++;
    }
    tempfName[length]='\0';
    int counter =0;
    while(templName[counter] != '\0')
    {
    people[0].name[counter] = templName[counter];
    counter++;
    }
}
        }
matt
  • 79
  • 2
  • 13
  • "I CANNOT USE std::string" - If you're going to be that emphatic about it, you may as well say *why*. And `while(fin2.good())` is just as wrong as `while(!fin2.eof())`, which you can [**read about here**](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – WhozCraig Sep 16 '14 at 00:35
  • Please reduce the amount of code you are showing to only the relevant lines to your question. – mbadawi23 Sep 16 '14 at 00:54
  • fin2.good does work just as fin.good works. thanks for replying and addressing ABSOLUTELY NO relevant information whatsoever – matt Sep 16 '14 at 01:21
  • I suggest you read up on the `str*()` functions located in ``. Rather than writing loops to copy individual characters, you should be using `strcat`. You can also use `strchr` to find characters in a C-style string. – Thomas Matthews Sep 16 '14 at 01:54
  • I read your code. Did you extended the same courtesy and bother to read the link I provided? And if you can't use `std::string` what is `fin2Name` ? You want relevant information? Was it your intent to read *one* item from `fin2`? If you enter into your `while` body, what do you expect to break ` while(fin2.good())` when you never touch the stream again? – WhozCraig Sep 16 '14 at 01:58

2 Answers2

0

You could do some cheating:

  char full_name[256]; // Binary quantities are better.
  snprintf(full_name, sizeof(full_name),
           "%s %s",
           first_name, last_name);

You should do more research regarding C-style string functions, like check a good reference book. It should have a chapter about the C-style string functions.

Here are some helpful ones:
strcpy, strcat, strchr, sprintf, strlen

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

it seems your tempfName is not pointing to a correct object in your while loop in else statement else {

int j =0;
fin2 >> people[j].name;  //prime file
while(fin2.good())
{
//find the length
int index =0, length=0;
    while(tempfName[length] != '\0')
    {
        length++;
    }
cck3rry
  • 191
  • 2