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,...

  • 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>...

  • I need help with using the infile and outfile operations in C++... Here's my assignment requirements:...

    I need help with using the infile and outfile operations in C++... Here's my assignment requirements: Assignment: Cindy uses the services of a brokerage firm to buy and sell stocks. The firm charges 1.5% service charges on the total amount for each transaction, buy or sell. When Cindy sells stocks, she would like to know if she gained or lost on a particular investment. Write a program that allows Cindy to read input from a file called Stock.txt: 1. The...

  • 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...

  • 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...

  • I need to add something to this C++ program.Additionally I want it to remove 10 words...

    I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #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> words);    int main() { // Declaring variables std::map<std::string,int> words;       //defines an input stream for the data file ifstream dataIn;    string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...

  • C Programming I was given this code to display the following as the output but it...

    C Programming I was given this code to display the following as the output but it does not seem to work. what is wrong? or can someone guide me through the steps? thanks! I have created the txt file named myfile.txt but the program will not compile. please help. I am forced to use Microsoft visual studio. This program makes a duplicate copy of a file (reads input from a file and write output to another file) 1 #include <stdio.h>...

  • 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...

  • I need to update this C++ code according to these instructions. The team name should be...

    I need to update this C++ code according to these instructions. The team name should be "Scooterbacks". I appreciate any help! Here is my code: #include <iostream> #include <string> #include <fstream> using namespace std; void menu(); int loadFile(string file,string names[],int jNo[],string pos[],int scores[]); string lowestScorer(string names[],int scores[],int size); string highestScorer(string names[],int scores[],int size); void searchByName(string names[],int jNo[],string pos[],int scores[],int size); int totalPoints(int scores[],int size); void sortByName(string names[],int jNo[],string pos[],int scores[],int size); void displayToScreen(string names[],int jNo[],string pos[],int scores[],int size); void writeToFile(string...

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
ADVERTISEMENT