Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 27th, 2006, 4:27 PM   #1
can342man
Newbie
 
can342man's Avatar
 
Join Date: Jan 2006
Location: London, Ontario, Canada
Posts: 16
Rep Power: 0 can342man is on a distinguished road
Wink Simple output problem

Hi,

I have been struggling to understand why this simple program is generating the exact same number 100 times in the output.dat file. If I display the results on the screen (cout), the program works fine, generating numbers from 1 to 99 in order.

Is it true that a variable will only pass the first value to a function but not the rest of the values?

How would I go about trying to dump all the numbers generated from 1 to 99 into the output file by calling the function fileout()?

There are no compilation errors in this program, just that it produces the wrong result when the numbers are sent to the fileout() function.

//Output of data to file "output.dat"

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

int fileout(int j);

int main () {

        int i;
        for (i=0; i<100; i++) {
                fileout(i);
        }

}

int fileout(int j)
{
        ofstream out("output.dat");
        if(!out) {
                cout << "Cannot open file." << "\n";
                return 1;
        }
        for (int k=0; k<100; k++) {
                out << j << "\n";
        }
        out.close();
        return 0;
}

Really appreciate it if anybody has come across something like this before.

Thanks.
__________________
Greatness courts failure and solitude.
--- Anonymous
can342man is offline   Reply With Quote
Old Jan 27th, 2006, 4:51 PM   #2
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
       for (int k=0; k<100; k++) {
                out << j << "\n";
        }

In English, if k is less than 100, output j to the screen (or file in your case) one more time. How many times do you want the output saved to the .dat file? If only one time then use

       for (int k=0; k<1; k++) {
                out << j << "\n";
        }
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old Jan 27th, 2006, 5:30 PM   #3
Jason Isom
Programmer
 
Join Date: Dec 2005
Posts: 53
Rep Power: 3 Jason Isom is on a distinguished road
Quote:
Originally Posted by can342man
Hi,

I have been struggling to understand why this simple program is generating the exact same number 100 times in the output.dat file. If I display the results on the screen (cout), the program works fine, generating numbers from 1 to 99 in order.

Is it true that a variable will only pass the first value to a function but not the rest of the values?

How would I go about trying to dump all the numbers generated from 1 to 99 into the output file by calling the function fileout()?

There are no compilation errors in this program, just that it produces the wrong result when the numbers are sent to the fileout() function.
.
snip

Really appreciate it if anybody has come across something like this before.

Thanks.
Trace your program on paper. It'll probably become more clear.

First off your main problem (although not your only problem...)

for (int k=0; k<100; k++) {
    out << j << "\n";
}

You're outputting the same value 100x. Namely the value of the variable that you passed to the fileout function. You probably intended to use k?

Anyways, I'm a little confused on what you're trying to accomplish. You have this in main...

int i;
for (i=0; i<100; i++) {
        fileout(i);
}

So basically you're calling fileout(...) 100x with the values 0-99.

In fileout(...) you have another loop that loops 100x with the values 0-99, but it's only outputting the value you passed to the function.

An important thing you have to realize though, is that when you open a file with
ofstream out("output.dat");
you effectively overwrite whatever USED to be there. If you want to append to an already existing file, you need to use ios::app

You need to figure out whether you want the loop to be in main, or fileout. You can technically do it either way, but your program is rather trivial so I can't figure out what way you SHOULD do it. If you're trying to understand the appending of files, just take out the loop in fileout(...), use ios::app and append the integer that you pass to the function to the file. If you don't care about appending, take out the loop in main and take out the parameter of your fileout function and just use the loop in fileout()
Jason Isom is offline   Reply With Quote
Old Jan 27th, 2006, 6:25 PM   #4
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
[php]
#include <iostream>
#include <fstream>
using namespace std;

int fileout(int j);

int main () {

int i;
ofstream out("output.dat");

if(!out) {
cout << "Cannot open file." << "\n";
return 1;
}

for (i=0; i<100; i++) {
fileout(i);
}

out.close();

}

void fileout(int j)
{
out << j << "\n";
}
[/php]
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old Jan 27th, 2006, 7:31 PM   #5
Jason Isom
Programmer
 
Join Date: Dec 2005
Posts: 53
Rep Power: 3 Jason Isom is on a distinguished road
Quote:
Originally Posted by Kilo
snip
Actually, that won't work either because of scoping rules. You could do that if you pass a reference to the ofstream. Not to mention your prototype differences from the definition.

Try changing the function to

void fileout(ofstream& out, int j)

and calling it with

fileout(out, i);
Jason Isom is offline   Reply With Quote
Old Jan 27th, 2006, 10:03 PM   #6
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
lol your so right.. i just did a quick edit.. my fault to all!
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old Jan 28th, 2006, 8:31 AM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
You don't need a function do do something as trivial as out << num << std::endl;. I'd personally do it like this:
#include <iostream>
#include <fstream>

int writeNumbers(std::string filename, int start, int end);

int main ()
{
	std::string filename;
	int start, end;
	
	std::cout << "Enter the name of the file you want to write to: ";
	std::cout.sync(); // flushes the output buffer to make sure the string is shown
	std::getline(cin, filename); // gets the entire line and stores it in "filename"
	
	std::cout << "Enter the starting value: ";
	std::cout.sync();
	std::cin >> start; // gets the starting value and stores it in "start"
	
	std::cout << "Enter the ending value: ";
	std::cout.sync();
	std::cin >> end;
	
	writeNumbers(filename, start, end);
	
	return 0;

}

bool writeNumbers (std::string filename, int start, int end)
{
	std::ostream out(filename);
	
	if (start < end)
	{
		for (int i = start; i <= end; i++)
		{
			out << i << std::endl;
		}
	}
	else
	{
		for (int i = start; i >= end; i--)
		{
			out << i << std::endl;
		}
	}
	
	out.close();
}
I'll let you add the error-checking.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jan 28th, 2006, 9:57 AM   #8
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
@Ooble: I don't want to sound like an ass, but there are a few basic mistakes in the code you posted above. Although you did only say it's "like" the way you would do it, at least.

Take a look at the return type in the prototype of 'writeNumbers' - then look at the return type in it's definition. Your missing a 'std::' on one of your cin's. And when you open the file stream, perhaps you meant: std:: ofstream out( filename.c_str() ); (I put the space to avoid a smiley face).


There's one more thing, but it's more of question: in the code you posted above, you synchronize the output buffer using 'std::cout.sync();'. Is that valid? Dev-C++ spits and error about this. The error: 'struct std::ostream' has no member named 'sync'
Cache is offline   Reply With Quote
Old Jan 28th, 2006, 11:41 AM   #9
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
Well i have not done any coding in a while so this proberly isnt the best or the cleanest. but it works
//Output of data to file "output.dat"
 
#include <iostream>
#include <fstream>
 
using namespace std;
 
int main () {
int i=0;
 
ofstream out("output.dat");
if(!out) {
	cout << "Cannot open file." << "\n";
	return 1;
}
 
while (i < 100) {
	out << i << endl;
	i++;
}
 
out.close();
 
return 0;
}
HaCkeR is offline   Reply With Quote
Old Jan 28th, 2006, 1:01 PM   #10
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Quote:
Originally Posted by Cache
@Ooble: I don't want to sound like an ass, but there are a few basic mistakes in the code you posted above. Although you did only say it's "like" the way you would do it, at least.

Take a look at the return type in the prototype of 'writeNumbers' - then look at the return type in it's definition. Your missing a 'std::' on one of your cin's. And when you open the file stream, perhaps you meant: std:: ofstream out( filename.c_str() ); (I put the space to avoid a smiley face).


There's one more thing, but it's more of question: in the code you posted above, you synchronize the output buffer using 'std::cout.sync();'. Is that valid? Dev-C++ spits and error about this. The error: 'struct std::ostream' has no member named 'sync'
I forgot to add: "not tested, but the essence is there". It's supposed to be a bool in both cases, by the way, though that's really up to whoever's coding the error-checking.

Oh, and I couldn't remember which one was ANSI C++ and which one was the old version that works with iostream.h. Do this instead:
std::cout << std::flush;
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:08 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC