mardi 30 juillet 2019

Problem with logic when usng if statements

I am writing a small program to accept a name and a score. The program will store data in vectors. Then the program will print the name and score of each entry. Then the program should be able to search for a stored name, and if found display the name and corresponding score, if not found it should display a "not found" message. The program works great, up until the point where a name is entered to be searched, if the name is stored, it will print the name and score correctly. If the name is not found, it will display "not found" correctly as well. The problem is if the name is found, the program will also display the "not found" message as well as the name and score. I am not sure where my logic is flawed.

I have tried using else if, I have tried separate loops, I have tried putting the if statement in the same loop. I have tried using a break statement. Nothing I try will remove the "not found" message when a record is found. Since this is just a program to help me learn, it isn't that big of a deal, however I would like to understand what is going wrong to help me learn more, and to fix this annoying issue.

#include<iostream>
#include<vector>
#include<string>
using namespace std;
inline void keep_window_open() { char ch; cin >> ch; }
vector<string> listOfNames{};
vector<int> listOfScores{};
string name = " ";
int score = 0;

int main()
{
    cout << "Please enter a name followed by a score: \n";
        cout << "Press enter when finished\n";
    cout << "To quit enter NoName 0\n";     
    while (cin >> name >> score && name != "NoName")
    {
        for (int i = 0; i < listOfNames.size(); ++i)
        {
            if (name == listOfNames[i])
            {
                cout << "error name is a duplicate: \n";
            }
        }
            listOfNames.push_back(name);
            listOfScores.push_back(score);      
    }
        cout << "\nlist of names and scores entered\n";
    for (int i = 0; i < listOfNames.size(); ++i)
    {
        cout << listOfNames[i] << ',' << listOfScores[i] << "\n";
    }
    cout <<"Please enter a name to search for\n";
    string searchName = " ";
    cin >> searchName;
    for (int i = 0; i < listOfNames.size(); ++i)
    {
        if (searchName == listOfNames[i])
        {
     cout << listOfNames[i] << ',' << listOfScores[i]<< "\n";
        break;          
            }   
        }

    for (int i = 0; i < listOfNames.size(); ++i)
    {       
        if (searchName != listOfNames[i])
        {
            cout << "Not found\n";
            break;
        }
    }       
    keep_window_open();

    return 0;
}

If the user enters John 22 and Tim 28, it will print the list, then when a user searched for a name, if the name is not found, it will print "Not found", however if the name is found, it will print the name and score, as it should, but the next line it prints is "Not found". This line should only print if the record is not found.

Aucun commentaire:

Enregistrer un commentaire