Question

I'm really lost as to why I'm getting an error for this particular array index. I'm writing a program that takes student grades input from a text file. This file is 10 rows of 2 columns in the format (FirstName, LastName, Grade 1, Grade 2, Grade 3, Grade 4, Grade 5). During execution I need to loop through the file line by line and put the names into their own 10 rows by 2 columns and put the grades in their own 10 rows by 5 columns. It's a class thing, Idk why we are doing it that way. Basically the data will split and loaded into a namesArray and gradesArray during the loop. I've marked the error line identified in my IDE with * on either side and at the line positions

Finally, here is my code:

//Student Name:  Jacob Gillespie
//Date:  10/20/13
//Program:  Student Grade Manipulation
//Summary:  Reads in grade data from text file, manipulates data
//and stores data back in output file

//Headers
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//Declare variables and arrays
ifstream inData;
ofstream outData;

string namesArray[10][2];
string gradesArray[10][5];



//Main Program Execution

int main()

{
//Open the data file containing the student names and grades
inData.open("StudentGrades.txt");

//Read data lines and assign student names to namesArray and grades to gradesArray
for (int counter = 0; counter < 10; counter++)
    inData >> namesArray[counter][0] >> namesArray[counter][1]
           >> gradesArray[counter][0] >> gradesArray[counter][1] >> gradesArray[counter][2]
           >> gradesArray[counter][3] >> gradesArray [counter][4];

//TESTING, Print namesArray and gradesArray to ensure values are loaded and assigned correctly
for (int counter = 0; counter < 10; counter++)
    *****cout >> namesArray[counter][***0***] >> " " >> namesArray[counter][1] >> endl;*****

for (int counter = 0; counter < 10; counter++)
    cout  >> gradesArray[counter][0] >> " " >> gradesArray[counter][1] >> " " >> gradesArray[counter][2]
          " " >> gradesArray[counter][3] >> " "gradesArray [counter][4] >> endl;

return 0;
}
Was it helpful?

Solution

cout >> ... >> .. >> ...;

should be

cout << ... << .. << ...;

Think about putting data into an output stream, thus the data should go towards it.

OTHER TIPS

The operator of cout as an output stream is <<.

  +--------+                
  |        |                
  |  cout  | <<-----  data  
  |        |                
  +--------+                

  +--------+                
  |        |                
  |  cin   | ----->>  data  
  |        |                
  +--------+     
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top