Mathias Pettersson
Mathias Pettersson

Reputation: 19

How do I assign variables to items in a ASCII file and calculate them in C++?

I have been told to have a list in a text file that contains the following information:

Chisel 50 9.99  Hammer 30  15.99    Nails 2000 0.99
Bolts  200 2.99    Nuts  300  1.99  Soap 55 1.89

I have written this program to open the file.. and I have no idea on how to assign an item like "chisel" to a variable in the program, so I can multiply the amount by the price to output the total price.

    cout << "----Make sure that the appropriate text file is located \nin the same directory as this program----" << endl;
    cout << "\nNow enter the name of the text file, followed by " << ".txt" << endl;
    string item;
    int var1;
    int var2;
    int var3;
    char filename[50];
    ifstream wolf;  //object name
    cin.getline(filename, 50);
    wolf.open(filename);

    if (!wolf.is_open()){
        exit(EXIT_FAILURE);
    }
    char word[50];
    wolf >> word;
    while (wolf.good()){
        cout << word << " ";
        wolf >> word;
    }


    while (wolf >> item >> var1 >> var2 >> var3)
    {
        cout << item << var1 << var2 << var3 << endl;
    }
    system("PAUSE");
    return 0;
}

Upvotes: 1

Views: 112

Answers (1)

udit043
udit043

Reputation: 1620

Dude seriously use delimiter next time , below is the code that is feasible solution but not optimal. This code read text file and copy item name like chisel into the item_name[20] array , copy quantity of item like 50 into the qty and also copy rate of item like 9.99 into rate and then calculate total price by calculating qty*rate that you want . There are many variables used here so watch them carefully.

#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;

struct List
{
   char item_name[20];
   int qty;
   float rate;
}
List[20];

ifstream file;
int main()
{

    int i,j,y,l,z,null;
    l=j=0;z=-1;
    char filename[10];
    char line[80];char number[21];

    for(y=0;y<80;y++)
    {
      line[y]=NULL;
    }
    cout<<"Enter file name : ";
    cin>>filename;
    file.open(filename,ios::in);
    if(!file)
    {
      cerr<<"File does not exist !!!";
      exit(0);
    }
    while(file.getline(line,80))
    {
      z=z+1;j=0;
        i=strlen(line);
        for(y=0;y<i;y++)
        {
          if(isalpha(line[y]))
          { List[z].item_name[j]=line[y];j=j+1;}
          if(line[y]==' ')
          {
              for(null=0;null<=20;null++)
              {
                  number[null]=NULL;
              }
             if(isalpha(line[y-1]))
             {
                  l=1;j=0;
             }
             if(isdigit(line[y-1]))
             {
               l=2;j=0;
             }
             if(isalpha(line[y+1]))
             {
                  z=z+1;
             }
          }
          if(isdigit(line[y])||(line[y]=='.'))
          {
              number[j]=line[y];
              if(l==1)
              {
                  List[z].qty=atoi(number);
              }
             if((l==2)||(line[y]=='.'))
             {
                  List[z].rate=atof(number);
             }
             j=j+1;
           }
        }
    }
    file.close();

    for(y=0;y<=z;y++)
    {
       //cout<<List[y].item_name<<" "<<List[y].qty<<" "<<List[y].rate<<endl;
       cout<<"Name of item : "<<List[y].item_name<<" Qty of this item : "<<List[y].qty<<" Rate of this item : "<<List[y].rate;
       cout<<"\nPrice = Qty of item * Rate of this item = "<<List[y].qty*List[y].rate<<endl;
    }
    return 0;
}

Upvotes: 1

Related Questions