I think your problem is that you arent converting the text from the file into ints or doubles.
Heres a clean(er) and quick solution:
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
|
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
int main(){
//Declare file streams.
ifstream input;
//Open the input file
input.open("inInfo.txt");
if(!input){
cout << "inInfo.txt could not be accessed!" << endl;
return 0;
}
//Variables
string temp; //String data from file, to be converted to an int
int sum[] = {0,0}; //0 for addition, 1 for multiplication
int test[] = {0,0}; //0 for first line, 1 for second
//Get data from file
for(int i = 0; i <= 1; i++){
if(!input.eof()){
getline(input,temp); //Get 1 line from the file and place into temp
test[i] = atoi(temp.c_str()); //set the test variable to the int version of the line
}
}
input.close();
//Mathematical Functions
sum[0] = test[0] + test[1];
sum[1] = test[0] * test[1];
cout << "The sum of " << test[0] << " + " << test[1] << " = " << sum[0] << endl;
cout << "The product of " << test[0] << " * " << test[1] << " = " << sum[1] << endl;
cout << "\n\n\n";
cout << "Press any key to exit..."<<endl;
getch();
return 0;
}
|
If the contents of inInfo.txt were:
The output would be:
1 2 3 4 5 6
|
The sum of 3 + 4 = 7
The product of 3 * 4 = 12
Press any key to exit...
|
If i didn't answer your question, please describe your problem a bit more.
Otherwise, I'm glad I could help. :D
But, before I go, one thing you might want to make sure you know:
The difference between declaring, assigning, and initializing.
Above you stated:
1 2 3 4 5 6
|
//Initialize data
int ch;
int ch1;
int test2;
int test3;
int sum;
|
That wasn't initializing, that was declaring.
Declaring means telling the compiler you want to make a variable.
Assigning means putting a value into a variable.
Initializing means declaring a variable then assigning it, all in one line.
1 2 3 4 5 6 7 8
|
//Declaring
int a;
//Assigning
a = 2;
//Initializing
int b = 0;
|
Anyways, hope i helped.
Have a good one.