I do not getting the output that I expect on my pogram

When I compile and run my program I expect 6 to be written to the output file,
but I get 10757710 written there.
What could be wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 //Reads three numbers from the file infile.dat, sums the numbers,
//and writes the sum to the file outfile.dat.
//(A better version of this program will be given in Display 6.2)
#include <fstream>

int main()
{
	using namespace std;
	ifstream in_stream;
	ofstream out_stream;
	
	in_stream.open("file.dat");
	out_stream.open("outfile.dat");
	
	int first, second, third;
	in_stream >> first >> second >> third;
	out_stream << "The sum of the first 3\n"
	           << "numbers in infile.dat\n"
			   << "is " << (first + second + third)
			   << endl;
			   
			   in_stream.close();
			   out_stream.close();
			   
			   return 0;
}

the input file has
1
2
3
4
Okay its all sorted out now. Thank you all
Hello Bopaki,

I am not sure because the program ran fine for me,

Two things I can thing of:

1. It is ALWAYS a good practice to initialize your variables.

2. You need to check that the input file is open before you use it.

What might be happening is that the variables "first, second and third" are uninitialized and contain garbage, whatever is in memory where the variable is stored and if the file stream is not good, file opened, you are not reading useful data into the variable thus adding whatever garbage is in the variables memory.

The following code is a fix to your program that is worth using. I changed a few things, but it is mostly what you have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//Reads three numbers from the file infile.dat, sums the numbers,
//and writes the sum to the file outfile.dat.
//(A better version of this program will be given in Display 6.2)
#include <iostream>
#include <string>
#include <fstream>
#include <chrono>
#include <thread>

int main()
{
	using namespace std;  // <--- Best not to use.
	//ifstream in_stream;
	ofstream out_stream;

	//in_stream.open("InFile.txt");
	std::string iFileName{ "file.dat" };

	std::ifstream inFile;

	inFile.open(iFileName);

	if (inFile.is_open())
	{
		std::cout << "\n File " << iFileName << " is open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(2));  // <--- Needs header files chrono" and "thread".
	}
	else
	{
		std::cout << "\n File " << iFileName << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
		exit(1);
	}
	out_stream.open("outfile.dat");

	int first, second, third;
	inFile >> first >> second >> third;
	out_stream << "The sum of the first 3\n"
		<< "numbers in infile.dat\n"
		<< "is " << (first + second + third)
		<< endl;

	inFile.close();
	out_stream.close();

	return 0;
}


The if/else statement can be duplicated for the output file, but it is not necessary because an output file will be created if it does not exist. Still a good idea to use.

Hope that helps,

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <fstream>

int main()
{
    const char infile[] = "file.dat" ;
    const char outfile[] = "outfile.dat" ;

    // values returned by main: 0 - success, 1 - input failure, 2 - output failure

    int first, second, third ;

    if( std::ifstream(infile) >> first >> second >> third )
    {
        return std::ofstream(outfile) << "sum of the first 3 numbers in '" << infile
                                      << "' is: " << first + second + third << '\n' ? 0 : 2 ;
    }
    else return 1 ;
}
Topic archived. No new replies allowed.