• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Could use some C++ help

Judgement

Diamond Member
Its all basic stuff, this is an introduction to C++.

I'm having problems with the recent lab, my professor did as horrible job explaining how a few things work. I could use help if someone could talk to me over AIM for a bit, pm me your AIM screen name.
 
Look me up on AIM: YourCppProfessor 😉

I think it would be better to go into #WinProg on EFnet on IRC (where you will definitely get help), or just ask your professor in class.
 
I need to open a file, and pull the data from it and put it into an array of structs. The struct has 4 variables.

The function header we were given is passed 4 variables: an ifstream ( passed by &), the array of structures, the array size, and a counter (passed by &) used to count the number of total structures found in the file. The counter is the value passed back to the main program.

I can't seem to get the data from the file into the array of structures properly.

I opened the file using ifstream in the main part of the program because the header given passes the ifstream variable, which makes me think the file is not supposed to be opened in the file itself.

I then used a while statement with the condition that the end of the file has not been reached yet. Then I used a getline(arrayname, arraySize). The next statement was counter++, then the while loop is closed off.

Then return counter;

I called the function in the main part of the program with its return value being assigned to an interger variable.

Well obviously its not working and I'm doing something wrong 🙁

I'm thinking my getline statement is wrong, my book does a horrible join explaining it, and the one example they give is a cin.getfile statement... I have no idea if I'm supposed to be using a cin.getfile statement or a normal getfile statement heh.
 
Originally posted by: Judgement
I need to open a file, and pull the data from it and put it into an array of structs. The struct has 4 variables.

The function header we were given is passed 4 variables: an ifstream ( passed by &), the array of structures, the array size, and a counter (passed by &) used to count the number of total structures found in the file. The counter is the value passed back to the main program.

I can't seem to get the data from the file into the array of structures properly.

I opened the file using ifstream in the main part of the program because the header given passes the ifstream variable, which makes me think the file is not supposed to be opened in the file itself.

I then used a while statement with the condition that the end of the file has not been reached yet. Then I used a getline(arrayname, arraySize). The next statement was counter++, then the while loop is closed off.

Then return counter;

I called the function in the main part of the program with its return value being assigned to an interger variable.

Well obviously its not working and I'm doing something wrong 🙁

I'm thinking my getline statement is wrong, my book does a horrible join explaining it, and the one example they give is a cin.getfile statement... I have no idea if I'm supposed to be using a cin.getfile statement or a normal getfile statement heh.

ok, thats beyond my knowledge

have fun
 
Do you have the getline in the format:

ifstream someVar("testfile.txt");
someVar.getline(temp,100);

 
i remember doing this during my frist C++ class.

Atom has the right commands but the getfile.txt you need to actually put the exact location of that file dont you?
 
Originally posted by: atom
Do you have the getline in the format:

ifstream someVar("testfile.txt");
someVar.getline(temp,100);

my getline statement is in the form

ifstream_variable.getline(structurename, array_length);

I'm trying to open a file (ifstream_variable) and put into into an array (arrayname) of structures (structurename).

For instance a file that contains a list of basketball player names, their jersey number, average points per game and height in inches is opened and I have created a structure designed to hold each of those variables (name, jersey, points, height). I create an array of those structures, and want to transfer the info from the file into the array since there are mulitple players.
 
Getline just gets the entire line, you have to manually put the repsective data into the variables inside the struct. So when you do the getline don't make it put the data into the struct but rather a temp array or something like that. Once you have the line inside a temp array then break up the line into the individual data and then put them into the struct.
 
for a really basic version of what i have (my program has nothing to do with basketball though) so far it looks like this :

struct Players{
int height;
char name[100];
int jersey;
int points;
};

int playerData(ifstream& in, Players playerlist[], int arraySize, int &numPlayers);


main {

ifstream inData("players.data", ios::in);

int numPlayers = 0;
Players playerlist[arraySize];

numPlayers = playerData( in, playerlist, arraySize, numPlayers)
cout << numPlayers;

}

playerData(ifstream& inData, Players playerlist[], int arraySize, int &numPlayers)
{
while (!inData.eof() )
{
inData.getline(playerlist, arraySize);
numPlayers++;
}
return numPlayers;
}


The data file looks like:

Michael Jordan
78
23
45

Allen Iverson
72
3
35


The getline is whats screwing me up, and it doesn't even look like its going to put the data into the array of structures like I need it to.

How about something like:

playerData(ifstream& inData, Players playerlist[], int arraySize, int &numPlayers)
{
while (!inData.eof() )
{
playerlist[numPlayers].name = inData.getline(playerlist, arraySize);
playerlist[numPlayers].height = inData.getline(playerlist, arraySize);
playerlist[numPlayers].jersey = inData.getline(playerlist, arraySize);
playerlist[numPlayers].points = inData.getline(playerlist, arraySize);
numPlayers++;
}
return numPlayers;
}

I'm getting really confused here heh
 
Originally posted by: atom
Getline just gets the entire line, you have to manually put the repsective data into the variables inside the struct. So when you do the getline don't make it put the data into the struct but rather a temp array or something like that. Once you have the line inside a temp array then break up the line into the individual data and then put them into the struct.

Ok, well the data for 1 struct is on 4 lines....

I think I know what you're talking about now, since getline grabs an array of characters I have to make a temporary array to use the getline to assign the info it gets.

What about if getline grabs numbers and you need to assign it to an integer variable thats part of the structure?

On another note, would you do something like this:

inData.getline(playerlist[numPlayers].name, 80);

??
 
Forget what i said in the previous post, if the data is on separate lines then what i said doesn't apply. You don't even need a temp variable unless you want to verify data before assigning it. To convert chars to ints look at the "atoi" function?

The format you posted here:

playerData(ifstream& inData, Players playerlist[], int arraySize, int &numPlayers)
{
while (!inData.eof() )
{
playerlist[numPlayers].name = inData.getline(playerlist, arraySize);
playerlist[numPlayers].height = inData.getline(playerlist, arraySize);
playerlist[numPlayers].jersey = inData.getline(playerlist, arraySize);
playerlist[numPlayers].points = inData.getline(playerlist, arraySize);
numPlayers++;
}
return numPlayers;
}

Should be generally ok I think but you might have to adjust it a little (skipping whitespace and things like that) depending on the format of the input file.

EDIT: And use a different variable other than "arraySize" as the second argument in your getline statement since your array size has no correlation to what you aree getting as input.
 
Originally posted by: atom
Forget what i said in the previous post, if the data is on separate lines then what i said doesn't apply. You don't even need a temp variable unless you want to verify data before assigning it. To convert chars to ints look at the "atoi" function?

The format you posted here:

playerData(ifstream& inData, Players playerlist[], int arraySize, int &numPlayers)
{
while (!inData.eof() )
{
playerlist[numPlayers].name = inData.getline(playerlist, arraySize);
playerlist[numPlayers].height = inData.getline(playerlist, arraySize);
playerlist[numPlayers].jersey = inData.getline(playerlist, arraySize);
playerlist[numPlayers].points = inData.getline(playerlist, arraySize);
numPlayers++;
}
return numPlayers;
}

Should be generally ok I think but you might have to adjust it a little (skipping whitespace and things like that) depending on the format of the input file.

Right now I think I might be able to get it working with a statement for each variable that looks like this:
inData.getline(playerlist[numPlayers].name, 80);

I'm getting an error message on the lines where the structure variable is an integer value and not a character array. I guess because its trying to assign a string to a integer variable, but I don't know how to change the string getline takes from the file and turn it into an integer.
 
Originally posted by: atom
Getline just gets the entire line, you have to manually put the repsective data into the variables inside the struct. So when you do the getline don't make it put the data into the struct but rather a temp array or something like that. Once you have the line inside a temp array then break up the line into the individual data and then put them into the struct.

Ok, I have everything working, but another input file used in a seperate function has all the data on one line.

I read it in and put it into a character array, and now I am having a hard time splitting it up into seperate words to be assigned to different temp character arrays before they are converted to integers using the atoi function.

I've been trying to use a function called strtok in cstring library but I can't get it to work with all its damn pointers and what not.

Ideas?
 
Come on guys just a little more help 🙂

I just need to figure out how to read in multiple variables from a single line in a file, and then split that string up into seperate strings which can be assigned to variables which are part of a structure. I can differentiate where the seperate strings inside the one large string are by a blank space between them.


Example:

The sun is not up yet.

struct words{

char[10] v1;
char[10] v2;
char[10] v3;
char[10] v4;
char[10] v5;
char[10] v6; }

Then assign each word to the seperate variables to make it so that:

v1 = "The"
v2 = "sun"
v3 = "is"

etc
 
Originally posted by: Judgement

I've been trying to use a function called strtok in cstring library but I can't get it to work with all its damn pointers and what not.

Ideas?
char_ptr=strtok(inputstring, delimiters);

while(char_ptr !=NULL){
string[counter]=strtok(NULL, delimiters);
counter++;}

Come on guys just a little more help 🙂

I just need to figure out how to read in multiple variables from a single line in a file, and then split that string up into seperate strings which can be assigned to variables which are part of a structure. I can differentiate where the seperate strings inside the one large string are by a blank space between them.


Example:

The sun is not up yet.

struct words{

char[10] v1;
char[10] v2;
char[10] v3;
char[10] v4;
char[10] v5;
char[10] v6; }

Then assign each word to the seperate variables to make it so that:

v1 = "The"
v2 = "sun"
v3 = "is"

etc

ifstream infile(file, ios::in)

infile>>integer>>char>>float... etc, etc, etc.

or

infile>>v1>>v2>>v3>>.. etc, etc, etc




Might have errors... it's late, and I'm rusty.
 
Originally posted by: motoamd
Heh... I'm on the same thing right now!

You wouldn't happen to be from Austin?

Nope, not from Austin

Thanks for the reply Sahakiel, problem solved 🙂
 
Back
Top