C :在结构中以零为零的输出
C++: output with extra zero in struct
我是C 的初学者,只是开始学习struct。我创建结构并从文件中读取数据。
这是我的文件:
John Smith 26832904 1657 Commerce St Flushing NY 11204 7183942833 Company01 962 51
我成功将数据输入到结构。输出是完美的,但最终只有零,这会让我发疯。
这是我的整个代码:
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
using namespace std;
struct nametype
{
string first;
string last;
};
struct addresstype
{
string address1;
string address2;
string address3;
string city;
string state;
string zip;
};
struct contacttype
{
string home;
string person;
};
struct employeeType
{
nametype name;
string id;
addresstype address;
contacttype contact;
string worklocate;
double basewage;
int carsale;
};
int main()
{
ifstream infile;
infile.open("employeeInfor.txt");
if (!infile)
{
cout << "Error." << "n";
}
employeeType employee;
employee.basewage = 0.0;
employee.carsale = 0;
infile >> employee.name.first >> employee.name.last
>> employee.id
>> employee.address.address1 >> employee.address.address2
>> employee.address.address3 >> employee.address.city
>> employee.address.state >> employee.address.zip
>> employee.contact.home >> employee.contact.person
>> employee.worklocate
>> employee.basewage >> employee.carsale;
cout << employee.name.first <<" "<<employee.name.last
<< " " << employee.id
<< " " << employee.address.address1 << " " << employee.address.address2
<< " " << employee.address.address3 << " " << employee.address.city
<< " " << employee.address.state << " " << employee.address.zip
<< " " << employee.contact.home << " " << employee.contact.person
<< " " << employee.worklocate
<< " " << employee.basewage << " " << employee.carsale << "n";
infile.close();
system("pause");
return 0;
}
i输出数据。那就是我得到的:
额外0的末尾不是我的期望。
顺便说一句。我想将结构放在数组中。使此功能适用于多个员工。我知道应该创建诸如applyepy型员工[5]之类的结构;我只是因为阅读文件而陷入困境。无法继续移动。
您尝试读取14个值,但是您的输入文件仅包含13个。因此,输出的最后一个值是employee.carsale
是0
,如初始化中。
您读取14个参数,但您的文件只提供13。也许您忘了在employeeInfor.txt
中添加一列。
相关文章:
- 介子复制/安装头文件到输出目录并保持文件夹结构
- 调用 cout 时如何在结构中输出常量文本?
- 如何使用函数的输出初始化 const 数组结构字段?
- C++可以输出一个结构的内存地址吗
- 使用文件和结构读取数据并输出数据
- CMAKE-输出目录结构和QML模块
- 如何使用结构分析此代码的输出
- boost :: Spirit :: Karma语法:逗号从结构上划定了带有选件属性的输出
- 如何输出属于向量数组的结构字符串
- C++ 如何将结构化 git 日志的输出解析为对象
- 将在 C++ 中输出多维数组内容的 for 循环的结构是什么
- 试图通过结构类型的向量,但没有控制台输出
- 如何在 c++ 中使用二进制文件输入/输出读取/写入结构的字符串类型成员
- C :在结构中以零为零的输出
- 输出用户定义的结构使用boost ::日志
- 将值输入到结构数组 + 打印输出
- 值语义与具有大数据结构的输出参数
- 输出结构的枚举返回负数
- 如何在编译时输出结构体中成员的偏移量(C/ c++)
- 使用位字段输出结构大小