Array of Structures

How would I store this information in an array of structures? I need to put all of this in a loop because the amount of data in the input file can vary but I can't figure out how I would do it to where I could call each one separately.
6.8 kDa mitochondrial proteolipid	Homo sapiens (Human)	MLQSIIKNIWIPMK
Pre-T/NK cell associated protein 6H9A	Homo sapiens (Human)	MRLSCLVIITITAELC
Alcohol dehydrogenase	Brachydanio rerio	MDTTGKVI
RNA-dependent RNA polymerase [Fragment]	San Miguel sea lion virus	PSGMPLTSIINSL
Cecropin	Bombyx mori (Silk moth)	RWKIFK

So far I have declared a struct :
1
2
3
4
5
struct Protein {
	string Name;
	string Species;
	string Sequence;
}data;

I also have placed the first three types into data.Name, data.String, data.Sequence, using getline but where do I go from there? Like I said, the amount of data in the input file can vary so each time around the loop I need another struct name so I would be able to call them later on. Thanks for the help.
By the way each Name, Species, and Protein is separated by a tab... just for clarification.
Last edited on
Since you don't know the size of the input file (i.e. the number of records) you can use a vector of Protein structs. Use a temporary Protein variable to hold the current input. Use getline to get the strings and then push_back the current input in your vector.

If you're not familiar with vectors take a look here -> http://cplusplus.com/reference/stl/vector/
Another detail: I think you need some indicator in your file where the next section (name, species, or sequence) begins. This can be a character, string of characters...

-Albatross
I think you need some indicator in your file where the next section (name, species, or sequence) begins. This can be a character, string of characters...
That's what the newline character is for ;)
Thanks for the help guys.
That's what the newline character is for ;)

Wrong, m4ster r0shi.
RNA-dependent RNA polymerase [Fragment] San Miguel sea lion virus PSGMPLTSIINSL

Format got messed up. The second to last whitespace is a tab.

Sorry.

-Albatross
Last edited on
If you bothered to actually read his post you would see that he separates the elements of each record with tabs. I thought you read that, so the only thing you could be referring to could be the separation of the records from one another. That's why I answered what I answered... But I guess increasing your post counter is of greater priority to you than understanding the problem, so it doesn't really matter...

EDIT: In fact, it wouldn't surprise me if you had read this and yet posted what you posted...
Last edited on
m4ster r0shi
Calm the **** down.
'\n' != '\t'
evaluates to true. I admit, I didn't notice the last line, however your post is wrong also.

I saw something I didn't understand, pointed it out using terms that express uncertainty, you gave me a wrong explanation how he separated his sections (understandable but wrong anyway), I point out that this isn't the case, and you attack me about just raising my post count. Sorry, but I do not tolerate it when someone accuses me of posting garbage.

Note: tysonic, I think you have an error in your example file, then. :)

-Albatross
Calm the **** down.
Watch your tongue plz
Watch your tongue plz

Who is posting garbage now? Seriously, calm the **** down. :)

Anyways, after all that irrelevance, a vector of structs is good idea. For reading, provided you haven't figured this detail out already, I recommend an otherwise infinite while loop that terminates when an EOF is found, and within that three loops that will read characters and add them to the corresponding variable until it finds a tab, in which case it will go to the next loop which will do the same thing for a different variable.
http://cplusplus.com/reference/stl/vector/

Have fun!

-Albatross
Last edited on
Who is posting garbage now? Seriously, calm the **** down. :)
I am calm. And I don't think posting to defend myself against such words is considered garbage posting...

Why bother to use 3 loops instead of using getline? He can simply use two getlines with '\t' as delimitation character and then one with '\n' as delimitation character. It's simpler to write and it probably compiles to faster code.
Guys, in case you haven't noticed I marked this topic as solved... m4ster r0shi, that is exactly what I did and it works fine.
Topic archived. No new replies allowed.