I need help with .txt input, can someone help me?

Jan 28, 2018 at 8:47pm
The basic concept of this code is that you take input from a .txt file about The type of operation you want to do, the first number and the second number (It's a calculator obviously). So if i write "Addition 2 3" It should output 5 to the output.txt file, but it prints some numbers and letters which i think its an address of some sort. how do i solve this? much thanks. This is the code (please don't mind that it's written in italian):


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
48
49
50
51
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){
float num1,num2,ris;
string scelta;
	
	ifstream infile;
	infile.open("input.txt");
	
	//se manca il file di input
	if(infile.fail()){
		cerr<<"Errore: non è stato trovato il file di input.\n";
		system("pause");
	}
	
	
	while(infile.eof()){
		getline(infile,scelta);
		infile>>scelta>>num1>>num2;
	}
	infile.close();
	
	ofstream outfile;
	outfile.open("output.txt");

	if(scelta=="addizione"||"Addizione"){
		ris=num1+num2;
		outfile<<ris;
	}
	else if(scelta=="sottrazione"||"Sottrazione"){
		ris=num1+num2;
		outfile<<ris;
	}
	else if(scelta=="moltiplicazione"||"Moltiplicazione"){
		ris=num1*num2;
		outfile<<ris;
	}
	
	else if(scelta=="divisione"||"Divisione"){
		ris=num1/num2;
		outfile<<ris;
	}


outfile.close();
return 0;	
}
Jan 28, 2018 at 9:00pm
Hello,

You haven't assigned num1 or num2 any values. Maybe you should add

1
2
3
4
5

cout << "What 2 numbers would you like to calculate with?" << endl;
cin >> num1;
cin >> num2;


Maybe the output you are getting to the text file is just what ever happens to be in the memory of your computer at that time.
Jan 28, 2018 at 11:16pm
But wouldnt this get the value from the emulator? i need to get the num1 value and num2 value just from the text file; Imagine if i you write this in the "input.txt" file:

Addition
2
3

using infile>>scelta>>num1>>num2; i should save "Addition" on the string scelta, 2 on num1 and 3 on num2. But i think i'm doing something wrong with the string. Thanks for your help.
Jan 28, 2018 at 11:22pm
You are inputting scelta twice - the first time with getline on line 21, the second with >> on line 22.

Line 20 is seriously wrong: you can put the first read operation in the while test, not a test for eof.
Last edited on Jan 28, 2018 at 11:24pm
Jan 28, 2018 at 11:25pm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

infile.open("input.txt");

// ...

outfile.open("output.txt");

while (infile >> scelta >> num1 >> num2)
{
    // successful read

	if (scelta == "addizione" || scelta == "Addizione") { // NOTICE: Two complete expressions!!!
		ris = num1 + num2;
		outfile << ris;
	}

	// ...
}

Last edited on Jan 28, 2018 at 11:26pm
Jan 28, 2018 at 11:44pm
Thanks for your help everyone!
Ganado, why exactly do i put those while parameters? Whats the difference between putting those parameters on the while loop and using a while with !infile.eof() and with infile >> scelta >> num1 >> num2 inside?
Jan 28, 2018 at 11:51pm
I tried modifying it and it actually works like this too:

1
2
3
	while(!infile.eof()){
	infile>>scelta>>num1>>num2;
	}


the error, like lastchance said, was that i inputted scelta twice, and i also forgot the ! on the while statements.

Jan 29, 2018 at 12:05am
Luca, you are slightly misquoting me: I said that you should put the [first] read statement in the while test. @Ganado's form is good here.

The problem with !infile.eof() is that this is NOT triggered by reading the last item, but by trying to read another one afterward. The end result is that a loop which shouldn't run actually does, the stream fails and what was previously in the input variable ends up being dealt with twice.

I should follow @Ganado's form here.
Jan 29, 2018 at 12:26am
Oh okay, i got it. Thanks for your help! :)
Topic archived. No new replies allowed.