Hey, my name is Alberto i am just starting to lear all about programming with C++ and one of my assignments is to open a file data1.txt into the program using ifstream inData where inData is the name of the variable for my file.
the problem i have found is that when i am trying to open the file and read the data it gives me a lot of erros. if someone can help me, by telling me what is my erro i would apriciate it.
this is what i have so far:

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

int main ()
{
    int item1, item2, item3;
    double count1, count2, count3;
    double price1, price2, price3;
    double itemTotal1, itemTotal2, itemTotal3;
    long double totalInventoryValue;

    totalInventoryValue = totalCount * totalItem;
    totalCount = count1 + count2 + count3;
    totalItem = item1 + item2 + item3;

    ifstream inData;

    inData.open("C:\\data1.txt");
    inData >> item1 >> price1 >> count1;
    getline (cin, item1);
    getline (cin, price1, count1);

    cin.ignore(100, '\n');



    return 0;                                                              
    }

and this is my file-data: "data1.txt"

1Gig DDR2 Memory
35.55 120
2Gig DDR2 Memory
48.99 60
19" Shart LCD Monitor
345.95 25

Recommended Answers

All 3 Replies

What do your errors look like? I think I have an idea of whats going on but I would like to see the output you are getting first.

Cameron

you know i already wrote another program that actualy is building but it seems like it is not reading the file i want, here it is the new program:

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

using namespace std;

int main ()
{
    string item1, item2, item3;
    double itemPrice1,itemPrice2, itemPrice3;
    double count1, count2, count3;
    double itemTotal1,itemTotal2,itemTotal3, totalInventoryValue;
    ifstream inFile;

    inFile.open("c:\\data1.txt");

    getline(inFile, item1);
    inFile >> count1 >> itemPrice1;
    inFile.ignore (100, '\n');

    getline(inFile, item2);
    inFile >> count2 >> itemPrice2;
    inFile.ignore (100, '\n');


    getline(inFile, item3);
    inFile >> count3 >> itemPrice3;
    inFile.ignore (100, '\n');

    cout << setprecision(2) << showpoint << fixed;
    itemTotal1 = count1 * itemPrice1;
    itemTotal2 = count2 * itemPrice2;
    itemTotal3 = count3 * itemPrice3;
    totalInventoryValue = itemTotal1 + itemTotal2 + itemTotal3;

    cout << left << setw(5) << "COUNT" << right << setw(9) << "ITEM" << right << setw(23)
         << "Price" << right << setw(7) << "ITEM" << right << setw(6) << "TOTAL" << endl;
    cout << setfill('-')<< right << setw(5) << " " << right << setw(26) << " " << right
         << setw(9) << " " << right << setw(11) << " " << endl;

    cout << setfill(' ') << setw(4) << count1 << setw(17) << item1 << setw(11)<< "$"
         << setw(7) << itemPrice1 << setw(2) << "$" << setw(9) << itemTotal1 << endl;    
    cout << setw(4) << count2 << setw(17) << item2 << setw(11) << "$" << setw(7)
         << itemPrice2 << setw(2)<< "$" << setw(9) << itemTotal2 << endl; 
    cout << setw(4) << count3 << setw(22) << item3 << setw(6) << "$" << setw(7)
         << itemPrice3 << setw(2)<< "$" << setw(9) << itemTotal3 << endl;
    cout << right << setw(47) << "----------" << endl;
    cout << "Total Inventory Value:" << setw(19) << "$" << setw(9) << totalInventoryValue << endl;


    return 0;                                                                  
    }

After infile.open check whether infile.good()
also check whether you need c:\data1.txt in lieu of c:\\data1.txt .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.