GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Nov-2007, 19:32
Megan Megan is offline
New Member
 
Join Date: Nov 2007
Posts: 1
Megan is on a distinguished road

I/O streams


Hello there,

I am new to this forum and I am new to c++. Anyway, I was doing so well in the class so far, oh boy, until the I/O chapter. I am so lost. The more i read, the more i am getting confused. Please help me.
I save the input file below called (number.dat) in notepad. I don't know if I have to save in notepad or within c++ (..as to what...?) I am so confused with infile stuff. I know the biggest integers is 47. How do i get this 47 to output?

the input file:
12 5 45 -3
13 -2
14 47 43

I have write those codes with the little that I was able to understand by reading 2 chapters on I/O streams. I'm hoping that someone here could help me because I'm so hopeless and need to turn in this lab by tuesday. Thank you so much in advance.

Megan

CPP / C++ / C Code:
/* Megan D. CSE 110 Lab 5
This program processes an input file that contains integers. This program reads
the input file and determine the biggest integer contained in the file and 
outputs the result*/

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

void number(ifstream& fin, ofstream& fout);

int main ()
{
	ifstream in_file;
	ofstream out_file;

	fin.open("number.txt");
	if (fin.fail())
	{
		cout << "Opening Input file failed." << endl
			 << "Program was terminated." << endl;
		exit(1);
	}

    fout.open("number.txt");
    if (fout.fail( ))
    {
        cout << "Output file opening failed.\n";
        exit(1);
    }

	number(fin, fout);

	fin.close();
	fout.close();

	cout << "End of program.\n";
	return 0;
}

	out_file.close();
	in_file.close();

	return 0;
}

void number(ifstream& fin, ofstream& fout)

double next;
while (number >> next)
{
	cout << "The larger integer in the file is" << endl;

  #2  
Old 05-Nov-2007, 07:46
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 440
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: I so need help with I/O streams


Is it your job to decide the format of the file? If so, I would put one integer per line. That will make it easier to parse. Also, your code is all bad. Try doing it like this:

1) Open the file for reading
2) Read a line into an integer.
3) If that integer is greater than the greatest integer, set the greatest to that one.
4) Loop back to step 2.
5) After the end of the loop, print the greatest integer to the screen.

It doesn't look like you'll need anything but a main() function for this. The value of GreatestInteger will need to be initialized to the value of the first line. Try that and post your code if you are still having problems.
  #3  
Old 05-Nov-2007, 08:21
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: I so need help with I/O streams


Quote:
Originally Posted by fakepoo
Is it your job to decide the format of the file? If so, I would put one integer per line. That will make it easier to parse. Also, your code is all bad. Try doing it like this:
I'd add one line to this:

1) Open the file for reading
1a) Read a line into the max integer. This initializes the max integer to one of the numbers in the file, which could be the max
2) Read a line into an integer.
3) If that integer is greater than the greatest integer, set the greatest to that one.
4) Loop back to step 2.
5) After the end of the loop, print the greatest integer to the screen.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
A problem using streams. (I've spent a month trying to solve it) Mararia CPP / C++ Forum 8 14-Dec-2006 16:52
Menu of character strings aijazbaig1 C Programming Language 21 27-Jul-2006 03:07
??? if no fclose() balusss C Programming Language 3 07-Jul-2006 02:48
Reading socket input streams. AJSHOPE CPP / C++ Forum 2 21-May-2006 16:42

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:47.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.