Dec 6, 2012 at 6:57am UTC
my this programm just doing the square of every first number it gets.
my file is like
120
22
code :
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<fstream>
using namespace std;
int main ()
{
char filename [25];
ifstream myfile;
cin.getline(filename,25);
myfile.open(filename);
if (!myfile.is_open())
{
exit (0);
}
char number[25];
while (myfile.good ())
{
myfile.getline(number,25);
int num=atoi(number);
cout << num + num<< endl;
}
}
output: 240
44
0
i want those two values of file to be added like total= 120 + 22 =142.
Dec 6, 2012 at 7:26am UTC
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<fstream>
using namespace std;
int main ()
{
char filename [25];
ifstream myfile;
cin.getline(filename,25);
myfile.open(filename);
if (!myfile.is_open())
{
exit (0);
}
char number[25];
while (myfile.good ())
{
myfile.getline(number,25);
int num=atoi(number);
int sum = 0 ;
sum+= (num + num) ;
cout << sum;
}
}
i did this bt still same result
Dec 6, 2012 at 7:45am UTC
Put sum at the beginning of main with the rest of your variables. Also, move your cout statement outside of the while loop.
Dec 6, 2012 at 8:42am UTC
thanks it working now properly