0

I want to search for a name from a list of names and if its found I should return the whole info of that person else not found. I dont know why my code is not working. I can print the info of the person, eg:

Input:

3
Steve 9812761810 017
Wayne 8299915781 102
Ronnie 9161462903 120
Wayne 

Output:

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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Stone cold Cold
  • 142
  • 1
  • 8

2 Answers2

1

After reading an integer from the input stream, you should use cin.ignore(); before reading any strings from the input stream.

cin.ignore(); ignores the "new line" character.

Your modified code:

#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;
}

Read more here: Using getline(cin, s) after cin

  • `operator>>` ignores leading whitespace **including line breaks**, unless `std::noskipws` is used. What you say would be true if the OP were reading values any other way, like `std::getline()`, `std::cin.get()`, etc, but since all reading in the OP's code is being done using `operator>>` only, calling `cin.ignore()` should not matter. Also, your code is making the same mistake the OP's code is making regarding writing out the wrong output. – Remy Lebeau Jan 15 '19 at 03:48
0

At the end of the program, you are ignoring the value of check and just printing everything in your list regardless of whether the entered name was actually found or not.

Try something more like this instead:

#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;
}

You might also consider using a struct to organize your data better, eg:

#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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770