PART 1 of my Project:Using turbo c++ 4.5, and I'm very new to Programming, i'm not good
1. I made a program which stores 100 records please check if it's right.
2. I want to read each line into an array. I want to use a struct or what they call a struct array but i'm not yet familiar with it so I'm confused...I can't seem to figure out how would I read this .txt file and then store the data on the struct array.
I would like to read the txt file at the very first line of void main()
0001    Apples      Fruits      35.00
0002    Banana      Fruits      20.00
0003    Grapes      Fruits      55.00
0004    Kiwi        Fruits      60.00
0005    Lemon       Fruits      50.00
0006    Batteries   Misc        30.00
0007    Charcoal    Misc        15.50
0008    Flashlight  Misc        50.00
0009    Catsup      Condiments  30.00
0010    SoySauce    Condiments  20.00
0011    Vinegar     Condiments  20.00
0012    GravyMix    Condiments  35.00
0013    Curry       Condiments  20.25
0014    Candy       Snacks      20.00
0015    Cookies     Snacks      40.00
0016    Gelatin     Snacks      20.00
0017    Graham      Snacks      35.00
0018    PotatoChips Snacks      20.00
0019    Chicken     Meat        150.00
0020    Hotdog      Meat        90.00
0021    GroundBeef  Meat        170.00
0022    Pork        Meat        180.00
0023    Cereal      Breakfast   70.00
0024    Oatmeal     Breakfast   50.00
0025    PancakeMix  Breakfast   50.00
0026    Beer        Drinks      30.25
0027    Champagne   Drinks      85.50
0028    Coffee      Drinks      20.00
0029    DietSoda    Drinks      20.00
0030    BathSoap    Hygiene     27.50
0031    HandSoap    Hygiene     34.00
0032    HairSpray   Hygiene     50.00
0033    Lotion      Hygiene     55.00
0034    BrownRice   Pasta&Rice  60.00
0035    Lasagna     Pasta&Rice  75.00
0036    Spaghetti   Pasta&Rice  75.00
0037    Cabbage     Vegetable   40.00
0038    Eggplant    Vegetable   30.00
0039    Tomato      Vegetable   20.00
0040    Squash      Vegetable   45.00
0041    Lettuce     Vegetable   45.00
0042    Yoghurt     Dairy       40.00
0043    Butter      Dairy       25.00
0044    IceCream    Dairy      199.00
0045    Milk        Dairy       37.00
0046    Cheese      Dairy       30.00
0047    Bleach      Cleaning    20.00
0048    Sponges     Cleaning    50.00
0049    Dishwashing Cleaning    30.00
0050    FabCon      Cleaning    20.00
-the text file is long so I was thinking of reading it by using for loop/s...I tried and tried but failed miserably...
 #include<iostream.h>
        #include<cstring.h>
        #include<conio.h>
        #include<fstream.h>
        #include<time.h>

        int i=0;
        char yn;
        struct BuyerInfo
        {
         char id[5];
         string fn;
         string mn;
         string ln;
         string ad;


        }
         info[100];


        void scanb()
        {clrscr();
         cout<< "\n================================================================================";
         cout<< "\n\t\t\t\tSCAN BUYER'S ITEMS\n";
         cout<< "\n================================================================================";
         for(i=0; i<1; i++)
         {
            cout<<"\n----------Data---------\n";
            cout<< "\nBuyer's ID:B"<<info[i].id;
            cout<< "\nName:"<<info[i].ln<<","<<info[i].fn<<" "<<info[i].mn;
            cout<< "\nAddress:"<<info[i].ad;
            cout<<"\n-----------------------\n";
         }
           cout<< "\nBarcode and Quantity of Products Bought:";
               //this is where I'll input the barcode:0001
               //this is where I'll input the Quantity:2
              //hopefully it would get stored in a struct
           cout<<"\n-----------------------\n";
           cout<<"\nTry another transaction?[y/n]:";
           cin>>yn;
           clrscr();
        }


        void main()

        {

         cout<< "\n================================================================================";
         cout<< "\t\t\n\t\t  Please fill in the following required information\n";
         cout<< "\n================================================================================";
         for(i=0; i<1; i++)
         {
          time_t now = time(0);
          char* dt = ctime(&now);
          cout<<dt;
          cout<< "\nBuyer's ID:B";
          cin>>info[i].id;
          cout<< "Enter First Name:";
          getline(cin,info[i].fn);
          cout<< "Enter Middle Name:";
          getline(cin,info[i].mn);
          cout<< "Enter Last Name:";
          getline(cin,info[i].ln);
          cout<< "Enter Address:";
          getline(cin,info[i].ad);
        }
         A:
         struct BuyerInfo info[100];

         int ch;
            cout<< "\n================================================================================";
            cout<< "\t\t\t         VONGOLA POINT OF SALE SYSTEM";
            cout<< "\n================================================================================";
            cout<< "\nPlease choose from the following:";
            cout<< "\n[1]Scan Buyer's Items";
            cout<< "\n[2]Additional Items";
            cout<< "\n[3]Cancel Items";
            cout<< "\n[4]Searching for Buyer's Record";
            cout<< "\n[5]Deleting Buyer's Record";
            cout<< "\n[6]Show all Products available in the Grocery Store";
            cout<< "\n[7]Show Products based on Product Category";
            cout<< "\n[8]Exit";
            cout<< "\n================================================================================";
            cout<< "\nOption:";
            cin>>ch;
            switch(ch)
                {
                  case 1:

                         scanb();
                         if(yn=='Y'||yn=='y')
                         goto A;
                         else
                         break;
                }
        }

The above code is okay, given you are compelled to use Turbo. ;)

So, whats your question ? And, why aren't you using class instead of struct ?

we havent tackled it yet...if you want, you can teach me now..hehe.btw, we are making a Point of sale system, so are you saying its much more easier to do my project using classes??can you teach me?hahah..right now, im serious.WHAT is my question?well, Im trying to read the txt file but since its long as hell I figured to use a for loop/s to read it but I failed to do it...If you can help me with that, it would be awesome...thats really the problem I have now....If it works then I think I just need to make another struct, but how do I store the read data onto that struct..

Edited 4 Years Ago by RonKevin

Yes you need a loop for reading data from a file. Like this

// using File pointer

File *fp ; 
char c;

fp = fopen( "data.txt", "r") // means open file data.txt in "r"ead mode



while ( ( c = getc( fp ) ) != EOF )

          //means read a character at a time from a file unless EOF is encountered
{
   putc(c, stdout);
}

//same program using stream

fstream fp ( "data.txt", ios::in | ios::out);

char c ;

while( ! fp.eof() )
{
  c = fp.get();

  cout<< c;
}

Edited 4 Years Ago by np complete

wtf was that?!hahaha..damn...will that work on my compiler?I'll try

If you use class, reading and writing becomes simplified.
It uses read and write functions, for example :

// suppose you have a class student with their member data and member functions

student boy1;

fstream fp("data.dat" ios::in | ios::out)

while( ! fp.eof()) //reading from file
{
   fp.read( (char *) boy1, sizeof(boy1));
}

// writing to file

student boy2 ;

// take info of boy2

fp.write( (char *) boy2, sizeof(boy2));

You were told here about void main(), conio.h

You were told here about conio.h, void main(), putting non-code into your code to confuse us.

You were told here and here about void main()

And here about using goto

here again void main() was mentioned.

And I know goto, conio.h, formatting your code, and not proofreading your post has been mentioned before.

Question -- if you refuse to listen, why should we help you?

This article has been dead for over six months. Start a new discussion instead.