结构中的Visual Studioc 阵列大小太长

visual studioc++ array size in struct is too long

本文关键字:阵列 Studioc Visual 结构      更新时间:2023-10-16

我当前正在为我的初学者C 课程进行分配,本章是关于structs的。我正在使用Visual Studio,所以我不能为动态阵列(即没有向量等)做任何奇特的事情。

我遇到麻烦的家庭作业的一部分是在文件末尾读取带有某些空格的文件。由于我使用的是filename.eof(),所以它正在读取空白并记录该数据。我尝试做cin.ignore(xxxxx,' n'),但是这无效。我当前的排名是我想要的数据,只是一排垃圾。我如何摆脱垃圾?

a)将数据读取到数组中的功能。您可以使用名为supcer-1.txt的附件来测试您的代码。不用说您的代码必须与任何输入数据文件一起使用。当然,要进行测试,请使用文件避免在测试时输入数据。数据文件的名称必须始终由用户输入(请勿硬代码文件名)。另外,检查以确保存在给定的输入数据文件。如果文件不存在,请发出错误消息,以提醒用户有关无效的文件名。确保再次向用户询问另一个文件的名称。但是,在用户输入不正确的文件名以总计3次输入后,终止程序。注意:数据文件名可以输入函数内部。

文本文件看起来像这样:"

Duckey E Donald forward 8 2 21
Goof B Goofy defense 12 0 82
Brave A Balto goalkeeper 0 0 5
Snow W White defense 1 2 3
Alice I Wonderful midfield 1 5 15
Samina S Akthar right_defense 1 2 7
Simba P Green left_back 7 3 28
**************WHITESPACE****************************
**************WHITESPACE****************************

这是我的代码:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int subSize = 100;
//struct to store nameInfo
struct nameInfo
{
    string fName;
    char middleInitial;
    string lName;
};

//struct to store playerInfo
struct playerInfo
{
    nameInfo name;
    string postion;
    int goals;
    int penalties;
    int jersey;
};
int getData(playerInfo matrix[]);
void displayData(playerInfo matrix[], int arraySize);
int main()
{
    playerInfo p;
    playerInfo playerArray[subSize];
    int arraySize;
    int userSelection;
    string searchTerm;

    arraySize = getData(playerArray);
    cout << arraySize << " records found." << endl << endl;

    displayData(playerArray, arraySize); //call to display all data
    cout << endl;
    return 0;
}
//function to read the data into the array
int getData(playerInfo matrix[])
{
    ifstream infile;
    string fileName;
    int i = 0;          //counter to hold array row length
    int k = 0;          //counter for file input
    int x = 0;          //counter for user input

    cout << "Enter the file name (e.g. soccer-1.txt): ";
    getline(cin, fileName);
    cout << endl;
    infile.open(fileName.c_str());
    //checks if file exists
    //ask the user again for the name of another file
    //loop returns -1 after 3 failed attempts to enter a file
    while (!infile)
    {
        k++;
        cout << "After attempt " << k
            << " input file not opened." << endl;
        cout << "Attempt " << k + 1 << ", enter the file name (e.g. soccer.txt): ";
        getline(cin, fileName);
        cout << endl;
        infile.open(fileName.c_str());

        cout << endl;
        if (k == 2) //terminate program at 2 because entered validation loop
        {           //after first attempt
            cout << "Terminating program.";
            return -1;
        }
    }
    while (!infile.eof())
    {
        infile >> matrix[i].name.fName >> matrix[i].name.middleInitial
            >> matrix[i].name.lName >> matrix[i].postion
            >> matrix[i].goals >> matrix[i].penalties
            >> matrix[i].jersey;
        i++; //holds size of array
    }
    infile.close();
    return i;
}

void displayData(playerInfo matrix[], int arraySize)
{
    for (int y = 0; y < arraySize; y++)
    {
        //display format:
        //Duckey.(E)Donald:8 2 21 – forward
        cout << matrix[y].name.fName
            << ".(" << matrix[y].name.middleInitial << ")"
            << matrix[y].name.lName << ":" << matrix[y].goals << " "
            << matrix[y].penalties << " " << matrix[y].jersey
            << " - " << matrix[y].postion << endl;
    }
}

好吧,这是您可以应用更改的地方:

while (!infile.eof()) {
  infile >> matrix[i].name.fName >> matrix[i].name.middleInitial >>
      matrix[i].name.lName >> matrix[i].postion >> matrix[i].goals >>
      matrix[i].penalties >> matrix[i].jersey;
  i++; // holds size of array
}

读取到字符串或一组字符串,而不是直接到容器。这样,您可以检查它们在复制到matrix容器之前的有效(即非空白)。