简体   繁体   English

填充和打印结构数组

[英]Filling and printing Array of Structs

my program is supposed to fill an array of structs and eventually sort them. 我的程序应该填充结构数组,并最终对其进行排序。 My problem that I'm running into is when I fill it and try to print it, it's printing out things that weren't inputted. 我遇到的问题是,当我填充它并尝试打印时,它会打印出未输入的内容。

#include <iostream>
#include <string>

using namespace std;

struct bankaccount
 {
        int number;
        //string name;
        int money;
 };

int main()
 {
        int i;
        bankaccount bank[10];
        for (i = 0; i < 10; i++)
        {
          cin >> bank[i].number >> bank[i].money;
         // getline(cin, bank[i].name);
        }
        for (i = 0; i < 10; i++)
        {
          cout << bank[i].number << " " << bank[i].money << endl;
        }
 }

And I'm inputting with a data file. 我正在输入一个数据文件。

4 5024.24
3 2234.23
2 4332.21
1 4567.32
8 2345.32
5 2233.56
9 9008.98
10 9430.23
6 4560.29
7 8384.08

And when I run it by typing cat data | 当我通过输入cat data运行它时| sortingarray.cpp, it gives me the weird output: sortingarray.cpp,它给了我奇怪的输出:

4 5024
0 1
-1469612912 32767
4197157 0
2 0
4197261 0
952461936 32593
4197168 0
0 0
4196608 0

in which only the first line is correct. 其中只有第一行是正确的。

Define data member money as having type double 将数据成员money定义为类型为double

struct bankaccount
{
        int number;
        //string name;
        double money;
};

Otherwise an error occurs when you are trying to enter an integer and the input buffer of the stream contains a dot. 否则,当您尝试输入整数并且流的输入缓冲区包含一个点时,将发生错误。 It is not a valid symbol for integer number representations. 对于整数表示,它不是有效的符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM