how to read a file with words and numbers into an array, c++?
If you just want to read, you might want to look up "ifstream"...
should i use a char array and an int array?
Well, you are trying to read a text-file, I think!? Why not use a string?
it depends what you want it can be string or char and then you can convert numbers to int if you need
Uh.. there's also somethin like "istringstream" for this.. but it depends on what kind of input you are expecting and what u intend to do with it, I gues..
okay here is my code #include <iostream> #include <fstream> #include <string> using namespace std; void main() { ifstream fin; fin.open("Student Data.txt"); char students[15][11]; for(int i=0; i<15; i++) for(int j=0; j<11; j++) fin>>students[i][j]; for(int r=0; r<15; r++) cout<<students[r]<<endl; fin.close(); }
this is the text file: Amy Adams 10111 97 86 78 95 Ben Barr 20222 89 81 73 87 Carla Carr 30333 79 71 63 77 Don Davis 40444 69 62 58 67 Edna Eaton 50555 63 51 62 48
this is my output: AmyAdams1011197867895BenBarr2022289817387CarlaCarr3033379716377DonDavis404446962 5867EdnaEaton5055563516248╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠l3╖h 1197867895BenBarr2022289817387CarlaCarr3033379716377DonDavis4044469625867EdnaEat on5055563516248╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠l3╖h enBarr2022289817387CarlaCarr3033379716377DonDavis4044469625867EdnaEaton505556351 6248╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠l3╖h 89817387CarlaCarr3033379716377DonDavis4044469625867EdnaEaton5055563516248╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠l3╖h laCarr3033379716377DonDavis4044469625867EdnaEaton5055563516248╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠l3╖h 79716377DonDavis4044469625867EdnaEaton5055563516248╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠l3╖h Davis4044469625867EdnaEaton5055563516248╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠l3╖h 9625867EdnaEaton5055563516248╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠l3╖h Eaton5055563516248╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠ ╠╠╠╠╠╠╠╠l3╖h 3516248╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠l3╖ h ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠l3╖h ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠l3╖h ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠l3╖h ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠l3╖h ╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠l3╖h Press any key to continue . . .
First of all, the main() -methode should _always_ return int - but that's not the problem here. The problem I got, is.. I copied this and I don't get this output.... I don't think, I get what u want, either, but I get no "╠╠╠╠╠╠" thingys... and I don't know where they come from :-/
Those come from reading outside of the array.
excuse the main() thing, i am taught to void; is there a way to get rid of those?
i am wondering if there is a way to read the file line by line.
Sure, you can use getline().
Oh, I see where your error is coming in. The >> operator stops when it encounters whitespace, your third line on each entry is filled with whitespace.
You could try putting a check for the \n char but I think getline will be more of what you're wanting.
how do i use getline with a file?
Muh4ha had put up an example a minute aggo.
jep, but the formatting is bad and I just copied it - the brace-style was horrible :P #include <iostream> #include <fstream> #include <string> using namespace std; int main () { string line; ifstream myfile ("example.txt"); if (myfile.is_open()) { while ( myfile.good() ) { getline (myfile,line); cout << line << endl; } myfile.close(); } else { cout << "Unable to open file"; } return 0; } hope the intendation is somewhat ok, now :-/
ah. thanks (: okay so i see that Amy Adams comes out. how does the string line work exactly?
wait, how would i put that information in arrays then? like i need to average the scores on the third line.
um so the info is stored in "line"?
In the example, yes, line is a string and the read data is stored there.
is line an array?
Well I hope not only Amy Adams comes out, but your whole file.. as it is!? Yes the "current" line (it is in a loop) is stored in the strig line. You can get your numbers out of there..
yeah sorry not just amy adams. but how can i crunch the numbers if it is just a string ?
If your file is always formatted like you have it you can always adjust your loop to account for the whitespace, otherwise you might consider using atoi()
i think i will try a loop. i havent learned atoi yet.
ahm... atoi is just a function that converts a C string to an integer... char str[] = "345"; int i = atoi(str); after that, i == 345 ...
#include <iostream> #include <fstream> #include <string> using namespace std; void main() { ifstream fin; fin.open("Student Data.txt"); string line; while(!fin.eof()) { for(int cnt=0; cnt<15; cnt++) { getline(fin, line); cout<<line<<endl; if (cnt%3==0) { int scores[4]; getline(scores, 4); for(int i=0; i<4; i++) { double total=0; total +=scores[i]; } double avg=0; avg=total/4; } } fin.close(); }
am i getting somewhere ?
i am getting a compiler error at getline(scores, 4)
Well yeah.. the first argument needs to be of type istream and the second of type string.. that's not the case in your code, so this is a syntax error.
const int size=4; int scores[size]; getline(scores, size); ?
What do you want to do?
i want to get the numbers and add them together to average them out...
It would be easier if you were to create a structure/object and read into that. :)
i am rather ignorant about all these terms..."structure/object"?
Part of the error you are getting is the second parameter, as MuH4ha pointed out, is the max number of char to read in, so in order to get the 4 values of 2chars each, you will need to step through it 4 times.
Well you are doing C++ which is an ojbect oriented language, structure is a throwback from C. It is basically making a "box" to hold other "boxes". But maybe you have not gotten there yet.
Looking at your original attempt and making the assumption that your lessons had only gone that far, the best way to approach it would be to notice that each student has 7 values associated with their name. With 15 students you would need to step through the array of [15][7] rather than [15][11].
my counter was for each line, and then once it gets to the third line, i was trying to put the numbers in an array
for converting a string with 4 numbers in it to 4 integers, stringstream might be a good idea. Shouldn't something like: int scores[4]; stringstream ss(str); for(int i=0; i<4; i++) { ss >> scores[i]; } work? Since the numbers are seperated by whitespaces...??? Could be mistaken - it has been like 4-5 years since I last wrote some code (as you surely can tell by now ;) )
The part where you were getting the scores you were still using getline() so it was getting the next four lines. If you want to read the four values you are going to need the << operator as that was the one that stopped at whitespace.
what is stringstream? sorryyy.
stringstream provides an interface to manipulate strings as if they were input/output streams.
I hope you can figure it out, @jesssica - but I have to get to bed.. it's half past 5 in the morning over here and I lost the ability to think a while ago ;) good luck
thanks.
From your original attempt, this will produce the output you seek, however it is not the best, nor the cleanest solution. #include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream fin; fin.open("temp.txt"); char students[35][7]; for(int i=0; i<35; i++) //for(int j=0; j<7; j++) fin>>students[i]; for(int r=0; r<35; r++) cout<<students[r]<<endl; fin.close(); }
i believe i am learning a rudimentary level, that ^^ was exactly what i was looking for. i ended up doing this though: #include <iostream> #include <fstream> #include <string> #include <sstream> using namespace std; void main() { ifstream fin; fin.open("Student Data.txt"); const int size=4; int totalScores=0; string line; while(!fin.eof()) { for(int cnt=1; cnt<16; cnt++) { getline(fin, line); cout<<line<<endl; if (cnt%3==0) { double total=0; int scores[size]; stringstream ss(line); for(int i=0; i<size; i++) { ss>>scores[i]; total +=scores[i]; } double avg=0; avg=total/size; totalScores +=avg; cout<<"Average: "<<avg<<endl; if(avg>=90) cout<<"Letter Grade: A"<<endl; else if (avg>=80) cout<<"Letter Grade: B"<<endl; else if (avg>=70) cout<<"Letter Grade: C"<<endl; else if (avg>=60) cout<<"Letter Grade: D"<<endl; else cout<<"Letter Grade: F"<<endl; } } } double classAvg=0; classAvg=totalScores/5; cout<<"Class average: "<<classAvg<<endl; fin.close(); } thanks everyone for your help!
Glad you got it figured out, I was just formatting a very similar solution. :)
Join our real-time social learning platform and learn together with your friends!