Ifstream不会读取整数或其他任何东西
ifstream wont read integers, or anything else for that matter
我使用xcode。我编译得很好,然后一旦它运行,它成功打开文件,然后读取两个值,但值不进入变量,因此它跳过for循环并关闭文件并从main返回。
#include <stdio.h>
#include <iostream>
#include <fstream>
//salesman struct
struct Salesman
{
char firstname[64];
char lastname[64];
char middleinitial[1];
int averagecents;
int totalcents;
};
int main(int argc, const char * argv[]) {
//setup variables
const char* inputFilename = "TheSales.txt";
int numberPeople = 0, weeksToHandlePerPerson = 0;
int workweeklength = 5;
int totalcents = 0;
//open file
std::ifstream fileHandle;
fileHandle.open(inputFilename, std::ios::in | std::ios::app);
if(!fileHandle)
perror ( "Stream Failed to open because: " );
fileHandle >> numberPeople; <----- does not get value
fileHandle >> weeksToHandlePerPerson; <----- does not get value
//do calculations
for ( int i = 0; i < numberPeople; ++i ) <---- this gets skipped
{
Salesman nextsalesman;
fileHandle >> nextsalesman.firstname;
fileHandle >> nextsalesman.middleinitial;
fileHandle >> nextsalesman.lastname;
float t1, t2, t3, t4, t5;
fileHandle >> t1 >> t2 >> t3 >> t4 >> t5;
nextsalesman.totalcents = 100 * ( t1 + t2 + t3 + t4 + t5 );
nextsalesman.averagecents = nextsalesman.totalcents / workweeklength;
totalcents += nextsalesman.totalcents;
//print calculations calculateNumbers()
std::cout << "salesman " << i << " total: $" << nextsalesman.totalcents / 100 << "." << nextsalesman.totalcents % 100
<< " and average $" << nextsalesman.averagecents / 100 << "." << nextsalesman.averagecents % 100 << std::endl;
int averagecents = totalcents / ( numberPeople * weeksToHandlePerPerson );
std::cout << "total for all: " << totalcents / 100 << "." << totalcents % 100 << " and average for all $" <<
averagecents / 100 << "." << averagecents % 100 << std::endl;
}
fileHandle.close(); <---- this works
return 0; <---- then we return main.
}
文件:3
2
firstName1 A lastName1
20.00 25.00 30.90 40.00 55.50
20.00 25.00 30.90 40.00 55.50
firstname2 B lastName2
30.00 24.00 45.00 67.00 65.50
56.90 87.00 43.50 56.98 55.40
firstName3 C lastName3
62.00 34.50 12.50 34.00 34.90
70.00 80.00 90.00 65.00 39.00
其中第一个int为雇员数,第二个int为周数,每周为5天。
Actual output:
Expected output:
(fake output but expected form)
salesman1 total: 23424 avg: 3654
salesman2 total: 234 avg: 1654
salesman3 total: 424 avg: 364.
total for all: 5345683 and average for all: 34564564
当程序用来工作时输出是正确的
你的代码很好,为我工作
您是否使用文本编辑器在sale .txt开头写入unicode字节顺序标记?如果是这样,那么它会混淆你的程序。
您可以使用notepad++来剥离BOM,如下所述:
http://www.larshaendler.com/2015/01/20/remove-bom-with-notepad/(TextWrangler在OSX上可能是一个很好的选择)
相关文章:
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 为什么 c++ 中的 main() 函数不采用除 int 和 void 之外的任何其他返回类型
- 为什么当我没有变量或其他任何命名的东西时"no match for 'operator==' "错误?
- 如何在不进行任何其他修改的情况下"pretty print" C++的XML字符串?
- 将十六进制字符数组存储在字节数组中,而无需更改为 ASCII 或其他任何东西
- 如何对 int 变量应用验证,使其仅接受整数数据,并且在任何其他数据的情况下不会出错?
- GLSL 计算着色器 使用查找表设置缓冲区会导致不写入任何数据,与其他数据设置相同的缓冲区有效
- 如何计算该程序的复杂性?是否有任何其他复杂性较低的解决方案
- C const在函数声明中以及其他任何地方的anmpersand的右侧
- 如何使用"std::multimap"或任何其他容器对多个值进行排序?
- 如何在任何其他全局变量或静态变量之前创建/构造类实例
- SSO在STD :: String以外的任何其他标准库容器中使用
- 程序在C++ Tutor中执行时有效,但在其他任何地方都不起作用
- 避免重复变量类型:Return type Resolver、C++11 auto、..其他任何东西
- 强制仅在某些代码中使用的特定构造函数,而不是其他任何地方
- 除了在类和程序之前,函数是否需要在其他任何地方声明?
- Ifstream不会读取整数或其他任何东西
- 3摄像机捕获与视频输入或其他任何东西
- 模板中的构造函数不同:一个用于字符串,另一个用于其他任何东西