【问题标题】:input output C++ reading from a .txt file从 .txt 文件读取的输入输出 C++
【发布时间】:2012-11-17 01:02:08
【问题描述】:

如何将其打印到输出文件中,并按周计算这些人工资的总和和平均值,并按 4 周的总周数计算...

示例 txt 文件..

doe       jane
williams  tom
lons      adams

 45.7   56.3   345.6  344.7  // week 1
 43.6   89.0   543.6  12.5   // week 1  person 2
 90.5   78.0  345.4  345.6  //week 1 person 3
 67.5   34.5   56.6   34.5   // week2 person 1
  etc....for 4 weeks..

我知道使用循环有一种更简单的方法,我可以得到一些帮助吗?谢谢 :)

这是我目前所拥有的

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

int main()
{
ifstream infile;
 ofstream outfile;

double s1, s2, s3 , s4 ,s5;
double t1, t2,t3,t4,t5;
double w1, w2,w3,w4,w5;
string personlast,personfirst,personlast2,personfirst2,personlast3,personfirst3;
double sum, average;
int numberpeople, numberofweeks;

infile.open("data.txt");
outfile.open("output.txt");

outfile<< fixed<< showpoint;
outfile<< setprecision(2);

infile>> numberpeople >> personlast >> personfirst >> personlast2 >> personfirst2>>
  personlast3 >> personfirst3 >> numberofweeks;
outfile<< " The number of salespeople are " << numberpeople <<"they are" <<
personlast << personfirst << "and " <<
 personlast2 << personfirst2 <<
 "and " << personlast3 << personfirst3 <<"Number of weeks = " << numberofweeks;


infile>> s1 >> s2 >> s3 >> s4 >> s5;
outfile <<" sales for week 1  "<< " for" << personlast << personfirst << s1 << s2
<< s3 << s4 << s5 << endl;
sum= s1+s2+s3+s4+s5;
 outfile <<"Sum of first week is " << sum<<endl;

infile >> t1 >> t2 >> t3 >> t4 >> t5;
outfile <<" sales for week 1  "<< " for" << personlast2 << personfirst2 << t1 << t2 
<< t3 << t4 << t5 <<endl;



infile.close();
outfile.close();
return 0;

【问题讨论】:

  • 说明一行中的每个数字代表什么可能会有所帮助。
  • 你为什么不用std::vector-s?使用for 循环?
  • @BasileStarynkevitch 20 代表说它适用于 c++ 类,您不能使用 std::vector
  • 我是个大菜鸟,所以我不明白什么是标准向量或它是如何工作的..
  • 我需要一些辅导,愿意花 20 美元通过电子邮件获得价值一小时的 C++ 帮助...

标签: c++ input output


【解决方案1】:

我不会为你做作业,因为我不想,但我会给你一些开始的东西。基本概念是循环文件,读取每一行。使用类似(伪代码)的控件;

 while (string = readline() != EOF)
 {
     //split string on delimiters in this case spaces
     if (string piece is not an int)
     {
         // this is a name
         // set first and last name here
     }

     if (string piece is an int)
     {
        // set ints,
     }
 }

您还需要一些结构(如果您已经了解这些结构,这些可能应该是类)来保存您的数据。如果您可以使用字符串,请使用它们而不是 char* 如果您使用 char*,则必须动态声明一个 char 数组。那个,或者如果允许的话,你可以让他们char[64] first_name;

struct person {
   char *first_name;
   char *last_name;
   numbers[4] numbers;
};

struct numbers {
   float item1;
   float item2;
   float item3;
   float item4;
};

当您读取数据时,您需要将其读取到人员结构的实例中。

【讨论】:

  • @evanmcdonnal..你是否考虑今晚通过电子邮件辅导我..我愿意为你的时间支付 20 美元一小时..
  • 感谢您提供的代码 :) 但是..我需要进一步解释您的意思..就像我提到的那样,如果您今晚有空,将非常感谢一些辅导。
  • @user1766270 首先编写一些代码以使用循环逐行读取文件。如果您取得一些进展并编辑您的帖子,我将提供进一步的帮助。看看这个开始cplusplus.com/doc/tutorial/files
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 2020-03-30
相关资源
最近更新 更多