Display Two Dimen.. Info Help?

This is a discussion on Display Two Dimen.. Info Help? within the C++ Programming forums, part of the General Programming Boards category; Hey guys I'm reading a file into a two dimensional array that look like this: 2 2 2 2 2 ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    44

    Exclamation Display Two Dimen.. Info Help?

    Hey guys I'm reading a file into a two dimensional array that look like this:

    2 2 2 2 2
    2 2 2 2 2
    234 234 234 234 2.32323e+006
    22222 2 2 2 2
    Dilmer Valecillos ComputerScience 02/04/3008 323.009
    2 22 2 2 2

    My problem is when displaying the info in the program I want to specify every row with a number

    1. 2 2 2 2 2
    2. 2 2 2 2 2
    3. 234 234 234 234 2.32323e+006
    4. 22222 2 2 2 2
    5. Dilmer Valecillos ComputerScience 02/04/3008 323.009
    6. 2 22 2 2 2

    This is My code so far

    Code:
    system("cls");
    	string Engine[NROWS][NCOLS];
    	ifstream inFile;
    	inFile.open("Engineer.dat");
    	cout << " View ...\n" << endl;
    	///////////////Read data in file//////////////////////
    	for ( int i = 0 ; i <= NROWS -1; i++)
    	{
    		for ( int j = 0; j <= NCOLS-1; j++)
    		{
    			inFile >> Engine [i] [j];
    		}	
    	}
    	
    	///////////////Print Data in file//////////////////////
    	for (  i = 0 ; i <= NROWS -1; i++)
    	{
    		for ( int j = 0; j <= NCOLS -1 ; j++)
    		{	
    			cout << " " << Engine [i][j];
    		}
    		cout << endl; //I want to have a new 
    //line after the end of the las word could I do that?
    	}
    	inFile.close();
    	system ("pause");

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Code:
    cout << i << ". " << Engine[i][j] << "\n";
    Why wouldn't that work?

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    44
    Yes but Engine [i][j]; goes from 1 to 20 and there are only 3 rows that I'm reading from the file so either I have to change the for loop or do something else !

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    44
    and if I do this
    cout << i << ". " << Engine[i][j] << "\n";

    It will display my info like this
    2
    2
    2
    2
    2
    Instead of having the whole row in one line

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    44
    Basically I'm asking for 5 inputs that are First name, Last Name, Degree, HireDate, and Salary. Those four are output it to a file.

    Jason Bush ComputerScience 04/02/2004 $45.000

    But Since I have more than 1 record I need to be able to read the first line and display the data in the program as well as edit the data, and delete the data.
    So I'm kind of having a hard time but some advice would be helpful

    Thank you

  6. #6
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    You want to print the row number before you print each row. Do it just like you print out a new line after every row, only backwards:
    Code:
    for( i loop)
    {
       output row number
       for (j loop)
          { print the row }
       output new line
    }
    There is a difference between tedious and difficult.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Well here is what I suggest you do instead. Make a simple vector of strings, read the strings with in with a space for a delimeter, and push it onto the vetor.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    
    int main (void)
    {
        using namespace std;
        ifstream inFile("Engineer.dat", ios::out);
        vector<string> engine;
        string temp;
        short i = 0;
        
        cout << " View ...\n" << endl;
        while (getline(inFile, temp, ' '))
        {
              engine.push_back(temp);
              cout << ++i << ". " << temp << "\n";
        }
    inFile.close();
    cin.get();
    return 0;
    }
    
    OUTPUT: 
     View ...
    
    1. Jason
    2. Bush
    3. ComputerScience
    4. 04/02/2004
    5. $45.000
    Last edited by whiteflags; 04-28-2006 at 01:04 PM.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    44
    Thanks a lot now I'm getting this output

    1. 2 2 2 2 2
    2. 2 2 22 2 2
    3. 2 2 2 2 2
    4. 2 2 2 2 2
    5. 2 2 2 2 2
    6. 2 2 2 2 2
    7. 2 2 2 2 2
    8. 2 2 2 2 2
    9. Dilmer Valecillos Computer Nose 222
    10.
    11.
    12.
    13.
    14.
    15.
    16.
    17.
    18.
    19.
    20.

    Is there a way to add like inFile.eof to my for loop ?
    I need to display just the info until the end of the last data

    so instead of doing this :

    Code:
    		for (  i = 0 ; i < NROWS -1.; i++)
    		{
    			 cout << i +1 << ". ";
    			for ( int j = 0; j <= NCOLS -1 ; j++)
    			{		
    				cout << Engine[i][j] << " ";
    			}
    			cout << endl;
    		}
    could I do this?

    Code:
    
    		for (  i = 0 ; i < inFile.eof(); i++)
    		{
    			 cout << i +1 << ". ";
    			for ( int j = 0; j <= NCOLS -1 ; j++)
    			{		
    				cout << Engine[i][j] << " ";
    			}
    			cout << endl;
    		}
    Well I tried it but I'm only getting the two first rows follow by 5 columns

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Do not use the return value of eof() as a loop terminating condition regarding reading from a file. Use the fail bit of a stream:

    while(fin >> ....) //for space delimited values
    while(fin.getline(...)) //for reading in C style strings with whitespace
    whille(getline(....)) //for reading in STL style strings with whitespace

    or a given value:

    while(i < 55) //or some other discreet value

    If you want to put all the data in the file in the same table (two dimenstional array), then using a table of strings (an STL vector of STL strings as previously recommended or a 2 dimenstional array of type char if you aren't familiar with STL containers) is probably your best choice given you have more than one data type within the file otherwise.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help displaying info from structure
    By kisiellll in forum C Programming
    Replies: 6
    Last Post: 04-04-2009, 12:51 PM
  2. new problem with class
    By jrb47 in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2006, 07:39 AM
  3. Question about getting an info class from another Form
    By Joelito in forum C# Programming
    Replies: 0
    Last Post: 10-16-2006, 01:02 PM
  4. Help doing an e-mail program in c...
    By Tyler_Durden in forum C Programming
    Replies: 88
    Last Post: 01-02-2005, 02:12 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

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