Home > OS >  The output of my code is incorrect, and I need some help. (File input/output)
The output of my code is incorrect, and I need some help. (File input/output)

Time:01-13

For homework we have to input a text file provided for us:

Giselle Robinson Accounting
5600 5 30
450 9
75 1.5

and this is the expected output:

Name: Giselle Robinson, Department: Accounting
Monthly Gross Salary: $5600.00, Monthly Bonus: 5.00%, Taxes: 30.00%
Paycheck: $4116.00

Distance Traveled: 450.00 miles, Traveling Time: 9.00 hours
Average Speed: 50.00 miles per hour

Number of Coffee Cups Sold: 75, Cost: $1.50 per cup
Sales Amount = $112.50

My output :

Name: Giselle Robinson, Department: Accounting
Monthly Gross Salary: $5600.00, Monthly Bonus: 5.00%, Taxes: 30.00%
Paycheck: $4116.00
Distance Traveled: 450.00 miles, Traveling Time: 9.00 hours
Average Speed: 50.00 miles per hour
Number of Coffee Cups Sold: 1, Cost: $0.50 per cup
Sales Amount: $0.50

My issue is with the number of coffee cups sold. I doesn't pull the value of 75 for cups sold and skips to the price of the coffee cup "1.50" , and separates it. What am I doing wrong?

This is my code:

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

using namespace std;

int main() {   
   ifstream inFile;
   ofstream outFile;

   string firstName;
   string lastName;
   string departmentName;

   double grossSalary, bonus, taxes, salaryAndBonus, paycheck, distance, travelTime, avgSpeed, coffeeCost, salesAmount;
   int coffeeSold;
   
   inFile.open("inData.txt");
   outFile.open("outData.txt");   
   outFile << fixed << showpoint;
   outFile << setprecision(2);
   
   // Input file information 
   inFile >> firstName >> lastName >> departmentName;
   outFile << "Name: " << firstName <<" " << lastName << ", " << "Department: " << departmentName << endl;
   
   // Calculating monthly salary   
   inFile >> grossSalary >> bonus >> taxes;
   outFile << "Monthly Gross Salary: $" << grossSalary << ", " << "Monthly Bonus: " << bonus << "%, " << "Taxes: " << taxes << "%" << endl;  
   salaryAndBonus = grossSalary   (bonus/100 * grossSalary);
   paycheck = salaryAndBonus - (taxes/100 * salaryAndBonus); 
   outFile << "Paycheck: $" << paycheck << endl;
   
   // Calculating Average Speed
   inFile >> distance >> travelTime >> avgSpeed;
   outFile << "Distance Traveled: " << distance << " miles, " << "Traveling Time: " << travelTime << " hours" << endl;   
   avgSpeed = distance / travelTime;  
   outFile << "Average Speed: " << avgSpeed << " miles per hour" << endl;
   
   // Calculating number of coffee cups sold 
   inFile >> coffeeSold >> coffeeCost >> salesAmount;
   outFile << "Number of Coffee Cups Sold: " << coffeeSold << ", " << "Cost: $" << coffeeCost << " per cup" << endl;  
   salesAmount = coffeeSold * coffeeCost;  
   outFile << "Sales Amount: $" << salesAmount << endl;
   
   inFile.close();
   outFile.close();

   return 0;
}

CodePudding user response:

What you read is

inFile >> firstName >> lastName >> departmentName;
inFile >> grossSalary >> bonus >> taxes;
inFile >> distance >> travelTime >> avgSpeed;
inFile >> coffeeSold >> coffeeCost >> salesAmount;

The input is:

Giselle Robinson Accounting
5600 5 30
450 9
75 1.5

Lets make a list:

firstName       Giselle 
lastName        Robinson 
departmentName  Accounting
grossSalary     5600 
bonus           5
taxes           30
distance        450
travelTime      9
avgSpeed        75
coffeeSold      1.5
coffeeCost
salesAmount 

Thats only the reading part, I didnt look at the calculations yet, because already reading the input is wrong. As mentioned in a comment, avgSpeed should not be read from the file. You are actually calcualting already via avgSpeed = distance / travelTime; and by this overwriting the value read from the file. Thats 1 missing value. I hope you can figure out the other by yourself.

Once you got that straight I would suggest you to introduce some structure to your code. There are many different ways. I choose one, not necessarily the best, but its simple and already helps to make the code more readable...

Use one custom struct for the input:

 struct input_from_the_file {
      std::string firstName;
      std::string lastName; 
      std::string departmentName;
      int grossSalary;
      //...
 };

Write code to read this from the file. Look at the four lines at the top. Its not more complicated than that. In your code it does look complicated, because those four lines are scattered all around. Better keep stuff that belongs together together. If you have the code to read from the file (and not more) test the hell out of it until you are sure that it is correct.

Then you can define a second struct for the output:

  struct output {
      std::string firstName;
      std::string lastName; 
      std::string departmentName;
      // ...
      double avgSpeed; // <- not in the input but has to be calculated !
      // ...
  };

And a function to transform between them:

 output transform(const input_from_the_file& inp) { ... }

And eventually the code to write output to the console.

For bonus points you can take a look at how to write overloads for << and >>, but if you did the above, then thats just a small step.

CodePudding user response:

I commented the solution but I think you don't get it. However, here is your code with required same change at 3 certain positions....Your requirement was just to add '\n' after these number of lines so made it practically....

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

 using namespace std;

 int main() {

 ifstream inFile;
 ofstream outFile;


 string firstName;
 string lastName;
 string departmentName;

 double grossSalary, bonus, taxes, salaryAndBonus, paycheck, distance, travelTime, avgSpeed, coffeeCost, salesAmount;

 int coffeeSold;


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


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


 // Input file information
 inFile >> firstName >> lastName >> departmentName;


 outFile << "Name: " << firstName <<" " << lastName << ", " << "Department: " << departmentName << endl;


 // Calculating monthly salary

 inFile >> grossSalary >> bonus >> taxes;
 outFile << "Monthly Gross Salary: $" << grossSalary << ", " << "Monthly Bonus: " << bonus << "%, " << "Taxes: " << taxes << "%" << endl;

 salaryAndBonus = grossSalary   (bonus/100 * grossSalary);

 paycheck = salaryAndBonus - (taxes/100 * salaryAndBonus);
 outFile << "Paycheck: $" << paycheck <<"\n"<< endl;

 // Calculating Average Speed

 inFile >> distance >> travelTime >> avgSpeed;
 outFile << "Distance Traveled: " << distance << " miles, " << "Traveling Time: " << travelTime << " hours" << endl;

 avgSpeed = distance / travelTime;

 outFile << "Average Speed: " << avgSpeed << " miles per hour" <<"\n"<< endl;

 // Calculating number of coffee cups sold


 inFile >> coffeeSold >> coffeeCost >> salesAmount;

 outFile << "Number of Coffee Cups Sold: " << coffeeSold << ", " << "Cost: $" <<                          
 coffeeCost << " per cup" << endl;

 salesAmount = coffeeSold * coffeeCost;

 outFile << "Sales Amount: $" << salesAmount <<"\n"<< endl;

 inFile.close();
 outFile.close();
 // Write your main here
 return 0;
 } 

Hope you get understand!!!!!

  •  Tags:  
  • c
  • Related