• 0

Help With my fstream code..


Question

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.

Thanks in advance...

#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)

fname << z; // 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;

}

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

You're pretty much there, although I had to fix a few compiler errors too. You didn't use code tags to post so I'm not sure if you indent your code or not. Make sure you do as it will help you find issues easier ;)

As for your code you're pretty much there. I made a few changes now and I think its working:

#include &lt;iostream&gt;
#include &lt;iomanip&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;cmath&gt;

using namespace std;

// Main Program
int main()
{ 
	string fname,outfname;

	fstream infile, outfile;

	int z, y, count;

	// Initialize Count

	count = 0;

	// User Input

	cout &lt;&lt; " XZY Multiply Program " &lt;&lt; endl;

	cout &lt;&lt; "Enter File Name: ";
	cin &gt;&gt; fname;

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

	cout &lt;&lt; " Enter Output File Name: ";
	cin &gt;&gt; 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 0;


	// LOOP
	while (!infile.eof())
	{
		infile &gt;&gt; z &gt;&gt; y; // storing value of fname to an integer z

		if(infile.good()) // Checking if the infile is good
		{
			cout &lt;&lt; z &lt;&lt; " " &lt;&lt; y &lt;&lt; endl;
			outfile &lt;&lt; z &lt;&lt; " " &lt;&lt; y &lt;&lt; " " &lt;&lt; z*y &lt;&lt; endl;
			count += 2; // Inc 2 as we read 2 numbers
			z = 0;
			y = 0;
		};

	};

	// Close file
	infile.close();
	outfile.close();

	//Display Count
	cout &lt;&lt; "Count = " &lt;&lt; count;

	return 0;
}

When you were doing fname << z, what you really meant was fname >> z. You want to store the number in the file, in z. All I really added was another variable to store the second number on the line, and some spaces.

Hope this helps :)

Link to comment
Share on other sites

  • 0

i see Vizion, thanks for clearing that glitch. But i still ran the code and i din't get any output recorded to my output file. Btw this is what i am using as an in put file -

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

and this is wht i am expecting to see in my output file -

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

but i am still not sure, where i am doing it wrong ... the complier didn't gave me any errors. !! any help would be appreciated..

thanks :D

Link to comment
Share on other sites

  • 0

The reason is that you're storing floating point numbers in your file, and trying to store them in an integer (int) variable. The fix is simply to change z and y to float :)

Here's what I get when I run the program now:

7.283 84.400 614.685
9.133 124.020 1132.637
7.262 133.160 966.995
6.253 43.960 274.869
4.216 24.080 101.521
5.272 57.800 304.745
5.202 73.890 384.413
-9.950 80.530 -801.273
0.279 -12.430 -3.468
2.211 3.370 7.453

Also make sure there is a blank line after your last line of numbers.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.