Need help with array

This is a discussion on Need help with array within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <iomanip> #include <fstream> using namespace std; void studentData(); // declarations char studentName[24][40],letterGrade[24]; int classData[24][4], tempGrade; bool ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    18

    Need help with array

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    
    using namespace std;
    
    void  studentData(); // declarations
    char studentName[24][40],letterGrade[24];
    int classData[24][4], tempGrade;
    bool end = false;
    
    ifstream inFile;
    
    
    int main()
    {
    inFile.open("ClassData.txt");
    
    
    
    	if (inFile.fail())  // if file fails to open terminate the program.
    		{
    		cout << "Failed to open file.  Bye!";
    		return 0;
    		}
    		
    while(end == false) // perform function while eof is false
    	{		
    	studentData(); // calling function
    	}
    			
    return 0;
    }
    
    void studentData()
    {
    if(inFile >> tempGrade) //priming read
    end = true; // if eof set boolean to true
    
    
    	for(int count = 0; count < 24; count++) // loop to increment the first dimenson
    		{
    			for(int counter = 0; counter < 4; counter++) // loop to increment the second subscript
    				{
    				classData[count][counter] = tempGrade; 
    				cout   << classData[count][counter] << "  ";
    				inFile >> tempGrade;
    				}
    		inFile.getline(studentName[count],40); // store info for the students name
    		cout << studentName[count] << "\n";
    		}
    }


    Im trying to have a two dimensional array read input from a file and then store the name in a different array. All I get with this program is the first four numbers then a bunch of zero's afterwards. I'll add the file, please this is has been bugging me for the past couple days!
    Attached Files Attached Files

  2. #2
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    shouldn't the while loop like:
    Code:
    while(!inFile.eof()){
        
        // ...
    }
    The way you have it looks like an infinite loop because:
    Code:
    bool end = false;
    
    while( end == false ) // will always be true.
    Also, what's up with all the global variables?

    The prototype for studentData() is okay to be located there, but I would put everything else within main().

    Then you need to pass the inFile by reference to studentData() like this:
    Code:
    studentData( &inFile );
    I didn't see this:
    Code:
    if(inFile >> tempGrade) //priming read
    end = true; // if eof set boolean to true
    that looks kinda fishy...
    Last edited by dudeomanodude; 03-14-2008 at 08:30 AM.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  3. #3
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    BTW, I don't know what your exact assignment is, but judging by what you have posted I would reconsider your design, specifically all the arrays you have:
    Code:
    char studentName[24][40],letterGrade[24];
    int classData[24][4], tempGrade;
    letterGrade[] is unused at present, though I don't know if you'll need that later.

    Consider using an array of structs for this particular assignment, if you're allowed to do so.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by dudeomanodude View Post
    shouldn't the while loop like:
    Code:
    while(!inFile.eof()){
        
        // ...
    }
    Typically no, eof() should not be used to control a loop.

    Quote Originally Posted by dudeomanodude
    The way you have it looks like an infinite loop because:
    Code:
    bool end = false;
    
    while( end == false ) // will always be true.
    Also, what's up with all the global variables?
    Actually, I suspect it is not an infinite loop precisely because end is a global variable, but you were misled because of that. This is anecdotal evidence why global variables should be avoided.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It looks like reading the file might be almost correct as far as controlling the end is concerned (whereas using inFile.eof() definitely isn't), although it seems to suffer from the exact same drawback.

    The usage of globals in this code is terrible indeed, so it is no wonder it confuses the hell out of a casual reader. No portion of code, neither main nor StudentData, makes much sense when read separately.

    As to the problem, you have 4 numbers followed by a string on a line, but it appears you are trying to read 24*4 + 1 numbers in a row (in the inner loop).


    Edit: Sorry, that's not quite right, although it seems that you might be trying to read 5 numbers on the first iteration.
    Last edited by anon; 03-14-2008 at 08:43 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    18
    Quote Originally Posted by anon View Post
    It looks like reading the file might be almost correct as far as controlling the end is concerned (whereas using inFile.eof() definitely isn't), although it seems to suffer from the exact same drawback.

    The usage of globals in this code is terrible indeed, so it is no wonder it confuses the hell out of a casual reader. No portion of code, neither main nor StudentData, makes much sense when read separately.

    As to the problem, you have 4 numbers followed by a string on a line, but it appears you are trying to read 24*4 + 1 numbers in a row (in the inner loop).


    Edit: Sorry, that's not quite right, although it seems that you might be trying to read 5 numbers on the first iteration.
    The way I set up the nested for loops was to do the following. Can someone point me in the right direction?

    when the for loops for the first time execute. The value of the first array is gonna be. Heres some pesudocode.

    Code:
    class[0][0] = 92;
    print 92;
    // the inner loop is then completed and is executed again.
    class[0][1] = 83;
    // etc etc, gets all 4 of the students test then gets the name at the end
    // then executes the outer loop for it's first subscript to be [1][0] yadda yadda
    .......
    
    studentName[0],40 = "john"

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Can someone point me in the right direction?
    I think that at the moment the right direction is to change your global variables into local variables. Try that first, and then post your attempt if you still need more help. If you do ask for help again, remember to post your code with consistent indentation.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Sorry, the above post doesn't make much sense. (I mean OP's).

    Your file input fails because of
    Code:
    if(inFile >> tempGrade) //priming read
    This reads one number, leaving 3 numbers unread on the first line. You then attempt to read 4 more integers. However, on the 4th time the file stream encounters a string. As it cannot convert it to a number, it goes into a failed state and all subsequent reads fail.

    You might probably need to check for potential eof at each input statement. Currently your code seems to be meant for reading the file in 24 line batches. What if the count of lines is not a multiple of 24?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21