How do i read a file in from a vector?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • makita18v
    New Member
    • Oct 2008
    • 7

    How do i read a file in from a vector?

    So my program works all right, but I can't read my information from a file into my vector, what am I doing wrong?

    Code:
    vector<char> grades;
    	grades.push_back ('A');
    	grades.push_back ('B');
    	grades.push_back ('C');
    	grades.push_back ('D');
    	grades.push_back ('E');
        
    	
    
    	// declare a place to hold scores read from a file 
    	//int scores[10]={0};
    	vector<int> scores ;
    	
    	// declare the stream object and open the file
    	ifstream theDataFile;
    	theDataFile.open("c:\\scores.txt");
    	if (theDataFile.rdstate() !=0)
    	{
    		cout << "\nError opening file.";
    		exit(1);	
    	}
    	else
    		cout <<"\nThe File opened correctly";
    	// read the scores into the array
    	int i = 0;
    	int aScore = 0;
    	int index = 0;
    	while(!theDataFile.eof( ))
    	{
    		theDataFile >> aScore;
    		if(!theDataFile.good( )) // the read failed ...
    		{
    			if (!theDataFile.eof( )) // and it was not an eof condition
    			{
    				cout << "\nError reading file.";
    				exit(1);
    			}
    			break; // it was an eof, so break out of the loop
    		}
    		 //scores[index++] = aScore;
    		scores.push_back(aScore);
    	}
    	
    	// print out the values just read and give each a grade
    	for (int i = 0; i < index; i++) 
    	{
    
    		cout << scores.size () ;
    		if (scores[i] < 61)
    			cout << grades [4];  // grade is an 'E'
    		else if (scores[i] < 71)
    			cout << grades [3];  // grade is a 'D'
    		else if (scores[i]  < 81)
    			cout << grades [2];  // grade is a 'C'
    		else if (scores [i] < 91)
    			cout << grades [1];  // grade is a 'B'
    		else
    			cout << grades [0];  // grade is an 'A'
    		cout << endl;
    	}
    
    	system("PAUSE");
    	return 0;
  • manontheedge
    New Member
    • Oct 2006
    • 175

    #2
    to read in a vector from a file you can't read directly into the vector, you need to do something like ...

    Code:
    vector<char> numbers;
    char temp;
    
    // then inside of your loop ...
    cin >> temp;
    number.push_back( temp );

    ... that's basically the format it has to be done in. It's the same for any data type, i.e. integers, characters, strings, ...


    sorry, I re-read your post after I posted an this appears to be pretty much what you're doing, ... I'd say to try to take out ALL of the error checking stuff you have, and see if it reads anything with none of the error checking, then if it does, you know there's a problem there. If that's NOT the problem, then make sure the temporary character that your pushing onto the vector has SOMETHING in it each time.

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Since you've commented out the faulty line scores[index++] = aScore, index is no longer getting updated, so your final loop will not work anymore. Try getting scores.size() into a variable and looping based on that instead of index.

      Comment

      Working...