What's new

C/C++ Help with having basic program read file

Status
Not open for further replies.
M

Maza

Video Editor
Messages
292
Reaction score
256
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
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.
    }
Here is the whole code
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 W WildeThing GoldBl4d3
 
W

WildeThing

Enthusiast
Messages
212
Reaction score
63
Two ways you could do it.

1) You could simply pass a string to the char* argv[] - that's the second argument in the main function. Like: int main(int argc, char* argv[]) - the first is the argument count (because deducing the size of a char* array[] is a nightmare) and the second is the char* array[] (an array of char arrays). This means that if you execute your program from console and append, for example: yourprogram.exe /some/path/to/file it will be able to take that from argv[1] and you can pass that directly into the std::ifstream constructor.
2) You could simply use std::cin and input a string and then construct a std::ifstream with it.
C++:
std::string path;
std::cout << "Enter a path" << std::endl;
std::cin >> path;
std::ifstream inFile(path);
if(inFile.is_open())
{
    // read data from file
    inFile.close();
} else {
    // throw path? show error? do whatever
}

You could also change your set of if statements from EmployeeInfo() to switch statement. I personally don't like that way you've made the EmployeeInfo() essentially the main method since now you're returning to the main method to input stuff. That seems kinda messy.
 
M

Maza

Video Editor
Messages
292
Reaction score
256
Two ways you could do it.

1) You could simply pass a string to the char* argv[] - that's the second argument in the main function. Like: int main(int argc, char* argv[]) - the first is the argument count (because deducing the size of a char* array[] is a nightmare) and the second is the char* array[] (an array of char arrays). This means that if you execute your program from console and append, for example: yourprogram.exe /some/path/to/file it will be able to take that from argv[1] and you can pass that directly into the std::ifstream constructor.
2) You could simply use std::cin and input a string and then construct a std::ifstream with it.
C++:
std::string path;
std::cout << "Enter a path" << std::endl;
std::cin >> path;
std::ifstream inFile(path);
if(inFile.is_open())
{
    // read data from file
    inFile.close();
} else {
    // throw path? show error? do whatever
}

You could also change your set of if statements from EmployeeInfo() to switch statement. I personally don't like that way you've made the EmployeeInfo() essentially the main method since now you're returning to the main method to input stuff. That seems kinda messy.
Two ways you could do it.

1) You could simply pass a string to the char* argv[] - that's the second argument in the main function. Like: int main(int argc, char* argv[]) - the first is the argument count (because deducing the size of a char* array[] is a nightmare) and the second is the char* array[] (an array of char arrays). This means that if you execute your program from console and append, for example: yourprogram.exe /some/path/to/file it will be able to take that from argv[1] and you can pass that directly into the std::ifstream constructor.
2) You could simply use std::cin and input a string and then construct a std::ifstream with it.
C++:
std::string path;
std::cout << "Enter a path" << std::endl;
std::cin >> path;
std::ifstream inFile(path);
if(inFile.is_open())
{
    // read data from file
    inFile.close();
} else {
    // throw path? show error? do whatever
}

You could also change your set of if statements from EmployeeInfo() to switch statement. I personally don't like that way you've made the EmployeeInfo() essentially the main method since now you're returning to the main method to input stuff. That seems kinda messy.

Going to try option #2 tommorow when I get a chance thank you ! & thank you for the feedback aswell , As you can see I'm still trying to learn good coding habits but i'm hoping you & the others can slowly see if i'm improving or not
 
M

Maza

Video Editor
Messages
292
Reaction score
256
Two ways you could do it.

1) You could simply pass a string to the char* argv[] - that's the second argument in the main function. Like: int main(int argc, char* argv[]) - the first is the argument count (because deducing the size of a char* array[] is a nightmare) and the second is the char* array[] (an array of char arrays). This means that if you execute your program from console and append, for example: yourprogram.exe /some/path/to/file it will be able to take that from argv[1] and you can pass that directly into the std::ifstream constructor.
2) You could simply use std::cin and input a string and then construct a std::ifstream with it.
C++:
std::string path;
std::cout << "Enter a path" << std::endl;
std::cin >> path;
std::ifstream inFile(path);
if(inFile.is_open())
{
    // read data from file
    inFile.close();
} else {
    // throw path? show error? do whatever
}

You could also change your set of if statements from EmployeeInfo() to switch statement. I personally don't like that way you've made the EmployeeInfo() essentially the main method since now you're returning to the main method to input stuff. That seems kinda messy.
Just did what you said & it works !! Thank you !!!
 
Status
Not open for further replies.
Top Bottom