数组中多余的数据
Extra Unwanted data in an array
当我去打印我的数组时,它打印出来了,但是在最后有额外的数据。Extra Data位于最后一行输出之后的一行。它包含:" -13096448"(注意,它以空格开头)
/***************************************************/
/* Author: Sam LaManna */
/* Course: */
/* Assignment: Program 6 Elves */
/* Due Date: 12/9/11 */
/* Filename: program6.cpp */
/* Purpose: Write a program that will process */
/* the work done by santas elfs */
/***************************************************/
#include <iostream> //Basic input/output
#include <iomanip> //Manipulators
#include <string> //String stuff
#include <fstream> //File input/output
using namespace std;
void instruct (); //Function Declaration for printing instructions
void input (ifstream &infile, string &names, int &numoftoys); //Function declaration for getting data from file
void headers (); //Prints headers
int main()
{
string names [50]; //Array for storing names
int numoftoys [50]; //Array for storing the number of toys made
int i = 0;
int p = 0;
ifstream infile("elves.dat"); //Opens input file "elves.dat"
instruct(); //Function call to print instructions
while (!infile.eof())
{
input (infile, names[i] , numoftoys[i]);
++i;
}
for (int p = 0; p<i; p++)
{
cout << names[p] << " " << numoftoys[p] << "n";
}
return 0;
}
/***************************************************/
/* Name: instruct */
/* Description: Prints instructions to user */
/* Parameters: N/A */
/* Return Value: N/A */
/***************************************************/
void instruct ()
{
cout << "n" << "This program will calculate the toys made by santas elfs and assign" << "n";
cout << "a rating to each elf. It will also sort them and print average, min and max." << "n";
cout << "n" << "Make sure you have a file named elves.dat in the same directory as";
cout << " this porgram or you will recieve errors.";
cout << "n" << "n";
return;
}
/***************************************************/
/* Name: input */
/* Description: Reads from file */
/* Parameters: N/A */
/* Return Value: N/A */
/***************************************************/
void input (ifstream &infile, string &names, int &numoftoys)
{
infile >> names;
infile >> numoftoys;
// infile.ignore ('n');
return;
}
数据文件:
Smiley 662
Curley 88
Clementine 335
Jasper 105
Lucinda 775
Brunhilda 103
Florence 441
Oskar 820
Snowflake 990
Bernard 690
Punch 298
Chuckie 10
Frosty 102
Snowman 311
April 830
Merry 299
Sunshine 331
Buddy 1234
Carol 271
Misty 111
Harold 52
Henry 292
Twinkle 308
Starlight 703
Burr 112
Angelica 444
Bluenose 689
Harry 254
Twinkle 259
Stardust 121
Greensleeves 453
Noel 312
Happy 209
Yukon 534
Snowcap 190
Northpole 598
输入循环存在多个问题。它应该像下面这样:
while ((infile >> names[i]) && (infile >> numoftoys[i]))
{
++i;
}
这避免了在输入失败和文件末尾有空白行时处理额外的行(在这种情况下,最终的.eof()
测试将成功,input(...)
将失败,同时仍然增加i
。
检查转换是否成功。
bool input (ifstream &infile, string &names, int &numoftoys)
{
infile >> names;
if (infile.fail()) return false;
infile >> numoftoys;
if (infile.fail()) return false;
return true;
}
所以你可以这样使用:
if (input(...)) { /* valid data */ }
相关文章:
- 防止主数据类型C++的隐式转换
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 如何在C++中序列化结构数据
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 在c代码之间共享数据的最佳方式
- 链表,反向函数,数据结构
- 数据成员SFINAE的C++17测试:gcc vs clang
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 如何对点云数据进行排序
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 用户提供的多余数据会发生什么情况
- 数组中多余的数据