#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

const double PI = 3.1416;

int main()
{
	ifstream inFile;

	string fname, lname;
	int age, beginningBalance;
	double length, width, radius, interestRate;
	char ch1;

	inFile.open("inData.txt");


	inFile >> length >> width >> radius >> fname >> lname >> age >> beginningBalance >> interestRate >> ch1;
	inFile.close();

	ofstream outFile;
	outFile.open("outData.txt");

	outFile << "Rectangle: " << endl;

	outFile << "Length = " << length << ", " << "width = " << width << ", " << "area = " << (length*width) << ", " << "parameter = "
		<< (length*length) + (width*width) << endl;

	outFile << "Circle: " << endl;

	outFile << "Radius = " << radius << ", " << "area = " << PI*(radius*radius) << ", " << "circumference = " << radius*PI*2 << endl;

	outFile << "Name: " << fname << " " << lname << ", " << "age: " << age << endl;

	outFile << "beginning balance = $" << beginningBalance << ", " << "interest rate = " << interestRate << endl;

	outFile << "balance at the end of the month = $" << beginningBalance - (interestRate/100)*beginningBalance << endl;

	outFile << "the character that comes after " << ch1 << "in the ASCII set is B" << endl;

	outFile.close();

	return 0;
}

The file I'm inputting information from contains :

10.20 5.35
15.6
Randy Gill 31
18500 3.5
A

and when I run the program it returns as :

Rectangle:
Length = -9.25596e+061, width = -9.25596e+061, area = 8.56729e+123, parameter = 1.71346e+124
Circle:
Radius = -9.25596e+061, area = 2.6915e+124, circumference = -5.81571e+062
Name: , age: -858993460
beginning balance = $-858993460, interest rate = -9.25596e+061
balance at the end of the month = $-7.95081e+068
the character that comes after Ìin the ASCII set is B

I'm not really sure what's going wrong. I've checked everything I could for the last hour and there must be some semantic error that I'm blind to. Any help would be greatly appreciated! Thanks in advance.

Recommended Answers

All 5 Replies

This looks like garbage input from memory, if you initialize all your variables to zero will the program output zero or garbage?

It outputs a bunch of garbage still, I honestly don't know why. It's very frustrating, and I still haven't been able to figure it out.

here is what I got as a result

Rectangle: 
Length = 10.2, width = 5.35, area = 54.57, parameter = 132.662
Circle: 
Radius = 15.6, area = 764.54, circumference = 98.0179
Name: Randy Gill, age: 31
beginning balance = $18500, interest rate = 3.5
balance at the end of the month = $17852.5
the character that comes after Ain the ASCII set is B

I really think that your program is not finding this file so add this after you open the infile....

...
	
	inFile.open("inData.txt");

	if(inFile == NULL)
		printf("File not found!\n");
...

In visual studio your input file has to be in the build directory, the same place as the .cpp source referring to this file. You might also fix this by adding the Absolute Path to the input file, however then it is not likely to port over to someone else machine.

My project was called temp11 and here is where I stored my input file

..My User Directory..\Documents\Visual Studio 2010\Projects\Temp11\Temp11\

Also I kinda think you mean perimeter but I cant be sure. Perimeter is 2*width+2*height not squared, could be wrong in that assumption...

I get the same error you do when it cannot find the file, I am surprized that the last line of the output is correct... lol...

anyway got to sleep, good luck finding your answer... Im 100% sure it cant find the file, place it in the directory with the source, or give it the correct relative path, or full absolute path.

Wow, I didn't have the file saved in the right spot. Thanks a ton man, I really appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.