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:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* 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
Go Back   Tech Support Forum > The Conversation Pit > Programming
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read

Programming A discussion forum for programs and programming used in tech-related businesses.

Reply
 
Thread Tools
Old 04-04-2008, 12:12 PM   #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");
}
The file that is inputted is here:

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
Todd88 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Bookmark on Thread SoupReddit!
Reply With Quote
Old 04-24-2008, 12:03 AM   #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.
Taquito is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Bookmark on Thread SoupReddit!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -7. The time now is 07:50 PM.



Copyright 2001 - 2008, Tech Support Forum

Search Engine Friendly URLs by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81