Simple fstreams problem

Hello All,
I am a noob in c++ and i was trying to code a program where i can get some data from a input file and then create an out put file.
My input file looks somthing like this -

21 32
33 02
76 48

Now i have to read these lines using the get command, and then multply them and store them in z , so that my output looks like
21 32 672

i am having trouble with my code. I am posting it below any comments and help would be appreciated.




#include <iostream>

#include <iomanip>

#include <fstream>

#include <string>

#include <cmath>

using namespace std;



// Main Program



void main()

{ string fname,outfname;

fstream infile,outfile;

int z,count;

// Initialize Count

count = 0;

// User Input

cout << " XZY Multiply Program " << endl;

cout << "Enter File Name: ";
cin >> fname;

infile.open(fname.data(),ios::in);
if (!infile.is_open()) return; // Input File

cout << " Enter Output File Name: ";
cin >> outfname;

outfile.setf(ios::fixed); // setting fixed output for output file
outfile.precision(3); // setting precision for output file
outfile.open(outfname.data(),ios::out); // Output File
if (!outfile.is_open()) return;


// LOOP

while (!infile.eof())
{
int z;

getline(infile,fname); // get a line from the fine(infile) and put it in string(fname)

infile << x << y; // storing value of fname to an integer z



if(infile.good()) // Checking if the infile is good

{
cout << z << endl;
count++;
};

};

// Close file

infile.close();

outfile.close();


//Display Count

cout << "Count = " << count;

}
Last edited on
You have two errors here:
1
2
3
4
5
6
7
infile << x << y; // storing value of fname to an integer z

if(infile.good()) // Checking if the infile is good
{
    cout << z << endl;
    count++;
};
You should use >> in this line



Did you ever give a value to 'z'?
And use int main, not void main
I got a value of Z as -7.23352345346

and its spread out all over my .txt file.. i am just trying to read the thing one line at the time, but it isn't working as i expected.
This is the type of format i am trying to get them in ...

Output.txt

7.2830 84.40 614.685
9.1327 124.02 1132.637
7.2619 133.16 966.995
6.2527 43.96 274.869
4.2160 24.08 101.521
5.2724 57.80 304.745
5.2025 73.89 384.413
-9.9500 80.53 -801.274
0.2790 -12.43 -3.468
2.2115 3.37 7.453

Input.txt
7.2830 84.40
9.1327 124.02
7.2619 133.16
6.2527 43.96
4.2160 24.08
5.2724 57.80
5.2025 73.89
-9.9500 80.53
0.2790 -12.43
2.2115 3.37
I got a value of Z as -7.23352345346
z is an integer how can it have that value?
Anyway I already stated that you never give a value to z in your program. After reading x and y you should set z to x*y.
while (!infile.eof())
{
int x,y,z;

infile >> x >> y; // get a line from the fine(infile) and put it in string(fname)

z = (x*y); // Calculating Z
outfile << "Z" << z;


if(infile.good()) // Checking if the infile is good

{
count++;
cout << count;
};

};

output for this fle -
Z-1717986924Z-1717986924Z-1717986924Z-1717986924
Topic archived. No new replies allowed.