Maza
Video Editor
Hello all,
Basically in my class we have to make a program that prompts the user to enter in a .txt file name, and the program will read the file and do the calculations.
My problem is, I basically hard coded the .txt file inside the program, I need to have it so it actually prompts the user to enter the .txt file.
The program works perfectly fine with the .txt file hard coded, but i'm having trouble trying to get it so the user can enter the .txt file name.
Here is the area in the code were I think my problem can be fixed
Here is the whole code
My professor did say I made things way harder on myself then it needed to be, But said he would be impressed if I could make it work with my current set up, Any ideas ?
S7 Pro WildeThing GoldBl4d3
Basically in my class we have to make a program that prompts the user to enter in a .txt file name, and the program will read the file and do the calculations.
My problem is, I basically hard coded the .txt file inside the program, I need to have it so it actually prompts the user to enter the .txt file.
The program works perfectly fine with the .txt file hard coded, but i'm having trouble trying to get it so the user can enter the .txt file name.
Here is the area in the code were I think my problem can be fixed
Code:
ifstream theFile("employeeinfo.txt"); // Loads file into program
string firstname; // First name
string lastname; // Last Name
double baseSalary; // Base Salary
double commPercent; // Commission Percentage
double totalSales; // Total sales
double empExpenses; // Employee's expenses
double finalComm; // Employee Commission
system("cls");
while (theFile >> firstname >> lastname >> baseSalary >> commPercent >> totalSales >> empExpenses)
{
finalComm = (totalSales / 100) * 3.5;
double grandTotal = baseSalary + finalComm + empExpenses;
cout << fixed << showpoint;
cout << "Payroll for: " << firstname << ' ' << lastname << endl;
cout << "Base Salary: " << setprecision(2) << baseSalary << endl; // Where the final output get displayed
cout << "Commission : " << setprecision(2) << finalComm << endl;
cout << "Expenses : " << setprecision(2) << empExpenses << endl;
cout << "----------------" << endl;
cout << setprecision(2) << grandTotal << endl; // baseSalary + Commission + Expenses & ask why number does not round up.
}
Code:
/*
Description: This program will accept input from a file and then calculate and output the employees base salary, commission, expenses, and the all of these amounts combined.
*/
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <iomanip>
using namespace std;
void EmployeeInfo()
{
int firstNum = 1;
int secondNum = 2;
int userChoice;
cout << "Press 1 to edit employee info, press 2 to calculate current stored info" << endl;
cin >> userChoice;
if (userChoice == firstNum)
{
system("cls");
ofstream theFile("employeeinfo.txt");
string firstname; // First name
string lastname; // Last Name
double baseSalary; // Base Salary
double commPercent; // Commission Percentage
double totalSales; // Total sales
double empExpenses; // Employee's expenses
cout << "Please enter the following information: " << endl;
cout << "First Name: " << endl;
cout << "Last Name: " << endl;
cout << "Base Salary" << endl; // Prompt's user to enter their information
cout << "Commission Percentage: " << endl;
cout << "Total Sales: " << endl;
cout << "Expenses: \n" << endl;
cout << "When finished, Press Ctrl+Z.\n" << endl;
while (cin >> firstname >> lastname >> baseSalary >> commPercent >> totalSales >> empExpenses)
{
theFile << firstname << ' ' << lastname << ' ' << baseSalary << ' ' << commPercent << ' ' << totalSales << ' ' << empExpenses << endl;
}
return;
}
else if (userChoice == secondNum)
{
return;
}
while (userChoice != firstNum && userChoice != secondNum)
{
cin.clear();
cin.ignore();
cout << "Invalid Input ! Please try again. \n" << endl; // Loop works, But ask why if I put two or more characters it says "Invalid" more then once ?
cin >> userChoice;
}
}
int main()
{
EmployeeInfo(); // This function is where File itself can be edited
system("cls");
cout << "Processing...\n";
system("pause");
ifstream theFile("employeeinfo.txt"); // Loads file into program
string firstname; // First name
string lastname; // Last Name
double baseSalary; // Base Salary
double commPercent; // Commission Percentage
double totalSales; // Total sales
double empExpenses; // Employee's expenses
double finalComm; // Employee Commission
system("cls");
while (theFile >> firstname >> lastname >> baseSalary >> commPercent >> totalSales >> empExpenses)
{
finalComm = (totalSales / 100) * 3.5;
double grandTotal = baseSalary + finalComm + empExpenses;
cout << fixed << showpoint;
cout << "Payroll for: " << firstname << ' ' << lastname << endl;
cout << "Base Salary: " << setprecision(2) << baseSalary << endl; // Where the final output get displayed
cout << "Commission : " << setprecision(2) << finalComm << endl;
cout << "Expenses : " << setprecision(2) << empExpenses << endl;
cout << "----------------" << endl;
cout << setprecision(2) << grandTotal << endl; // baseSalary + Commission + Expenses & ask why number does not round up.
}
system("pause");
return 0;
}
My professor did say I made things way harder on myself then it needed to be, But said he would be impressed if I could make it work with my current set up, Any ideas ?
S7 Pro WildeThing GoldBl4d3