C ++数组和从文件中读取

时间:2017-04-13 01:04:17

标签: c++ arrays struct

我正在创建一个地址簿程序,允许用户按名字,姓氏,电话号码和地址进行搜索。提示用户输入文件名,并将文件读入数组。我无法通过现有的SearchFirstName函数进行修改以循环遍历数组。我多次读过这个话题,我只是不理解它。任何帮助将不胜感激。

文件

Dim DataRange As Variant
Dim Irow As Long
Dim Icol As Integer
Dim Mean As Double
Dim Span As Long

'Copies the data to the VBA variant
DataRange = Range("A12:C" & Cells(Rows.Count, "B").End(xlUp).Row)

'new time interval devided by the original time interval to get half the number of cells for range
Span = Round(Range("H10") / Range("B9") / 2, 0)

'want to select the mid point between the span as the starting point
For Irow = LBound(DataRange) + Span To UBound(DataRange) - Span Step Span * 2
    For Icol = 2 To 3
        Mean = DataRange(Irow, Icol)

       'Average the values in the array
       Mean = WorksheetFunction.Average(DataRange(Irow - Span, Icol), DataRange(Irow + Span, Icol))

       DataRange(Irow, Icol) = Mean

   Next Icol
Next Irow

'writes all the results back to the range at once
Range("D12:F" & Cells(Rows.Count, "B").End(xlUp).Row).Value = DataRange 

标头文件

Susan, Smith, 123 456 789 
101 Main Street
Bob, Smith, 567 345 9076 
456 Market Street

代码

#include<string>
using namespace std;

enum Title {Mr, Mrs, Ms, Dr, NA};

struct NameType {
Title title;
string firstName;
string lastName;
};

struct AddressType {
  string street;
  string city;
  string state;
  string zip;
};

struct PhoneType {
  int areaCode;
  int prefix;
  int number;
};

  struct entryType {
  NameType name;
  AddressType address;
  PhoneType phone;
};

const int MAX_RECORDS = 50;

 struct addressBookType {
   entryType record[MAX_RECORDS];
   int numEntries;
};

我的尝试

string bookArray[MAX_RECORDS];

int main()
{
   entryType userRecord;
   string filename;
   ifstream inData;
   char searchOption;

   OpenFile(filename, inData);

   MainMenu(inData, filename);

   return 0;
}

void OpenFile(string& filename, ifstream& inData)
{
    do {
       cout << "Enter file name to open: ";
       cin >> filename;

      inData.open(filename.c_str());

    if (!inData)
        cout << "File not found!" << endl;

} while (!inData);


  if(inData.is_open())
  {

       for(int i=0; i<MAX_RECORDS;i++)
       {
         inData>> bookArray[i];
       }
   }
}

// Searches passed file stream for a first name read from the user

 void SearchFirstName(ifstream& inData)
{
  string searchName;
  entryType userRecord;
  string normalSearchName, normalFirstName;
  char choice;
  bool found = false;

  cout << "Enter first name to search for: ";
  cin >> searchName;

  normalSearchName = NormalizeString(searchName);     // Convert name to all uppercase

  // Loop through all records in the file
  while (GetRecord(inData, userRecord)){

    normalFirstName = NormalizeString(userRecord.name.firstName);   // Convert retrieved string to all uppercase

    if (normalFirstName == normalSearchName) { // Requested name matches
        PrintRecord(userRecord);
        cout << "Is this the correct entry? (Y/N)";
        cin >> choice;
        choice = toupper(choice);
        cout << endl;

        if (choice == 'Y') {
            found = true;
            break;
        }
    }
}

// Matching name was found before the end of the file
if (inData && !found){
    cout << "Record found: " << endl;
    PrintRecord(userRecord);
    cout << endl;
}
else if (!found)   // End of file. Name not found.
{
    cout << searchName << " not found!" << endl << endl;
}

// Clear file fail state and return to beginning
inData.clear();
inData.seekg(0);
}

1 个答案:

答案 0 :(得分:0)

  1. 创建您的数组,在这种情况下,它将是std::vector,因为通过在while循环内运行GetRecord函数并将结果附加到矢量w / vector_variable_name.push_back(NormalizeString(value_returned_from_GetRecord));NormalizeString部分是您不必在以后拨打数十亿次。
  2. void SearchFirstName(std::vector<entryType> *in_data_arr>)
  3. 一样传递你的数组
  4. while循环更改为for循环:for (int i = 0; i < in_data_arr.size(); i++) {
  5. 在循环内部将normalSearchName = NormalizeString(searchName);更改为normalSearchName = in_data_arr[i].name.firstName;
  6. 从那里它通常应该是相同的。

相关问题