Reading largest/smallest of 4 numbers from file

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 22005 Views - Last Post: 02 April 2010 - 02:31 PM Rate Topic: -----

#1 czuker36   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 25-February 10

Reading largest/smallest of 4 numbers from file

Posted 30 March 2010 - 07:08 PM

I am having a tough time with this project. Here are the instructions:

For this project, you will need to write a program that has two functions in it.
Input file: input.txt
Output file: output.txt

Your program should read from the file and use a function named largest that will return the largest
of the 4 numbers and a function named smallest that will return the smallest of the 4 numbers.
Print out the first name, last name, largest number and smallest number to the output file (See file output3.txt).
Format your output so that it is similar to the output file.
Global variables are NOT allowed although Global constants are fine.
____________________________________________________________________________________________________

I am lost on what to do for the largest() and smallest() functions. I've been searching for some examples of
how to get the largest/smallest of 4 numbers and have found people are using "arrays" which I haven't been introduced to yet.

I also am not sure how I would get the program to do this for each and every line in the file. Would I use the getline() function?

Here is all I have so far. It's not much, but I've tried several things and deleted them due to no success.
This comes up with a debug error also, I'm not sure why. Any advice/examples would be greatly appreciated. Thanks!

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

void largest();
void smallest();

void main() 
{ 
   string s; // File name
   int a, b, c, d; // Numbers
   ifstream in_stream;
   ofstream out_stream;

   cout << "Enter input file name: ";
   cin >> s;
   in_stream.open(s.data());

   cout << "Enter output file name: ";
   cin >> s;
   out_stream.open("output.txt"); // Debug error shows up here
  
   while(!in_stream.eof())
   { 
     in_stream >> a;

      if(a>b && a>c && a>d) // Is this legal?
      { 
	     out_stream << a; // This if-else statement is wrong. I know 
      }
	  else
	  {
	     out_stream << a;
	  }
   }
   in_stream.close();
   out_stream.close();
}

void largest()
{
   // ???
}

void smallest()
{
   // ???
}


Is This A Good Question/Topic? 0
  • +

Replies To: Reading largest/smallest of 4 numbers from file

#2 hlemke   User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 57
  • Joined: 18-January 10

Re: Reading largest/smallest of 4 numbers from file

Posted 30 March 2010 - 07:28 PM

You should create a couple of temporary variables inside the function.
One stores the "current" value, and one stores the "max" value, and you just
keep comparing current to max and setting max to current if current is greater,
until you've hit the end of the file. It might look something like this:

int largest()
{
   int current, max;
   in_stream >> current;
   max = current;
   while(in_stream) //this is a neat little trick; basically says "while there's still stuff in the file, do this"
   {
      in_stream >> current;
      if(current > max)
         max = current;
   }
   return max;
}



I'll let you write smallest(), as it follows a pretty similar form. Oh, and you definitely want them to return integers, because that way you can just say someVariable = largest() which is fun times.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#3 czuker36   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 25-February 10

Re: Reading largest/smallest of 4 numbers from file

Posted 30 March 2010 - 07:36 PM

Ok thanks, that clears up some things. :smile2: I can definitely work with it.

Also, what should the parameters of my functions be? Should I include "string& name;" in there too and call all parameters by reference?
Was This Post Helpful? 0
  • +
  • -

#4 hlemke   User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 57
  • Joined: 18-January 10

Re: Reading largest/smallest of 4 numbers from file

Posted 30 March 2010 - 07:51 PM

View Postczuker36, on 30 March 2010 - 06:36 PM, said:

Ok thanks, that clears up some things. :smile2: I can definitely work with it.

Also, what should the parameters of my functions be? Should I include "string& name;" in there too
and call all parameters by reference?

To be perfectly in good form you ought to pass them an istream by reference, as it's not good programming
form to use a specific istream or ostream inside of a function. So you might pass a parameter like this:
void largest(istream& in). Since you're just starting out it's likely your professor won't care...
if he/she does, they'd probably have said something, at any rate... but good form is good.
Was This Post Helpful? 0
  • +
  • -

#5 czuker36   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 25-February 10

Re: Reading largest/smallest of 4 numbers from file

Posted 30 March 2010 - 08:18 PM

OK so this is a little better, but I'm still stumped on what to do in the
main function to get the output.txt file to look right. I have 2 errors showing
up about largest() and smallest() taking 0 arguments when I call them in the main,
and I don't know what parameters to use for them. I also want to know how to get
the firstnam and lastname to show up in the output.txt. Should I just leave them
as they are already in the input file (and worry about formatting later)?

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

void largest(int& current, int& max);
void smallest(int& current, int& min);

void main() 
{ 
   string s, firstname, lastname;
   int a, b, c, d; // Numbers
   ifstream inp;
   ofstream outp;

   cout << "Enter input file name: ";
   cin >> s;
   inp.open(s.data());

   cout << "Enter output file name: ";
   cin >> s;
   outp.open("output.txt");
  
   while(!inp.eof())
   { 
      outp << firstname << lastname;
      largest();
      smallest();
   }
   inp.close();
   outp.close();
}

int largest(ifstream& in_stream)
{
   int current, max;

   in_stream >> current;
   max = current;
   while(in_stream)
   {
      in_stream >> current;
      if(current > max)
         max = current;
   }
   return max;
}

int smallest(ifstream& in_stream)
{
   int current, min;

   in_stream >> current;
   min = current;
   while(in_stream)
   {
      in_stream >> current;
      if(current < min)
         min = current;
   }
   return min;
}


Please help me work out the kinks :sad2:
Was This Post Helpful? 0
  • +
  • -

#6 czuker36   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 25-February 10

Re: Reading largest/smallest of 4 numbers from file

Posted 30 March 2010 - 08:38 PM

I've plugged away at it a bit more and I now have 0 errors :bananaman:...
But bananaman shouldn't be celebrating so enthusiastically just yet.
When I look at the output file, it is completely blank.
I'm sure I missed something big here... :helpsmilie:

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

int largest(ifstream& in_stream);
int smallest(ifstream& in_stream);

void main() 
{ 
   string s, firstname, lastname;
   ifstream inp;
   ofstream outp;

   cout << "Enter input file name: ";
   cin >> s;
   inp.open(s.data());

   cout << "Enter output file name: ";
   cin >> s;
   outp.open("output.txt");
  
   while(!inp.eof())
   { 
      outp << firstname << lastname;
      largest(inp);
      smallest(inp);
   }
   inp.close();
   outp.close();
}

int largest(ifstream& in_stream)
{
   int current, max;

   in_stream >> current;
   max = current;
   while(in_stream)
   {
      in_stream >> current;
      if(current > max)
         max = current;
   }
   return max;
}

int smallest(ifstream& in_stream)
{
   int current, min;

   in_stream >> current;
   min = current;
   while(in_stream)
   {
      in_stream >> current;
      if(current < min)
         min = current;
   }
   return min;
}

Was This Post Helpful? 0
  • +
  • -

#7 hlemke   User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 57
  • Joined: 18-January 10

Re: Reading largest/smallest of 4 numbers from file

Posted 30 March 2010 - 10:06 PM

Filenames are C-strings rather than strings. you should say
inp.open(s.c_str())

Then the input file should open properly. Also, don't use eof();
it's not very reliable. I would say while(inp) instead; eof()
will under certain circumstances read the last thing in the file
twice due to the way it is written, whereas while(nameOfFilestream)
will always work well.

Anyway, if you're not getting values in your output file, it looks
to be because you're not actually sending the largest and smallest
values to the file. You should say
outp << largest(inp);
outp << smallest(inp);

Those functions return integers, and so you'll need to actually
write said integers to the file.
Was This Post Helpful? 0
  • +
  • -

#8 czuker36   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 25-February 10

Re: Reading largest/smallest of 4 numbers from file

Posted 31 March 2010 - 06:41 AM

OK when you say to write said integers to the file, should I declare "int a, b, c, d;" in the main() and then somehow include "inp >> a" "inp >> b" etc? Also idk if that is the reason, but the output file still is showing no names and only displays "-858993460-858993460" :bored:

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

int largest(ifstream& in_stream);
int smallest(ifstream& in_stream);

void main() 
{ 
   string s, firstname, lastname;
   ifstream inp;
   ofstream outp;

   cout << "Enter input file name: ";
   cin >> s;
   inp.open(s.c_str());

   cout << "Enter output file name: ";
   cin >> s;
   outp.open("output.txt");
  
   while(inp)
   { 
      outp << firstname << lastname;
      outp << largest(inp);
      outp << smallest(inp);
   }
   inp.close();
   outp.close();
   return;
}

int largest(ifstream& in_stream)
{
   int current, max;

   in_stream >> current;
   max = current;
   while(in_stream)
   {
      in_stream >> current;
      if(current > max)
         max = current;
   }
   return max;
}

int smallest(ifstream& in_stream)
{
   int current, min;

   in_stream >> current;
   min = current;
   while(in_stream)
   {
      in_stream >> current;
      if(current < min)
         min = current;
   }
   return min;
}

Was This Post Helpful? 0
  • +
  • -

#9 hlemke   User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 57
  • Joined: 18-January 10

Re: Reading largest/smallest of 4 numbers from file

Posted 31 March 2010 - 12:47 PM

Why is this a while loop?
   while(inp)
   { 
      outp << firstname << lastname;
      outp << largest(inp);
      outp << smallest(inp);
   }

It shouldn't be a loop, as you only want to do that once.

And come to think of it, you should say this:
int largest(ifstream& in_stream)
{
   int current, max;

   in_stream >> current;
   max = current;
   while(in_stream >> current) //this is the big change
   {
      if(current > max)
         max = current;
   }
   return max;
}

while(inp), it occurs to me, is not going to work... basically you're saying "while in_stream is able to read into current, do that, and then do the stuff in this loop". Otherwise I believe you end up skipping over values, but someone much wiser than me in the workings of filestreams ought to be consulted on that.
Was This Post Helpful? 0
  • +
  • -

#10 citus   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 140
  • Joined: 28-March 10

Re: Reading largest/smallest of 4 numbers from file

Posted 31 March 2010 - 03:38 PM

I have reviewed your code and found a few basic problems.

      /**** You have not assigned any values to firstname or lastname ****/
      outp << firstname << lastname;



	/******** You don't use either of these two lines so I commented them out
        cout << "Enter output file name: ";
        cin >> s;
	*********/

        outp.open("output.txt");


There is no point in leaving those two lines of code in if you are going to hardcode the file name into your program.

I have reviewed your project3.txt file as well, it is formatted text. The file has first name, last name, and four numbers, they are all separated by a space and the line ends with a \n. You can use this to your advantage. Here is one suggestions:
Inside of your while loop parse the data into a string, multiple strings, arrays, or a mix of all of them. Rewrite largest() and smallest() to take strings, integers, or arrays of strings or integers instead of references to an ifstream object. The code could look something like the following.
while(inp.getline(someString))
{
     //Each iteration will populate someString with one line of your input file, making it easy to deal with
     //Process someString however you want
     //Pass data to largest and smallest functions
     //Do not forget to write first and last name of each line to your file as well
}


This should simplify your program a lot. The way you currently have it setup is prone to errors.

Here are some links to help you out:
getline()
substr()
Was This Post Helpful? 2
  • +
  • -

#11 czuker36   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 25-February 10

Re: Reading largest/smallest of 4 numbers from file

Posted 31 March 2010 - 05:20 PM

Ok guys, I implemented a little bit of both of your ideas, but I'm getting an error at the getline()... I am not sure how I would make/assign a string that represents every line in the file. This is all so new to me, it seems impossible to save my mess of code...

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

int largest(string& fname, string& lname, int& large);
int smallest(string& fname, string& lname, int& small);

void main() 
{ 
   string s, line;
   int a;
   ifstream inp;
   ofstream outp;

   cout << "Enter input file name: ";
   cin >> s;
   inp.open(s.c_str());

   cout << "Enter output file name: ";
   cin >> s;
   outp.open("output.txt");
  
   while(inp.getline())
   {
      outp << largest(inp, outp);
      outp << smallest(inp, outp);
   }

   inp.close();
   outp.close();
   return;
}

int largest(string& fname, string& lname, int& max)
{
   int current, max;

   in_stream >> current;
   max = current;
   while(in_stream >> current)
   {
      in_stream >> current;
      if(current > max)
      max = current;
   }
   return max;
}

int smallest(string& fname, string& lname, int& min)
{
   int current, min;

   in_stream >> current;
   min = current;
   while(in_stream >> current)
   {
      in_stream >> current;
      if(current < min)
      min = current;
   }
   return min;
}


Anyone brave enough to try and help me, I thank you... I bet, to you, it looks like a 5 year old wrote this program.... Sorry in advance :sad2:
Was This Post Helpful? 0
  • +
  • -

#12 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Reading largest/smallest of 4 numbers from file

Posted 31 March 2010 - 06:18 PM

to read line by line, change your while loop to this
   while(getline(inp,line))


Was This Post Helpful? 0
  • +
  • -

#13 czuker36   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 25-February 10

Re: Reading largest/smallest of 4 numbers from file

Posted 31 March 2010 - 06:56 PM

View PostImaSexy, on 31 March 2010 - 05:18 PM, said:

to read line by line, change your while loop to this
   while(getline(inp,line))



I did that and it gave these errors:

Error 1 error C2660: 'largest' : function does not take 2 arguments

Error 2 error C2660: 'smallest' : function does not take 2 arguments

Error 3 error C2082: redefinition of formal parameter 'max'

Error 4 error C2065: 'in_stream' : undeclared identifier
^ (This one occurred twice)

Error 6 fatal error C1903: unable to recover from previous error(s); stopping compilation


This project is a trainwreck
Was This Post Helpful? 0
  • +
  • -

#14 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Reading largest/smallest of 4 numbers from file

Posted 31 March 2010 - 07:02 PM

did you bother even reading the errors?

Quote

Error 1 error C2660: 'largest' : function does not take 2 arguments


well the function largest() accepts a different amount of parameters

Quote

Error 4 error C2065: 'in_stream' : undeclared identifier
^ (This one occurred twice)

the variable in_stream is not declared, so you have not defined it anywhere...define it! it looks like you were trying to do a stringstream or something

Quote

Error 3 error C2082: redefinition of formal parameter 'max'


You declared max more than once in the same scope!

now i didnt even look at your code to tell you what the errors were, i just saw the error log. The fact that the compiler displays the errors is for your benefit :)
Was This Post Helpful? 0
  • +
  • -

#15 czuker36   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 25-February 10

Re: Reading largest/smallest of 4 numbers from file

Posted 31 March 2010 - 07:24 PM

Quote

now i didnt even look at your code to tell you what the errors were, i just saw the error log. The fact that the compiler displays the errors is for your benefit :)


Yeah sorry about that, I just don't understand some of the error messages because I'm completely new to programming. But your explanations helped me manage to fix up the code a bit. Here's what I have:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;

int largest(string& fname, string& lname, int& max);
int smallest(string& fname, string& lname, int& min);

void main() 
{ 
   string s, firstname, lastname, line;
   int large, small;
   ifstream inp;
   ofstream outp;

   cout << "Enter input file name: ";
   cin >> s;
   inp.open(s.c_str());

   cout << "Enter output file name: ";
   cin >> s;
   outp.open("output.txt");
  
   while(getline(inp, line))
   {
      outp << largest(firstname, lastname, large);
      outp << smallest(firstname, lastname, small);
   }

   inp.close();
   outp.close();
   return;
}

int largest(string& fname, string& lname, int& max)
{
   int current;
   ifstream in_stream;

   in_stream >> current;
   max = current;
   while(in_stream >> current)
   {
      in_stream >> current;
      if(current > max)
      max = current;
   }
   return max;
}

int smallest(string& fname, string& lname, int& min)
{
   int current;
   ifstream in_stream;

   in_stream >> current;
   min = current;
   while(in_stream >> current)
   {
      in_stream >> current;
      if(current < min)
      min = current;
   }
   return min;
}


The only issue is that the output file shows this:

-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460-858993460 :blink:

I think it is showing that number as many times as there are lines in the txt file. So at least the getline() kind of works, right? I just have no clue how I can get the names and correct numbers to come out.

This post has been edited by czuker36: 31 March 2010 - 07:35 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2