Question

Input input2.Lxt Puture Value Prsent Value Monthly Intereat Month 0.4248 Your output 12203.06 10000.00 06-11 0000 . 00-wo. 424148e Expected output 12205.06 Puture Value Pesent Value Monthly Interest Honths 10000 00 0.42 48 2: Compare output 0/10 Output differs. See highlights below. Special character legend Input input3.txt -10000 1.1 48 One or moze of the abovo values are not greater than zoo 10000 -1 12 One or more of the above values are not greater than zero One or more of the above values are not greater than zerO One or more of the abo Your output 10000 1.1 reater zero One or more of the above values are not greater than zero -10000,00 1.10 One or more of the above values are not greater than zero 10000.00 -1,00 12 One or more of the above values are not greater than zero Expected output 100002081,180 One OL liore uf the abov values, are no? greater than zero 000 0.00 One or more of the above values are not greater than zero

Please help the out keeps printing twice

#include
#include
#include
#include
#include
using namespace std;

/**
* Function to calculate the future value and return the same
*/
double calculateFutureValue(double presentValue, double interestRate, int months)
{
   double futureValue = (double)presentValue * pow((1 + interestRate), months);
   return futureValue;
}
/**
* Function to read the input file and assign the value to the variables
*/
unsigned int readfile(ifstream &inF, double &presentValue, double &interestRate, int &months)
{
   inF >> presentValue;
   inF >> interestRate;
   inF >> months;
   if (presentValue <= 0 || interestRate <= 0 || months <= 0)
   {
      return 2;
   }
   else if (!(presentValue <= 0 || interestRate <= 0 || months <= 0))
      return 1;
   else
   {
      if (inF.eof())
         return 0;
   }
}
/**
* Function to write data to file
*/
void write2File(ofstream &of, double futureValue, double presentValue, double interestRate, int months)
{
   of << futureValue << "\t" << presentValue << "\t" << interestRate << "\t" << months << "\n";
}
// main function
int main(void)
{
   std::setprecision(2);
   string infile;
  
   cin >> infile;
  
   ifstream inF;
   inF.open(infile.c_str());
   if (!inF)
   {
      cout << "File \"" << infile << "\" could not be opened\n";
      return -1;
   }
   string outfile = "output.xls";
   ofstream of;
   of.open(outfile.c_str());
   if (!of)
   {
      cout << "Error in creating the output file" << endl;
      return -1;
   }
   of << "Future Value\tPresent Value\tMonthly Interest\tMonths\n"; // printing header to output file
   double futureValue;
   double presentValue;
   double interestRate;
   int months;
   of << fixed << setprecision(2); // to print 2 places after decimal into file
   while (!inF.eof()) // looping untill end of file
   {
      int returnVal = readfile(inF, presentValue, interestRate, months);
      if (returnVal == 2)
      {
         std::setprecision(2);
         cout << presentValue << "\t " << interestRate << "\t " << months << endl;
         cout << "One or more of the above values are not greater than zero" << endl;
      }
      else if (returnVal == 1)
      {
         futureValue = calculateFutureValue(presentValue, interestRate / 100, months);
         write2File(of, futureValue, presentValue, interestRate, months);
      }
   }
   inF.close();
   of.close();
   return 1;
}

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

//Changes are in bold

//main.cpp
//include header files
#include<iostream>
#include<string>
#include<iomanip>
#include<math.h>
#include<fstream>
using namespace std;
/**
* Function to calculate the future value and return the same
*/
double calculateFutureValue(double presentValue, double interestRate, int months)
{
   double futureValue = (double)presentValue * pow((1 + interestRate), months);
   return futureValue;
}
/**
* Function to read the input file and assign the value to the variables
*/
unsigned int readfile(ifstream &inF, double &presentValue, double &interestRate, int &months)
{
   inF >> presentValue;
   inF >> interestRate;
   inF >> months;
   if (presentValue <= 0 || interestRate <= 0 || months <= 0)
       return 2;
   else if (!(presentValue <= 0 || interestRate <= 0 || months <= 0))
       return 1;
   else if (inF.eof())
           return 0;
      
}
/**
* Function to write data to file
*/
void write2File(ofstream &of, double futureValue, double presentValue, double interestRate, int months)
{
   of << futureValue << "\t" << presentValue << "\t" << interestRate << "\t" << months << "\n";
}
// main function
int main(void)
{
   std::setprecision(2);
   string infile;

   cin >> infile;

   ifstream inF;
   inF.open(infile.c_str());
   if (!inF)
   {
       cout << "File \"" << infile << "\" could not be opened\n";
       system("pause");
       return -1;
   }
   string outfile = "output.xls";
   ofstream of;
   of.open(outfile.c_str());
   if (!of)
   {
       cout << "Error in creating the output file" << endl;
       return -1;
   }
   of << "Future Value\tPresent Value\tMonthly Interest\tMonths\n"; // printing header to output file
   double futureValue;
   double presentValue;
   double interestRate;
   int months;
   of << fixed << setprecision(2);
   // to print 2 places after decimal into file

   while (!inF.eof()) // looping untill end of file
   {
       int returnVal = readfile(inF, presentValue, interestRate, months);
       if (returnVal == 2)
       {
      
           //use fixed <<setprecision(2) before printing
           cout << fixed<<setprecision(2)<
<presentValue << "\t " << interestRate << "\t " << months << endl;
           cout << "One or more of the above values are not greater than zero" << endl;
       }
       else if (returnVal == 1)
       {
           futureValue = calculateFutureValue(presentValue, interestRate / 100, months);
           write2File(of, futureValue, presentValue, interestRate, months);
       }
   }
   inF.close();
   of.close();

   system("pause");
   return 1;
}

---------------------------------------------------------------------------

input3.txt

-10000 1.10 48
10000 -1.00 12
10000 1.10 0
0.00 0.00 0

---------------------------------------------------------------------------

Sample Output:

input3.txt 10000.00 One or more of the above values are not greater than zero 10000.0e 1.10 48 -1.00 12 1.10 e eor cere of th

---------------------------------------------------------------------------

input3.txt (chagne input file data)

10000 0.42 48

---------------------------------------------------------------------------

Sample output:

Future Value Present Value Monthly Interest Months 12228.42 10000.00 0.42 48

Excel screen shot:

A1 f Future Value 1 Future Value Present Value Monthly Interest Months 10000 2 12228.42 0.42 48

Add a comment
Know the answer?
Add Answer to:
Please help the out keeps printing twice #include #include #include #include #include using namespace std; /**...
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
  • This is the creatList and printList function #include <iostream> #include <fstream> using namespace std; struct nodeType...

    This is the creatList and printList function #include <iostream> #include <fstream> using namespace std; struct nodeType {    int info;    nodeType *link;    nodeType *next;    double value; }; void createList(nodeType*& first, nodeType*& last, ifstream& inf); void printList(nodeType* first); int main() {    nodeType *first, *last;    int num;    ifstream infile;    infile.open("InputIntegers.txt");    createList(first, last, infile);    printList(first);    infile.close();    system("pause");    return 0; } void createList(nodeType*& first, nodeType*& last, ifstream& infile) {    int number;...

  • #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool...

    #include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...

  • #include <iostream> #include <vector> #include <iomanip> using namespace std; int main() { const int NUM_ITEMS =...

    #include <iostream> #include <vector> #include <iomanip> using namespace std; int main() { const int NUM_ITEMS = 8; vector <double> inverse(NUM_ITEMS); int j; double temp; for (int i = 0; i < NUM_ITEMS; i++) { inverse.at(i) = 1 / (i + 1.0); } cout << fixed << setprecision(2); cout << "Original vector..." << endl; for (int i = 0; i < NUM_ITEMS; i++) { cout << inverse.at(i) << " "; } cout << endl; cout << "Reversed vector..." << endl; for...

  • //Need help ASAP in c++ please. Appreciate it! Thank you!! //This is the current code I have. #include <iostream> #include <sstream> #include <iomanip> using namespace std; cl...

    //Need help ASAP in c++ please. Appreciate it! Thank you!! //This is the current code I have. #include <iostream> #include <sstream> #include <iomanip> using namespace std; class googlePlayApp { private: string name; double rating; int numInstalls; int numReviews; double price;    public: googlePlayApp(string, double, int, int, double); ~ googlePlayApp(); googlePlayApp();    string getName(); double getRating(); int getNumInstalls(); int getNumReviews(); string getPrice();    void setName(string); void setRating(double); void setNumInstalls(int); void setNumReviews(int); void setPrice(double); }; googlePlayApp::googlePlayApp(string n, double r, int ni, int nr, double pr)...

  • 4) What is the output if the input istom - Sawyer? #include <iostream> using namespace std;...

    4) What is the output if the input istom - Sawyer? #include <iostream> using namespace std; int main() { string playerName; cout << "Enter name"; cin >> playerName; cout << endl « playerName; return 0; } a. Tom - Sawyer b. Tom Sawyer c. Tom d. Sawyer 5) Which XXX generates "Adam is 30 years old." as the output? #include <iostream> using namespace std; int main() { string name = "Adam"; int age = 30; XXX return 0; } a....

  • PLEASE HELP WITH THE FIX ME'S #include #include #include #include "CSVparser.hpp" using namespace std; //==...

    PLEASE HELP WITH THE FIX ME'S #include #include #include #include "CSVparser.hpp" using namespace std; //============================================================================ // Global definitions visible to all methods and classes //============================================================================ // forward declarations double strToDouble(string str, char ch); // define a structure to hold bid information struct Bid { string bidId; // unique identifier string title; string fund; double amount; Bid() { amount = 0.0; } }; //============================================================================ // Linked-List class definition //============================================================================ /** * Define a class containing data members and methods to *...

  • please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName();...

    please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName(); double getNumberExams(); double getScoresAndCalculateTotal(double E); double calculateAverage(double n, double t); char determineLetterGrade(); void displayAverageGrade(); int main() { string StudentName; double NumberExam, Average, ScoresAndCalculateTotal; char LetterGrade; StudentName = getStudentName(); NumberExam = getNumberExams(); ScoresAndCalculateTotal= getScoresAndCalculateTotal(NumberExam); Average = calculateAverage(NumberExam, ScoresAndCalculateTotal); return 0; } string getStudentName() { string StudentName; cout << "\n\nEnter Student Name:"; getline(cin, StudentName); return StudentName; } double getNumberExams() { double NumberExam; cout << "\n\n Enter...

  • Thank you! /*Lab 8 : Practicing functions and arrays Purpose: */ #include<iostream> #include<fstream> using namespace std;...

    Thank you! /*Lab 8 : Practicing functions and arrays Purpose: */ #include<iostream> #include<fstream> using namespace std; int read_function(int array[], int, int); int main() { int array[300], numba; read_function(array[300]); cout << "Enter a whole number between 2-20: " << endl; cin >> numba; read_function(numba); return 0; } int read_funtion (int arr[300], int num, int Values) { int sum=0; ifstream array_file; array_file.open("Lab8.dat"); for(int i=0; i < 300; i++) {   array_file >> arr[i];   cout << arr[i];   sum += i; } cout << sum;...

  • #include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void...

    #include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void displayArrayContent(int[]); void displayLargestValue(int[]); void displaySmallestValue(int[]); int main(){    int number;    int numbers[SIZE] = {9,1,90,98,53,22,76,29,37,65}; cout <<"Enter a number: "; cin >> number; cout << endl;    displayGreaterThan(numbers,number); cout << endl; displaySmallerThan(numbers,number); cout << endl; displayArrayContent(numbers); cout << endl; displayLargestValue(numbers); cout << endl; displaySmallestValue(numbers); cout << endl;    return 0;       } void displayGreaterThan(int value[],int num){ cout << " All larger value(s)than" <<...

  • #include <iostream> #include <chrono> using namespace std; double improvedPow(double x, int y) { // To be...

    #include <iostream> #include <chrono> using namespace std; double improvedPow(double x, int y) { // To be implemented by you } int main() { cout << "To calculate x^y ..." << endl; double x; int y; cout << "Please enter x: "; cin >> x; cout << "Please enter y: "; cin >> y; if(x == 0) { if (y > 0) cout << 0 << endl; else cout << "x^y is not defined" <<endl; } else { cout << improvedPow(x,y)...

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