![]() |
![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
|||
| Welcome
to Tech Support Forum home to more then 136,000 problems solved. Issues
have included: Spyware, Malware, Virus Issues, Windows, Microsoft,
Linux, Networking, Security, Hardware, and Gaming Getting your
problem solved is as easy as: 1. Registering for a free account 2. Asking your question 3. Receiving an answer Registered members: * See fewer ads. * And much more..
|
| Want to know how to post a question? click here | Having problems with spyware and pop-ups? First Steps |
|
|||||||
| Programming A discussion forum for programs and programming used in tech-related businesses. |
![]() |
|
|
Thread Tools |
|
|
#1 (permalink) |
|
Registered User
Join Date: Apr 2008
Posts: 1
OS: vista
|
C++ - For loop not running properly
We have project that reads a file that a user inputs and then fills an array from the inputted file and then prints out the array. I am having trouble getting this all to work with a struct, array, and for loop. It is printing two random numbers at the end and I'm not sure why. Here is the program:
Code:
// This program takes an array from a file and allows the user to manipulate
// the data in the file.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
struct studentType
{
string Name;
int ID;
int score;
};
int main()
{
int counter;
ifstream inFile;
string inputFile;
studentType studentList[40];
cout << "Enter the name of the input file: ";
cin >> inputFile;
inFile.open(inputFile.c_str());
if(!inFile)
{
cout << "Invalid file name" << endl;
system("pause");
return 1;
}
inFile >> studentList[0].Name >> studentList[0].ID >> studentList[0].score;
for (counter = 1; inFile; counter++)
{
inFile >> studentList[counter].Name
>> studentList[counter].ID
>> studentList[counter].score;
cout << studentList[counter].Name << " ";
cout << studentList[counter].ID << " ";
cout << studentList[counter].score << " " << endl;
}
inFile.close();
system("pause");
}
Code:
Alex 1220 62 Betsy 1121 70 Candace 2221 83 Don 3324 90 Earl 4678 51 Francesca 5555 86 Guiles 6666 83 Henrietta 7777 73 Ignacius 8888 63 Joliet 9999 81 Kris 1010 66 Lenny 1111 76 Mark 1221 50 Noah 1331 92 Oscar 1441 73 Prince 1442 75 Queenie 7778 73 Rob 2222 83 |
|
|
|
|
|
#2 (permalink) |
|
Registered User
Join Date: Apr 2008
Posts: 1
OS: xp
|
Re: C++ - For loop not running properly
you're not reading from file.
you should use fread ( http://www.cplusplus.com/reference/c...dio/fread.html ) i think this should work for you: while(!feof(f)) fread(&studentList,sizeof(studentType),20,f); where f is the stream to your file. |
|
|
|
![]() |
| Thread Tools | |
|
|