-
Help with this program!
I have a program that requires me to read from a file looking like this:
Last First 78 70 76 72 82 84
Last First 72 76 77 90 76 45
Last First 64 93 95 83 66 56
Last First 67 78 68 75 64 46
Last First 67 75 97 90 57 89
the program is to read line by line and print out the (last, First, and store the six numbers in an array for each line. How do I put the numbers in an array when reading from the file?
I would really appreciate it if someone can help me. Thanks
-
Use a for loop. Then post your attempt.
-
Try this code with your file:
Code:
#include <iostream>
#include <fstream.h>
#include <cstring>
using namespace std;
int main()
{
ifstream afile("file.txt"); //name of file holding the data you posted
string alldata[40];
for(int i=0;i<40;i++)
afile>>alldata[i];
afile.close();
for(int b=0;b<40;b++)
cout<<alldata[b]<<endl;
system("PAUSE");
return 0;
}
See, the above reads all the pieces(strings) into an array, then prints it out. Is that what you're looking for or no?
-
the numbers stand for grades and I need to manipulate them later in the program. The program is to print a students last name, First name, and then use the grades for some comparison and letter assignment. I can get the last name and the first name, but my problem is that I don't know how to store the grades into an array. I just learning C++
-
jmd15,
It's not appropriate for you to post answers to someone's homework. In addition, there are enough syntax errors, bad programming practices, and poor design to make it something no one should try and emulate.
vuudu,
If arrays are giving you trouble, forget about reading from a file for the moment--that just complicates things. An array is just a series of variables. If I want to store 100 numbers for use in my program, why should I have to think up 100 different names for those variables and declare them with 100 lines of code, e.g.
i
Code:
nt myfirstNum = 0;
int mysecondNum = 0;
int mythirdNum = 0;
...
...
int myonehundredthNum = 0;
All programming languages allow you to declare an array instead:
That means there are 100 variables with the names:
myNums[0], myNums[1], ...myNums[99]
Note how the variable names run from 0 - 99 instead of 1-100. Each of those variables is called an element of the array.
I was able to declare those 100 variables in one line. I can also set everyone of them equal to 0 in the same line when I declare the array:
Code:
int myNums[100] = {0};
Since you should always initialize your variables, that is good practice. Now, every one of those 100 variables is equal to 0.
Try these exercises:
Exercise 1:
1) Create an array of size 5.
2) Assign each element of the array the numbers 1-5 using five assignment statements.
Exercise 2:
The same as 1) except use a for-loop to assign the numbers 1-5 to the array using only one assignment statement.
Exercise 3:
The same as 2) except use a while loop instead.
-
It wasn't doing his homework, THAT WAS AN EXAMPLE OF READING THE DATA INTO AN ARRAY! What are my "syntax errors", "poor design" and all this bs you're talking about, point it out to me. HOW MUCH DESIGN DOES IT TAKE FOR AN EXAMPLE!? IT WAS AN EXAMPLE!! E-X-A-M-P-L-E. NOT HIS HOMEWORK. Man, I can't take arrogance.
-
Code:
#include <fstream.h>
should be:-
When you compile and execute it with this you should notice that your compiler returns no errors.
...and why string alldata[40]?
What happens if there are more than 40 pieces of data in your file? Ok so the OP has shown his file containing exactly 40 pieces of data. But maybe you should have shown him how to handle a file containing any amount of data?
He also said he wanted to read the file in one line at a time.
So why not do something like...
Code:
while(getline(afile, alldata, '\n'))
{
//do stuff
}
Perhaps?
Oh and system("pause") should be swapped with a few
cin.gets. System isn't portable just to name but one of its many pitfalls.
I understand you are probably just an enthusiastic programmer. But making suggestions after the person has had a chance to write something is probably better in the long run.
;)
-
Of course I would read a file like you have shown treenef, but I thought he wanted each string in the line in the array. Didn't he want each piece in an array, or does he just want to read the whole thing? Well either way, I thought he was going to use a SET file like he posted. If he did want to read a file that was bigger than that, you would need to use something bigger than a 40 slot array. Yeah I guess I just got mad in my last post when someone comes in, tells me that I don't know what I'm doing, and then says that only he knows how to do it right. Well I'm past that. Oh, I would normally use a cin.get() in place of a system call but for some reason on the copy of Dev-C++ installed on my WinXP box doesn't pause at the call of cin.get(). It does on my Win98 laptop but not on the XP one.
-
Code:
for(int i=0; i<100; ++i)
cout << "lol!" << endl;
kick his ass seebass