I have to utilize an array for a homework project. I want it to read a line from a file on my hard drive. However, the line starts with words and ends with numbers. I only need to utilize the numbers for a function that I want to pass a value from. I'm sorry if this doesn't make sense. Every example that i've seen for reading from a file into an array is for int and not any type of char, let alone both. Can someone point me in the right direction.

This is what the text file looks like:

Barney Jones 95 83 92 85 87 85 29
Sandra King 99 97 92 91 92 95 32
Josh Harding 0 0 0 0 31 37 30

Let's think it another way :) you do something like this :
Go into a while loop till end of file
{ Read the name
do another loop(till new line) {
array[++i];
}
}
think this should work

Edited 6 Years Ago by Robert1995: n/a

this is what I got so far. I can get it to read the first number that i want it to, but not the rest.

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    const int SIZE = 15; //Length of line
    char first[SIZE], last[SIZE];    
    const int ARRAY_SIZE = 4;
    int numbers[ARRAY_SIZE], t1, t2, attendance;
    int count;
    
    ifstream students;
    
    students.open("C:\\grades.txt");
    
    for (count = 0; count < ARRAY_SIZE; count++);
    students >> first >> last >> t1 >> t2 >> numbers[count] >> attendance;
    
    students.close ();
    
   	for (count = 0; count < ARRAY_SIZE; count++);
    cout << numbers[count] << " " ;
    cout <<"" << endl;
    
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Both of your for() loops end with a semicolon, that's a huge problem. Delete those semicolons and see how it changes the outcome.

If i delete the ;, it reads the first number and the rest is gibberish.

If i delete the ;, it reads the first number and the rest is gibberish.

OK, but you certainly will not want the semicolons there.
By looking at your first post, it seems that there are more integers per line than what you are reading now.

Try the following loop instead.

students.open("C:\\grades.txt");

    count = 0;
    int dummy; // Used for ignoring 3 numbers

    // As long as count remains legal 
    // and all fields can be read ..
    while
    (
      count < ARRAY_SIZE
      &&
      students  >> first >> last
                >> t1 >> t2
                >> numbers[count]
                >> attendance
                >> dummy >> dummy >> dummy
    )
    {
      ++count;
    }

After that loop exits, you might check the number of lines read and also use .eof() to see whether there would be more input. Also a much safer practice would be to change to std::string wrt. first and last .

Then when you print out the stored numbers, don't use ARRAY_SIZE in the for() loop, but rather limit the output to the number of items actually read or else you are likely to see garbage values.

Thank you for the help. I really do appreaciate it. I've got it displaying exactly what I want, but I have a line of garbage above the output I want. Can you tell were it's coming from?

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

int main()
{
    const int SIZE = 15; //Length of line
    char first[SIZE], last[SIZE];    
    const int ARRAY_SIZE = 100;
    int numbers[ARRAY_SIZE], t1, t2, a1, a2, a3, a4, attendance;
    int count;
    
    ifstream students;
    
    students.open("C:\\grades.txt");

    count = 0;
    
    do
   	{
    cout << first << " " << last << " " << a1 << " " << a2 << " " << a3 << " " << a4 << " " << endl ;
    cout << " " << endl;
    cout <<"" << endl;
    }
    
    while
    (
      count < ARRAY_SIZE &&
      students  >> first >> last >> t1 >> t2 >> a1 >> a2 >> a3 >> a4 >> attendance
    );
    {
      count++;
    }
    
    students.close ();
    
  
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

The output looks like this:

Attachments Picture0001.jpg 28.06 KB

Well, the first cout takes place before you have read anything -> garbage.

Perhaps delete the count variable, it's not serving any practical purpose at the moment (it only gets incremented once after the do/while loop is done) - unnecessary variables just add to the clutter, so to speak.

Also really consider using std::string instead of those unsafe char arrays - as of now, you have no control over how much data gets read into the arrays.

[EDIT]
You could also format the code and keep it that way - for readability's sake.

Edited 6 Years Ago by mitrmkar: Many

Completely changed what i was doing again. I've decided to us a
2d array to sum the totals that I need and eventually average them out. But, I'm not sure why, but my output is just increments of ten.

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{

    const int NUM_STUDENTS = 40;           //Number of Students
    const int NUM_SCORES = 4;               //Number of Scores
    double scores[NUM_STUDENTS][NUM_SCORES];
    double total;                           //Accumulator
    double average;                         //Hold Average
    
    fstream students;
    
    students.open("C:\\grades.txt");
    
   
    
    
    for (int row = 0; row < NUM_STUDENTS; row++)
    {
        total = 0;
        
        for (int col = 4; col < NUM_SCORES; col++)
        total += scores[row][col];
        cout << (row + 1) << total << endl;
        
        //average = total/NUM_SCORES;
        
        //cout << "Scores average for student "
        //   << (row +1) << " is " << average << endl;
    }
    
    students.close ();    
    
    


    system("PAUSE");
    return EXIT_SUCCESS;
}

I understand why i'm getting garbage, its not reading from the input file. Can someone help me to understand how to get it to read from my input file. I realized all i've done is opened it.

Completely changed what i was doing again. I've decided to us a
2d array to sum the totals that I need and eventually average them out. But, I'm not sure why, but my output is just increments of ten.

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{

    const int NUM_STUDENTS = 40;           //Number of Students
    const int NUM_SCORES = 4;               //Number of Scores
    double scores[NUM_STUDENTS][NUM_SCORES];
    double total;                           //Accumulator
    double average;                         //Hold Average
    
    fstream students;
    
    students.open("C:\\grades.txt");
    
   
    
    
    for (int row = 0; row < NUM_STUDENTS; row++)
    {
        total = 0;
        
        for (int col = 4; col < NUM_SCORES; col++)
        total += scores[row][col];
        cout << (row + 1) << total << endl;
        
        //average = total/NUM_SCORES;
        
        //cout << "Scores average for student "
        //   << (row +1) << " is " << average << endl;
    }
    
    students.close ();    
    
    


    system("PAUSE");
    return EXIT_SUCCESS;
}

After the page in your text that opens the file should be the page that teaches how to read the file. Try that... :icon_rolleyes:

[edit]
Looking back, it looks like you already know how to read the file. You've done it before. Maybe you should look back to the beginning of your thread, too.
[/edit]

Edited 6 Years Ago by WaltP: n/a

This article has been dead for over six months. Start a new discussion instead.