c++ - 如何将变量分配给 ASCII 文件中的项目并在 C++ 中计算它们?

标签 c++ fstream

我被告知要在包含以下信息的文本文件中列出一个列表:

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

我写了这个程序来打开文件..但我不知道如何将像“凿子”这样的项目分配给程序中的变量,所以我可以用数量乘以价格来输出总价.

    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;
}

最佳答案

哥们下次认真使用delimiter,下面是可行但不是最优解的代码。此代码读取 text 文件并复制 item namechiselitem_name[20] array ,复制 quantity of item like 50qty 并且复制 rate of item like 9.99rate 然后通过计算你想要的qty*rate 来计算总价。这里使用了很多变量,请仔细观察。

#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;
}

关于c++ - 如何将变量分配给 ASCII 文件中的项目并在 C++ 中计算它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31595054/

相关文章:

C++ 模板函数 - 编译器错误 : 'can not change from from const char* to int'

c++ - osx - 构建 POCO 库时出现链接错误

c++ - QVector2D* 大小未知

C++ - 截断变量文件名中 double 的小数位

c++ - 我正在阅读的代码中有奇怪的括号

c++ - 模板类中的“静态”模板类——封装、继承还是……?

c++ - 从文件 C++ 中读取 3 个整数

c++ - C++ 文件读写

c++ - 当磁盘快满时,使用 C++ 流缓冲区在 Linux 中复制文件没有异常?

c++ - 从 fstream 中的文本读取到某一行