Jump to content

Homework Help

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Jrb

Jrb

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Hello everyone,

I'm trying to do some I/O operations for some homework, but I'm stuck. I have to write a program that reads data from a text file, and store updated information in a [.dat] file. The text file reads as the following:

Miller Andrew 65789.87 5
Green Sheila 75892.56 6
Sethi Amit 74900.50 6.1

Last name, first name, current salary, and pay increase.

I have to read from the text file, write the [.dat] file with the last name, first name, and updated salary. Here is what I have so far, and I am completely lost. My professors husband died the first day of class, and we haven't went over I/O operations, but she still expects us to do the homework. How can I read from the text file, and write each line with the new updated salary?

#include <iostream>

#include <iomanip>

#include <string>

#include <fstream>

using namespace std;


int main() {

	string line;

	string firstName;

	string lastName;


	double currentSalary;

	double increaseVar;

	double updatedSalary;


	ifstream inData;

	ofstream outData;


	inData.open("Ch3_Ex8Data.txt");

	outData.open("Ch3_Ex8Output.dat");


	if(inData.is_open()) {

		while(!inData.eof()) {

			inData >> lastName >> firstName >> currentSalary >> increaseVar >> updatedSalary;

			getline(inData, line);

		}

		inData.close();

		outData.close();

	}


	return 0;

}


#2
Jrb

Jrb

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Got it working for the most part; however, in my [.dat] file, I'm supposed to show the updated salary in two decimal places, but it is only showing one decimal. Here is my updated code:

#include <iostream>

#include <iomanip>

#include <string>

#include <fstream>

using namespace std;


int main() {

	string line;

	string firstName;

	string lastName;


	double currentSalary;

	double increaseVar;

	double updatedSalary;


	ifstream inData;

	ofstream outData;


	inData.open("Ch3_Ex8Data.txt");

	outData.open("Ch3_Ex8Output.dat");


	if(inData.is_open()) {

		while(!inData.eof()) {

			cout << fixed << showpoint << setprecision(2);

			inData >> lastName >> firstName >> currentSalary >> increaseVar;

			updatedSalary = (currentSalary * increaseVar) / 100 + currentSalary;

			getline(inData, line);

			outData << lastName << "\t" << firstName << "\t" << updatedSalary << endl;

		}

		inData.close();

		outData.close();

	}


	return 0;

}


#3
Jrb

Jrb

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Nevermind....... Got it working. Sorry for the useless post. This worked for me:

outData << fixed <<setprecision(2) << lastName << "\t" 

				<< firstName << "\t" << updatedSalary << endl;

Sorry guys, this is embarssing.

Request to close this thread.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users