Question

Hey, i was wondering how i get data from a .txt file and put it into...

Hey, i was wondering how i get data from a .txt file and put it into an array? We have to create a structure to hold the information(firstname, lastname, and age) on some characters. That character info is stored on a .txt file. Any and all help is greatly appreciated. (We are using Visual studio 2017 if that makes a difference)

txt file

Bugs
Bunny
1940
Elmer
Fudd
1933
Porky
Pig
1935
Daffy
Duck
1937
Tweety
Bird
1942
Taz
Devil
1954

Im not sure what you mean by elaborating on the structure?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

#include<iostream>

#include<fstream>

#include<string>

using namespace std;

struct character

{

string firstname,lastname;

int age;

};

int main()

{

ifstream inFile;

character characters[10];

inFile.open("data.txt");

if(!inFile)

{

cout<<"Error!! File not found.."<<endl<<endl;

return 0;

}

int i=0;

while(inFile>>characters[i].firstname>>characters[i].lastname>>characters[i].age)

i++;

cout<<"Date form the file stored in structure array is: "<<endl<<endl;

for(int j=0;j<i;j++)

{

cout<<"First name: "<<characters[j].firstname<<endl;

cout<<"Last name: "<<characters[j].lastname<<endl;

cout<<"Age: "<<characters[j].age<<endl<<endl;

}

return 1;

}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Hey, i was wondering how i get data from a .txt file and put it into...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java : I keep getting something wrong in my loadfile method, how do I fix my...

    Java : I keep getting something wrong in my loadfile method, how do I fix my code? Thank you. here is what is contained in the text file (WorldSeriesWinners.txt) 112 1903 Boston Americans 1904 No World Series 1905 New York Giants 1906 Chicago White Sox 1907 Chicago Cubs 1908 Chicago Cubs 1909 Pittsburgh Pirates 1910 Philadelphia Athletics 1911 Philadelphia Athletics 1912 Boston Red Sox 1913 Philadelphia Athletics 1914 Boston Braves 1915 Boston Red Sox 1916 Boston Red Sox 1917 Chicago...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT