Question

// Retire.cpp : Defines the entry point for the console application. // program to convery years...

// Retire.cpp : Defines the entry point for the console application.

// program to convery years to retire

// sumin Kang

// 9/26/17

#include "stdafx.h"

#include <iostream>

#include <iomanip>

#include <string>

#include <fstream>

using namespace std;

int main()

{

//declare files

ifstream inFile;

ofstream outFile;

// declare constants and variables

string name;

int age;

int ageInRetire;

// open files

inFile.open("D:\\retire");

outFile.open("D:\\retire");

// get input from user

cout << "What is your name?";

getline(inFile, name);

cout << "How old are you?";

inFile >> age;

// perform calulations

ageInRetire = 27 + age;

// display results

outFile << "\nWell, " << name << " can retire in " << ageInRetire << " years.";

// close files

inFile.close();

outFile.close();

// freezen screen

string junk;

cout << "\n\nPress any keys to continue...";

cin >> junk;

return (0);

This is my homework guidline:

Modify your Retire.cpp program to accept its input from a disk file named "Retire.dat" and display the output to the screen.

Then modify it again to send the output to a disk file name "retire.out".

And I did above code, and my inFile is retire.dat which is

Sumin
21

then I have a file will become outFile, but it is strange.

I have a result that

"Well, Sumin can retire in 48 years."

but my file's result is


Well, can retire in -858993433 years.

How do I fix it?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/ Retire.cpp : Defines the entry point for the console application.

// program to convery years to retire

// sumin Kang

// 9/26/17

#include "stdafx.h"

#include <iostream>

#include <iomanip>

#include <string>

#include <fstream>

using namespace std;

int main()

{

//declare files

ifstream inFile;

ofstream outFile;

// declare constants and variables

string name;

int age;

int ageInRetire;

// open files

inFile.open("D:\\retire.dat");

if (!inFile)

   {

cout << "can't find retire.dat" << endl;

return 0;

   }

outFile.open("D:\\retire.dat");

// get input from user

cout << "What is your name?";

getline(inFile, name);

cout << "How old are you?";

inFile >> age;

// perform calulations

ageInRetire = 27 + age;

// display results

outFile << "\nWell, " << name << " can retire in " << ageInRetire << " years.";

// close files

inFile.close();

outFile.close();

// freezen screen

string junk;

cout << "\n\nPress any keys to continue...";

cin >> junk;

return (0);

}

=============================================
I am able to get the Output with my own correct files, The error could be file is not found, So you are getting this garbage values.

I have added statements , such that if you get file not found. You will see error


kundrar-vi output.txt - 80x24 Well, Sumit can retire in 48 years.

Add a comment
Answer #2

heres the thing bro, I like her too.....

source: me heart.gov
answered by: Elon musk
Add a comment
Know the answer?
Add Answer to:
// Retire.cpp : Defines the entry point for the console application. // program to convery years...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for...

    Theres an error on The bolded line, what am I doing wrong? #include <iostream> //just for writing and reading from console #include <fstream> //this one to deal with files, read and write to text files using namespace std; int main() { //first thing we need to read the data in the text file //let's assume we have the reading in a text file called data.txt //so, we need a stream input variable to hold this data ifstream infile; //now we...

  • Code in C++: Please help me fix the error in function: void write_account(); //function to write...

    Code in C++: Please help me fix the error in function: void write_account(); //function to write record in binary file (NOTE: I able to store data to a txt file but however the data get error when I try to add on data the second time) void display_all(); //function to display all account details (NOTE: This function is to display all the info that save account.txt which is ID, Name, and Type) void modify_account(int); //function to modify record of file...

  • I am trying to modify this program so that it stores the employee's name in a...

    I am trying to modify this program so that it stores the employee's name in a c-string and can handle up to 100 employees (so it would mostly be a partially filled array), but I cannot get it to work. It works with two files that it is suppose to read input from. Then it prints output to an output file. Employee Info File (first file read, M/F should be ignored): 5    Christine Kim 30.00   2       1    F 15    Ray...

  • CODE ERROR Please help to fix the error. I don't know why I can not output...

    CODE ERROR Please help to fix the error. I don't know why I can not output to file. when I open the output file there is an error. here is the input data: Princeton University NJ Princeton 41820    8014   0.0740   0.98   0.97 Harvard University MA Cambridge 43838    19882   0.0580   0.97   0.97 Yale University CT New Haven 45800    12109   0.0690   0.99   0.98 Columbia University NY New York           51008    23606   0.0690   0.99   0.96 Stanford University CA...

  • I need to make a few changes to this C++ program,first of all it should read...

    I need to make a few changes to this C++ program,first of all it should read the file from the computer without asking the user for the name of it.The name of the file is MichaelJordan.dat, second of all it should print ,3 highest frequencies are: 3 words that occure the most.everything else is good. #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int>...

  • C++. I need to alter this code below to the current specifications. Console Progressing Complete!!! Specifications...

    C++. I need to alter this code below to the current specifications. Console Progressing Complete!!! Specifications Included is an input file. Included is a function to aide, if needed Use the functions, enum, and namespace defined in Assignment 6. You will need to use a loop to read each student’s data You will need to load every homework grade into an array Calculate the homework average The homework average can be calculated while the array is loaded Move the printing...

  • // Assignment11.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream>...

    // Assignment11.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <string> using namespace std; int main() { int size, m, n, c, d, first[10][10], second[10][10], sum[10][10]; cout << "Enter the number of rows and columns of matrices: "; cin >> size; m = size; n = size; cout << "Enter the elements of first matrix: "; for ( c = 0 ; c < m ; c++ ) for ( d = 0...

  • Can some help me with my code I'm not sure why its not working. Thanks In...

    Can some help me with my code I'm not sure why its not working. Thanks In this exercise, you are to modify the Classify Numbers programming example in this chapter. As written, the program inputs the data from the standard input device (keyboard) and outputs the results on the standard output device (screen). The program can process only 20 numbers. Rewrite the program to incorporate the following requirements: a. Data to the program is input from a file of an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT