Thread: Reading Excel Row Cells A through J

  1. #16
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35

    hate to bother

    hate to bug you prelude but you seem to understand what im trying to do. now im not asking you to do this for me but you've been great at explaining this to me today. so anyhoo. when i first ran the program it would calculate the mean and display only the first line. now when i added the following code
    Code:
    	//verify open was sucessful
    	if(!inFile.fail())	//ifopen did not fail
    	{
    		//read cell a1 through j1
    		inFile >> cell1 >> cell2 >> cell3 >> cell4 >> cell5 >> cell6 >> cell7 >> cell8
                   >> cell9 >> cell10;
    		while(!inFile.eof()){
    
            //calculate the mean for ROW and place in mean variable
    		mean = (cell1+cell2+cell3+cell4+cell5+cell6+cell7+cell8+cell9+cell10)/10;
    		inFile >> cell1 >> cell2 >> cell3 >> cell4 >> cell5 >> cell6 >> cell7 >> cell8
                   >> cell9 >> cell10;
    		}
    	}
    ...it only calculates the mean for the very last set of floating point numbers and displays it. How in the world do i get it to display all the rows 1-2000???

  2. #17
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    well i got it to spit out 2000 rows but whats wierd is the calculations are all wrong for every row now. hmmmm maybe some pringles and soda pop will help illeviate this problem

  3. #18
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Assuming your file is formatted correctly (it should be from what I saw), the following is something like what you want:
    Code:
    #include <fstream.h>
    #include <iostream.h>
    
    int
    main()
    {
      float    cell[10];
      ifstream in("healthy.txt");
    
      // Check for success :-)
    
      while (in>> cell[0]) {
        // If we get one, then assume the rest are there
        for (int i = 1; i < 10; i++) {
          in>> cell[i];
        }
        float sum;
        for (int i = 0; i < 10; i++) {
          sum += cell[i];
        }
        out<< sum / 10 <<endl;
      }
    
      return 0;
    }
    My best code is written with the delete key.

  4. #19
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    im going to take a break and try it out after my snack attack. but it seems your code will get the first row of 10 foating point numbers. Then my program needs to do that for the next 1999 rows which i dont see just yet but dont tell me unless your code wouldnt work for that. thanks again and hopefully i can figure this out tonight

  5. #20
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but it seems your code will get the first row of 10 foating point numbers.
    My loop doesn't work with a "row" per se because the >> operator treats all whitespace alike. Just read values in groups of 10 and as long as all rows have 10 values, everything will work fine because if the first value isn't there, the condition will fail (on eof we hope) and terminate the loop at the right time.
    My best code is written with the delete key.

  6. #21
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    okay this is what ive done today and it calculates the mean for the first row in the healthy.txt attachment i added earliear. I need it to do the rest of the 1999 rows as well...

    Code:
    #include<iostream.h>
    #include<fstream.h>
    
    void main()
    {
    
    	//might need variables to store in the mean
    	float mean = 0.0;
    	float cell1= 0.0;
    	float cell2= 0.0;
    	float cell3= 0.0;
    	float cell4= 0.0;
    	float cell5= 0.0;
    	float cell6= 0.0;
    	float cell7= 0.0;
    	float cell8= 0.0;
    	float cell9= 0.0;
    	float cell10= 0.0;
    
    
    
    	//open up file
    	ifstream inFile;
    	inFile.open("healthy.txt");
    
    
    	//verify open was sucessful
    	if(!inFile.fail())	//ifopen did not fail
    	{
    		//read cell a1 through j1
    		inFile >> cell1 >> cell2 >> cell3 >> cell4 >> cell5 >> cell6 >> cell7 >> cell8
                   >> cell9 >> cell10;
    	
    		
            //calculate the mean for ROW and place in mean variable
    		mean = (cell1+cell2+cell3+cell4+cell5+cell6+cell7+cell8+cell9+cell10)/10;
    		cout<<mean<<endl;
    		
    	}//end if
    
    
    	
    	else //open failed
    		cout<<"Error opening file."<<endl;
    
    
    
    }//end main
    now when i added in the EOF function like so...

    Code:
    //verify open was sucessful
    	if(!inFile.fail())	//ifopen did not fail
    	{
    		//read cell a1 through j1
    		inFile >> cell1 >> cell2 >> cell3 >> cell4 >> cell5 >> cell6 >> cell7 >> cell8
                   >> cell9 >> cell10;
    		while(!inFile.eof()){
    
            //calculate the mean for ROW and place in mean variable
    		mean = (cell1+cell2+cell3+cell4+cell5+cell6+cell7+cell8+c  ell9+cell10)/10;
                    cout<<mean<<endl;
    		inFile >> cell1 >> cell2 >> cell3 >> cell4 >> cell5 >> cell6 >> cell7 >> cell8
                   >> cell9 >> cell10;
    		}
    	}
    it calculates all 2000 rows and displays them but it calculates them wrong. I checked with my trusty rusty calculator several times. Do you see anything wrong with my logic on getting the mean for each row?

  7. #22
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    oh btw prelude when i tried your code i got errors and im such a noob i tried fixing them on my compiler but i just didnt see how. I hope this thread isnt breaking any forum rules

  8. #23
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    Well the code actually works but its starting halfway down the list and i need it to start at the beginning. any ideas. maybe im saving it wrong but all i did was copy all the cells i needed and pasted them into a .txt file. I tried saving it as a tab deliminated file in excel as well but it just saves and empty txt file without the needed data. this is going to be the longest thread ever!

  9. #24
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This works just fine on my end:
    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    int main()
    {
      //might need variables to store in the mean
      float mean = 0.0;
      float cell1= 0.0;
      float cell2= 0.0;
      float cell3= 0.0;
      float cell4= 0.0;
      float cell5= 0.0;
      float cell6= 0.0;
      float cell7= 0.0;
      float cell8= 0.0;
      float cell9= 0.0;
      float cell10= 0.0;
    
      //open up file
      ifstream inFile;
      inFile.open("healthy.txt");
      //verify open was sucessful
      if(!inFile.fail())	//ifopen did not fail
      {
        //read cell a1 through j1
        while (inFile >> cell1 >> cell2 >> cell3 >> cell4 >> cell5 >> cell6 >> cell7 >> cell8 >> cell9 >> cell10)
        {
          //calculate the mean for ROW and place in mean variable
          mean = (cell1+cell2+cell3+cell4+cell5+cell6+cell7+cell8+cell9+cell10)/10;
          cout<<mean<<endl;
        }
      }//end if
      else //open failed
        cout<<"Error opening file."<<endl;
    }//end main
    >void main()
    This isn't valid C++. The main function always returns int.

    >while(!inFile.eof()){
    stream.eof() wasn't designed to work as a loop condition. The eof flag only gets set after you fail to read from the stream due to end-of-file. That means that the last line of the file will usually be processed twice.
    My best code is written with the delete key.

  10. #25
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    thank you prelude. When I desk check the calculations though I have found that both our codes are skipping rows for some reason. The final output is giving the results for only 299 rows. These seem to be random rows when i check with my calculator but the last two results are correct. The first one is not correct and I havent quite pin pointed which row from the healthy.txt my program is starting at. any suggestions on making it start at row1 and go through row2000? im mucho confusedo on this problemo

  11. #26
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >any suggestions on making it start at row1 and go through row2000?
    All of the code I've given to you works properly for all of the rows. Any errors in the mean are likely to be floating-point rounding errors that are present in computer science due to the imprecise nature of representing floating-point values in binary. Sadly, if I can't reproduce your problem then I can't help you with it. I would guess that your output code is the problem, not the input code.
    My best code is written with the delete key.

  12. #27
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    i was beginning to think that. so you do get all 2000 rows? thank you so much for you help prelude

  13. #28
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Why wait to print out all the rows? If you already have the data in readable form so you can use your calculator as a check on the computer calculations then you can check the first 10 or 20 rows one at a time. Once you are sure the first x number of rows calculate correctly you can display every tenth row or every 100th to check on them. All it takes is a strategically placed display sequence and a call for input to a dummy variable to hold your place so you can check the results if you are sure that something is going haywire someplace along the way. (I believe a debugger can help you do this if you understand how to use one. I do it manually, however, so I can't help you with a debugger.) If you can consistently reproduce the problem and you can pinpoint where/when it happens, then someone may be able to help you figure it out, even if it isn't reproduceable up front on their machine.

  14. #29
    cout<<"Wakka Wakka"<<endl
    Join Date
    May 2004
    Posts
    35
    great idea elad. Only thing is the books I learned from only deal with sequential access files. It says in my textbooks that random and binary are beyond the scope of the books unfortunately. Im guessing that I should go learn about random access files with I/O #fstream. thanks for the help

  15. #30
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you can do it with any type filehere's an example to check every line one at atime, which in this case is every 10 items

    Code:
    #include <fstream.h>
    #include <iostream.h>
    intmain()
    { 
      float cell[10]; 
      ifstream in("healthy.txt"); 
      char ch; int lineNum = 1; 
      // Check for success :-) 
      while (in >> cell[0]) 
      { 
    	// If we get one, then assume the rest are there 
    	for (int i = 1; i < 10; i++) 
    	{ 
    	  in>> cell[i]; 
    	} 
     
    	float sum = 0.0; 
    	for (int i = 0; i < 10; i++) 
    	{ 
    	  sum += cell[i]; 
    	} 
    	 //display average for each row one at a time 
    	 //to check for error before moving to next row 
    	 cout << "average for row " << lineNum << " = "; 
    	 cout<< sum / 10 <<endl; 
    	 cin >> ch; 
    	 ++lineNum; 
      } 
      return 0;
    }
    Last edited by elad; 05-20-2004 at 02:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with code modification
    By DCMann2 in forum C Programming
    Replies: 7
    Last Post: 05-06-2008, 10:33 PM
  2. Merging the cells of excel from vc++
    By yongzai in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2006, 03:42 AM
  3. excel spreadsheets / blank cells
    By xddxogm3 in forum Tech Board
    Replies: 3
    Last Post: 06-14-2005, 12:10 PM
  4. reading pictures and array
    By sunoflight77 in forum C++ Programming
    Replies: 0
    Last Post: 05-09-2005, 03:16 PM
  5. Q: Simpliest way to use Excel file?
    By cDir in forum Windows Programming
    Replies: 10
    Last Post: 02-01-2002, 03:21 PM