I have been stuck for awhile trying to read the data in my file. As you can see, I haven't did much coding, but I would appreciate the assistance if someone can tell me what is wrong with the code. Thanks!

Example of my input file:

Kay, Harry 95 A
Barnes, Mary 88 B
Cartwright, Matt 75 C
Downes, Ryan 80 B
Fields, Whitney 69 D


Here is my code:

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

struct studentType
{
     string studentFName;
     string studentLName;
     int testScore;
     char grade;
       
       
};


void init (studentType students[]);
void assignGrades (studentType students[]);
void highestTest (studentType students[]);
void printHighest (studentType students[]);

int main()
{
    
  studentType students[5];
    

  init(students);
  assignGrades(students);
  highestTest(students);
  printHighest(students);
  
  
 
 system ("PAUSE");
 return 0;   
}

void init (studentType students[])
{
     
    for (int i = 0; i < 1; i++)
    { 
    ifstream inData;
    inData.open("Unknown.txt");
    
    inData>>students[i].studentLName;
    inData>>students[i].studentFName;
    inData>>students[i].testScore;
    inData>>students[i].grade;
    inData.close();
    }
    
    
    
}


void assignGrades (studentType students[])
{
     
     
     

}

void highestTest (studentType students[])
{
     
     
     
     
}

void printHighest (studentType students[])
{
     
     
     
}

You forgot to include the string header:
#include <string>

without this, the compiler doesn't know how to handle string datatypes.

You forgot to include the string header:
#include <string>

without this, the compiler doesn't know how to handle string datatypes.

It didn't work. =(

How did it not work? It works over here. Perhaps dev++ doesn't include the string lib? I need more information (an error message?)

I keep getting the first person's information multiple times. It isn't recognizing the other names at all.


Here is my code:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

struct studentType
{
     string studentFName;
     string studentLName;
     int testScore;
     char grade;
       
       
};


void init (studentType students[]);
void assignGrades (studentType students[]);
void highestTest (studentType students[]);
void printHighest (studentType students[]);

int main()
{
    
  studentType students[5];
    

  init(students);
  assignGrades(students);
  highestTest(students);
  printHighest(students);
  
  
 
 system ("PAUSE");
 return 0;   
}

void init (studentType students[])
{
     
    for (int i = 0; i < 5; i++)
    { 
    ifstream inData;
    inData.open("Unknown.txt");
    
    inData>>students[i].studentLName;
    inData>>students[i].studentFName;
    inData>>students[i].testScore;
    inData>>students[i].grade;
    inData.close();
    }
    for (int i = 0; i < 5; i++)
    {
    cout<<students[i].studentLName<<" ";
    cout<<students[i].studentFName<<" ";
    cout<<students[i].testScore<<" ";
    cout<<students[i].grade<<" ";
    }
}

Can someone help me please?

whoa

ifstream inData;
    inData.open("Unknown.txt");

Try pulling that out of the for loop

whoa

ifstream inData;
    inData.open("Unknown.txt");

Try pulling that out of the for loop

I wish I could say that worked, but it didn't. o_O

I tried several different ways, but I had no luck. Can anyone help please?

I still need help. Does anyone know how to read from a file in this type of program? I need help, so I can start writing my program. Help..anyone?

Any of you guys coming back? lol... Someone wanna tell me how to read from the file in my program, so I can start working on it? o_O

I wish I could say that worked, but it didn't. o_O

I tried several different ways, but I had no luck. Can anyone help please?

"It didn't work" isn't helpful. You say you've tried several different ways, but we have no idea what you've tried and we don't know the results and we don't know whether you got the same bad results no matter what the input was, etc. You need to provide the updated code and more importantly, you need to be far more detailed in describing exactly what you've done and what exactly the results are, along with the input file you used, which I assume is the same one you initially posted. We can't do anything with "It didn't work".

Alrite, I will try to be more clear.

Alrite, I am doing a program for struct that will read data in from a file. I have currently having trouble reading all the names,testscores,lettergrades. When I read from the file, it just gives me the first person's names,grades,lettergrade going across multiple times. I want the output to print out exactly how it looks in my input file:


Input file:

Kay, Harry 95 A
Barnes, Mary 88 B
Cartwright, Matt 75 C
Downes, Ryan 80 B
Fields, Whitney 69 D


Here is my code:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

struct studentType
{
     string studentFName;
     string studentLName;
     int testScore;
     char grade;
       
       
};


void init (studentType students[]);
void assignGrades (studentType students[]);
void highestTest (studentType students[]);
void printHighest (studentType students[]);

int main()
{
    
  studentType students[5];
    

  init(students);
  assignGrades(students);
  highestTest(students);
  printHighest(students);
  

 
 system ("PAUSE");
 return 0;   
}

void init (studentType students[])
{

     ifstream inData;
     
    for (int i = 0; i < 5; i++)
    { 
     inData.open("Unknown.txt");
     
    inData>>students[i].studentLName;
    inData>>students[i].studentFName;
    inData>>students[i].testScore;
    inData>>students[i].grade;
    inData.close();
    }
    
    
    
    for (int i = 0; i < 5; i++)
    {
    cout<<students[i].studentLName<<" ";
    cout<<students[i].studentFName<<" ";
    cout<<students[i].testScore<<" ";
    cout<<students[i].grade<<" ";
    }
}


void assignGrades (studentType students[])
{
     
     
     

}

void highestTest (studentType students[])
{
     
     
     
     
}

void printHighest (studentType students[])
{
     
     
     
}

You most definitely want to take the inData.open("Unknown.txt"); and inData.close(); out of the for() loop. Open the file before you enter the loop and close the file after the loop has finished.

void init (studentType students[])
{
    ifstream inData;
     
    for (int i = 0; i < 5; i++)
    { 
    inData.open("Unknown.txt");
    
    inData>>students[i].studentLName;
    inData>>students[i].studentFName;
    inData>>students[i].testScore;
    inData>>students[i].grade;
    inData.close();
    }
    for (int i = 0; i < 5; i++)
    {
    cout<<students[i].studentLName<<" ";
    cout<<students[i].studentFName<<" ";
    cout<<students[i].testScore<<" ";
    cout<<students[i].grade<<" ";
    }
}

This is the exact same code as you posted before save for pulling the line in red outside of the loop. As chococrack pointed out, you needed to pull inData.open("Unknown.txt"); out too, and as mitrmkar points out, inData.close(); needs to be outside of the loop also.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.