user1807815
user1807815

Reputation: 93

C++ inputting from a file

I have to do a program on girl scout cookies that inputs a .txt file that has a customer first name, number of boxes bought, and name of cookie. Price of box is $3.50. In the program i have to display the customer name, boxes sold, name of cookie, and amount due. And at the end, display number of customers, total boxes sold, and total amount due. This is what I have so far and im not sure why it doesnt run,or at least say file not found, and i have already made the .txt file its in my project folder, Any help is appreciated thanks! The error i get is "unable to start program...system cannot find file specified". I am pretty sure my file is in the right place

#include <iomanip>
#include <string>
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    ifstream inFile;
    //Declare Variables

    string firstName;
    string cookieName;

    int boxesSold;
    int numCustomers;
    double amountDue;
    int totalCustomers;
    int totalBoxesSold = 0;
    double totalAmount = 0;

    inFile.open("girlscout.txt");
    if (inFile)
    {
        cout << "Customer     Boxes     Cookie Name" << endl;
        cout << "Name                              " << endl;

        while(!inFile.eof())//Not end of file
        {
            inFile >> firstName;
            inFile >> boxesSold;
            inFile >> cookieName;

            totalBoxesSold += boxesSold;
            totalAmount = boxesSold * 3.50;

            cout << setprecision(2) << fixed << showpoint;
            cout << setw(2) << firstName
            << right << setw(7) << boxesSold
            << cookieName << endl;
        }

        cout << "Total Boxes Sold: " << totalBoxesSold;
        cout << "Total Amount: " << totalAmount;
        inFile.close();
    }

    else
    {
        cout << "Could not open file " << endl;
    }

    system("pause");
    return 0;
}

Upvotes: 0

Views: 317

Answers (3)

Jathavan Sriram
Jathavan Sriram

Reputation: 19

Are you compiling and running on Visual Studio? I think your error just means that the *.exe file is moved automatically somewhere else - and when hitting "Run" it doesn't find the executable itself.

Upvotes: 0

Qortex
Qortex

Reputation: 7466

Your code looks correct (although it lacks all errors handling). Check that your file is in the working directory when you run your application. To see if it's just a problem with finding the file, try putting the full path to your file and see if it changes anything.

Otherwise, check that you have reading rights on your file. If it still doesn't work, you'll have to check for in state and see why it failed. Depending on your OS, you could investigate the situation with OS-specific functions (ReadFile on Windows, to see error code when you try to open the file). When you find the root cause and fix it, you can go back to the ifstream code.

And I guess you mean:

totalAmount += boxesSold * 3.50;

Upvotes: 0

Shania Twain
Shania Twain

Reputation: 11

I think your problem is that file girlscout.txt isn't in the directory you need it to be for your program to find it. you can fix this by putting the full path in your inFile.open call, or compiling and running the executable with your girlscout.txt file in the same directory

Upvotes: 1

Related Questions