Add values

Pages: 12
I am in need of help writing some code to open a file take two values, then add them together and output them to another file. I have very little programming experience and am looking for any help.

I have been reading that and I can't seem to make the connection.
What confuses you?
I don't see how I can tell the program to look at two different lines in the file
Last edited on
I have very little programming experience and am looking for any help.

If you do not really know C++, then you should read the tutorial (entirely):
http://www.cplusplus.com/doc/tutorial/

Edit: please give us your current C++ code, and sample input file.
Last edited on
Huh?
1
2
int value1, value2;
myfile >> value1 >> value2;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// copy from one file to another
#include <iostream>
#include <fstream>
using namespace std;

int main() {

	int a, b, c;

	ifstream inFile("intinput.txt", ios::binary); //opens intinput file
	ofstream outFile("results.txt", ios::binary); //creates output file

	c = a + b;


	while (inFile.good())  // Though perhaps this condition is wrong
	{
		outFile.put(inFile.get());
	}

	inFile.close();
	outFile.close();
}

This is what I have so far.
Did you even read my post?
@ slour:

1) Why are you opening in binary mode?
2) You forgot to actually read from the file.
3) You don't need to use a while() loop to write to a file.

input_file >> n; // read from input file to n
output_file << n; // write n to output file

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
// copy from one file to another
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {

	int a, b, c;
	string line;
	ifstream inFile;
	inFile.open("intinput.txt");
	ofstream outFile;
	outFile.open("results.txt");
		if (inFile.is_open());
		{
			while (getline(inFile, line));
			{

				cout << line << "results.txt";
			}
			inFile >> a >> b;

			c = a + b;

			outFile << c;

			inFile.close();
			outFile.close();
		}

}

Okay so I looked at what you said and this is what I have come up with
huele a gato encerrado


> take two values, then add them together and output them
¿can you do that? just that, and use the standard input and output streams (e.g. std::cin, std::cout)
Later you could simply do stream redirection
$ ./program.bin < input.txt > output.txt

or a textual replace in your program 's/std::cin/input/g'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

int main(){
	
	string myfile;
	float a = 0;
	int b=0 ;

	cout << "Enter the first value." << endl;
	getline(cin, myfile);
	stringstream(myfile) >> a;
	cout << "Enter the second value."<<endl;
	getline(cin, myfile);
	stringstream(myfile) >> b;
	cout << "The total is:" << a+b << endl;
	return 0;
}


okay I was able to do that using strings.
So I have been working on this and have gotten to the point where I can add the variables and output them to a different file, but I don't know the code to go tot he next line. This is what I have so far.
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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
using std::cerr;
using std::cout;
using std::endl;
using std::ifstream;

int main(){

		ifstream indata;
		ofstream outfile;
		int num1, num2, num3;

		indata.open("intinput.txt");
		outfile.open("results.txt");

		if (!indata) {
			cerr << "Error file cannot be opened!" << endl;
			exit(1);

		}
		indata >> num1 >>num2;
		while (!indata.eof()){
			outfile << "The number is:" << num1+num2 << endl;
			indata >> num3;
			
		}


		indata.close();
		outfile.close();
		cout << "End of file reached..." << endl;
		return 0;

}
Please give a sample input file, and put it in [output][/output] tags.
1 3
5 5
7 3
3 4
9 5
5 4
http://www.cplusplus.com/forum/beginner/121876/#msg664256
Why exactly are you reading in strings then converting to a float and an int?


You can input directly to integers/floats/doubles

1
2
3
4
5
int a;
cin >> a;

float b;
cin >> b;


Also if that is your input file basically you want to loop till it reads everything while inputting to a and b.

It should pretty much look like this:


1
2
3
4
5
6

while( in.good() )
{
    in >> a >> b;
    out << ( a + b ) << endl; //or how ever you are separating the values
}
Last edited on
To get to the next line when writing to the file, just send the file "\n" when you want subsequent data on the next line. To get the next line when reading a file in, use getline again. If you have many lines, you could use it within a loop.
Last edited on
I wouldn't necessarily suggest using '\n' when outputting. It can vary based on operating systems. Windows uses "\r\n" (CRLF) , UNIX uses '\n' (LF) , Mac before version 9 uses '\r' (CR) , and later mac uses '\n' (LF) there is no universal newline.


http://en.wikipedia.org/wiki/Newline
wiki wrote:

LF: Multics, Unix and Unix-like systems (GNU/Linux, OS X, FreeBSD, AIX, Xenix, etc.), BeOS, Amiga, RISC OS and others.
CR+LF: Microsoft Windows, DEC TOPS-10, RT-11 and most other early non-Unix and non-IBM OSes, CP/M, MP/M, DOS (MS-DOS, PC DOS, etc.), Atari TOS, OS/2, Symbian OS, Palm OS, Amstrad CPC
LF+CR: Acorn BBC and RISC OS spooled text output.
CR: Commodore 8-bit machines, Acorn BBC, ZX Spectrum, TRS-80, Apple II family, Mac OS up to version 9 and OS-9
RS: QNX pre-POSIX implementation.


I'm not 100% positive if endl is portable but you could write a macro or use the correct one according to the OP's OS.
Last edited on
Just to clarify, so you are trying to read line by line, and add whatever numbers are contained in a line.

So if the input file would be:

10 20 3
2
9 9 9 9


Then the output file would be:

33
2
36


If this is the kind of behavior you want to achieve, all you need to do is read line by line by using std::getline() and build a string stream from which you extract the numbers.

You already got very close. So I'll give you incomplete code, because it's enough.

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

// ...

std::string line;

while (std::getline(indata, line))
{
    std::stringstream numbers(line);
    int x; // a, b, c, d, ...
    int sum = 0;

    while (numbers >> x)
        sum += x;

    outfile << sum << '\n';
}


@ giblit: if the file is not opened in binary mode, the newline gets translated appropriately.
Last edited on
Pages: 12