![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2006
Location: London, Ontario, Canada
Posts: 16
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 | |
|
Professional Programmer
|
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:
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! ▄▄▄▄ |
|
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Dec 2005
Posts: 53
Rep Power: 3
![]() |
Quote:
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 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() |
|
|
|
|
|
|
#4 |
|
Expert Programmer
|
[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 |
|
|
|
|
|
#5 | |
|
Programmer
Join Date: Dec 2005
Posts: 53
Rep Power: 3
![]() |
Quote:
Try changing the function to void fileout(ofstream& out, int j) and calling it with fileout(out, i); |
|
|
|
|
|
|
#6 |
|
Expert Programmer
|
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 |
|
|
|
|
|
#7 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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();
} |
|
|
|
|
|
#8 |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
@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' |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
|
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;
} |
|
|
|
|
|
#10 | |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Quote:
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; |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|