954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Error reading file

i tried this

#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;


int main() 
{
    // Declare input stream
    ifstream fin;
    int min, max, val;
    int isFirst = true;
    fin.open("inFile.txt");



	fin >> isFirst;
    min = isFirst;
    max = isFirst;

    while (fin >> val)
    {

          fin >> val;
          if (val < min)
          val = min;
          else
          val = max;
		  
    }



    fin.close();

    cout << "Largest value is " << max << ", smallest value is " << min << "\n";
	return 0;
}


<< moderator edit: added code tags : [code][/code] >>
and it prints this
Largest value is 1, smallest value is 1
Press any key to continue
can someone suggest me what's i am doing wrong here

desidude
Newbie Poster
15 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 
while (fin >> val)
    {

          fin >> val;
          if (val < min)
          val = min;
          else
          val = max;
		  
    }


Changemin and/or max, not val.

while ( fin >> val )
   {
      if ( val < min )
      {
         min = val;
      }
      if ( val > max )
      {
         max = val;
      }
   }


It might also be helpful to post the contents of"inFile.txt".

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

i tried the way u suggested me but it still gives same output
the content of "inFile.txt" is
100
45
74
45
86
94
75
96
56
85
82
88
88
56
77

desidude
Newbie Poster
15 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

>i tried the way u suggested me but it still gives same output
Odd.

#include <fstream>
#include <iostream>
using namespace std;


int main() 
{
   ifstream fin("inFile.txt");
   int min, max, val;
   if ( fin >> val )
   {
      min = max = val;
      while ( fin >> val )
      {
         if ( val < min )
         {
            min = val;
         }
         if ( val > max )
         {
            max = val;
         }
      }
   }
   cout << "Largest value is " << max << ", smallest value is " << min << "\n";
   return 0;
}

/* my output
Largest value is 100, smallest value is 45
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

i tried every possible way u suggested me and it still doesnt work. could it be some other problem?

desidude
Newbie Poster
15 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Did you copy and paste what dave wrote and compiled it?

winbatch
Posting Pro in Training
466 posts since Feb 2005
Reputation Points: 68
Solved Threads: 18
 

>fin >> isFirst;
I think you mean:

fin>> val;

isFirst doesn't strike me as a data variable, it seems more like a status variable. You don't even need it. Oddly enough, I didn't bother looking at Dave's code, but my solution is only slightly different (but it avoids undefined behavior in the case of an empty or non-existent file):

#include <fstream>
#include <iostream>
using namespace std;

int main() 
{
  ifstream fin("inFile.txt");
  int val;

  if (fin >> val) {
    int min, max;

    min = max = val;

    while (fin >> val)
    {
      if (val < min)
        min = val;

      if (val > max)
        max = val;
    }

    cout << "Largest value is " << max << ", smallest value is " << min << "\n";
  }

  return 0;
}

Great minds think alike, I suppose.

>i tried every possible way u suggested me and it still doesnt work.
Then instead of looking at the suggestions and implementing them they wayyou think they should be done, actually try them as they are written. In my experience, Dave doesn't post untested code.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

thx a bunch guys
i am sorry dave it was my fault in implementing codes . its working
thx

desidude
Newbie Poster
15 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

again need help guys
trying to calculate average of all numbers in previous program
here is what i did but it gives average = 77

#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;



int main() 

{
   	
   ifstream fin("inFile.txt");
   ofstream fout("outFile.txt");
   
   if (fin.fail())
   {
	   cout<<"Input file opening failed.\n";
	   exit(1);
   }
   if (fout.fail())
   {
	   cout<<"output file opening failed.\n";
	   exit(1);
   }
   int min, max, val;
   double sum = 0;
   int count = 0, next ;
          
 
	  
   if ( fin >> val )
	   
   {
	     
	  
      min = max = val;
      while ( fin >> val )
      {
		 
			  
         if ( val < min )
         {
            min = val;
         }
		 
         if ( val > max )
         {
            max = val;
		 }
		  
      }
	  sum = sum+val;
	   count=count++;

   }
           
   

 sum=sum/count;
	

   
   fout<<"average score is:"<< sum << "\n";
   cout<<"average score is:"<< sum << "\n";

   
   fout << "Largest value is " << max << ", smallest value is " << min << "\n";
   cout << "Largest value is " << max << ", smallest value is " << min << "\n";
   fin.close();
   fout.close();
   
   return 0;
}

Code tags added. -Narue

what am i doing wrong here?

desidude
Newbie Poster
15 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

>here is what i did but it gives average = 77
Um...what's the problem with getting the correct answer?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

The average is 76.4666.

desidude, since your sum and count are modified after the while loop, only the last value of your numbers gets added to sum, and count gets incremented once, from zero to one. And 77 is your last value.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

Just put the

sum = sum+val;
   count=count++;

inside your while loop, and it shuld work.

zyruz
Junior Poster in Training
60 posts since Jun 2005
Reputation Points: 10
Solved Threads: 5
 

Zyruz, i tried that before but it gives 74.78 which is 1047( 100 less than total(1147))/14(it is suppose to be 15).

desidude
Newbie Poster
15 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Don't forget to account for the first value:

if ( fin >> val )
  {
    sum += val;
    ++count;
    min = max = val;

    while ( fin >> val )
    {
      if ( val < min )
      {
        min = val;
      }

      if ( val > max )
      {
        max = val;
      }

      sum += val;
      ++count;
    }
  }

On a side note, this is very, very wrong:

count=count++;

It modifies count twice between sequence points, which is undefined.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

thx alot guys

desidude
Newbie Poster
15 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You