打印搜索到的行

时间:2019-01-15 00:59:04

标签: c++ search data-structures

我想从一个名字列表中搜索一个名字,如果找到了我应该返回那个人的全部信息,否则找不到。我不知道为什么我的代码无法正常工作。我可以打印此人的信息,例如:

输入:

3
Steve 9812761810 017
Wayne 8299915781 102
Ronnie 9161462903 120
Wayne 

输出:

Wayne 8299915781 102
#include <iostream>
#include <string.h>
using namespace std;

int main() {
  string name[100];
  long long number[100];
  int year[100], i, n, check = 0;
  string inp;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> name[i] >> number[i] >> year[i];
  }
  cin >> inp;
  for (i = 0; i < n; i++) {
    if (inp == name[i]) {
      check = 1;
    }
  }
  if (check == 0)
    cout << "Info Not found";
  else
    cout << "The Entered Name is found";
  for (i = 0; i < n; i++) 
    cout << name[i] << number[i] << year[i];
  return 0;
}

2 个答案:

答案 0 :(得分:1)

从输入流中读取一个整数后,应该在从输入流中读取任何字符串之前使用cin.ignore();

cin.ignore();忽略“换行”字符。

您修改的代码:

#include <iostream>
#include <string.h>
using namespace std;

int main() {
  string name[100];
  long long number[100];
  int year[100], i, n, check = 0;
  string inp;
  cin >> n;
  cin.ignore();
  for (i = 0; i < n; i++) {
    cin >> name[i] >> number[i] >> year[i];
    cin.ignore();
  }
  cin >> inp;
  for (i = 0; i < n; i++) {
    if (inp == name[i]) {
      check = 1;
    }
  }
  if (check == 0)
    cout << "Info Not found";
  else
    cout << "The Entered Name is found";
  for (i = 0; i < n; i++) 
    cout << name[i] << number[i] << year[i];
  return 0;
}

在此处了解更多信息:Using getline(cin, s) after cin

答案 1 :(得分:0)

在程序结束时,您将忽略check的值,而仅打印列表中的所有内容,而不管是否确实找到了输入的名称。

请尝试以下类似操作:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name[100];
    long long number[100];
    int year[100], i, n, idx = -1;
    string inp;

    cin >> n;
    for (i = 0; i < n; i++) {
        cin >> name[i] >> number[i] >> year[i];
    }

    cin >> inp;
    for (i = 0; i < n; i++) {
        if (inp == name[i]) {
            idx = i;
            break;
        }
    }

    if (idx == -1)
        cout << "Info Not found";
    else {
        cout << "The Entered Name is found: ";
        cout << name[idx] << " " << number[idx] << " " << year[idx];
    }
    return 0;
}

您还可以考虑使用struct来更好地组织数据,例如:

#include <iostream>
#include <string>
using namespace std;

struct myData {
    string name;
    long long number;
    int year;
};

istream& operator>>(istream &is, myData &data) {
    is >> data.name >> data.number >> data.year;
    return is;
}

ostream& operator<<(ostream &os, const myData &data) {
    os << data.name << " " << data.number << " " << data.year;
    return os;
}

int main() {
    myData data[100];
    int i, n, idx = -1;
    string inp;

    cin >> n;
    for (i = 0; i < n; i++) {
        cin >> data[i];
    }

    cin >> inp;
    for (i = 0; i < n; i++) {
        if (inp == data[i].name) {
            idx = i;
            break;
        }
    }

    if (idx == -1)
        cout << "Info Not found";
    else
        cout << "The Entered Name is found: " << data[idx];

    return 0;
}