簡體   English   中英

C++ - 使用用戶輸入的字符串數據檢查結構字符串數據(無限循環)

[英]C++ - Checking struct string data with user entered string data (infinite do while loop)

從標題中可以看出,我想將用戶輸入的字符串與從文件中讀取的字符串“比較”。 我使用 struct 數據類型從文件中讀取數據。

這是輸入文件

Samed Skulj Stomatolog
Adin Vrbic Hirurg
Ahmed Skulj Psihijatar

之前我們go到問題這里是代碼

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct dataAboutDoctors
{
    string name;
    string surname;
    string profession;
};

int main(int argc, char** argv) 
{
    ifstream readingDoctors;
    readingDoctors.open("citanjedoktora.txt");
    if (readingDoctors.fail())
    {
        cout << "Ne postojeca datoteka";
        exit(1);
    }

    dataAboutDoctors doctors[100];
    int numbersOfDoctors = 0;

    while(readingDoctors >> doctors[numbersOfDoctors].name >> doctors[numbersOfDoctors].surname >> doctors[numbersOfDoctors].profession)
    {
        numbersOfDoctors++;
    }
    cout << "----- Name of doctors and their professions -----" << endl;
    cout << "---------------------------------------------" << endl;
    string temp1,temp2;
    for (int i = 0; i < numbersOfDoctors; i++)
    {
        cout << "Name of " << i+1 <<". doctor: " << doctors[i].name << endl;
        cout << "Surname of " << i+1 <<". doctor: " << doctors[i].surname << endl;
        cout << "Profession of " << i+1 <<". doctor: " << doctors[i].profession << endl;
        cout << "------------------------------------------" << endl;
        temp1+=doctors[i].name;
        temp2+=doctors[i].surname;
        cout << endl;
    }
    string name1,surname1;
    cout << "Enter the name of doctor you see: ";
    cin >> name1;
    cout << "Enter the surname of doctor you see: ";
    cin >> surname1;
    do
    {
    if (name1 != temp1 || surname1 != temp2)
    {
        cout << "There is no name nor surname of the doctor you have entered. Enter again: ";
        cin >> name1 >> surname1;
    }
    }while(name1 != temp1 || surname1 != temp2);

    return 0;
}

當用戶輸入醫生的姓名和姓氏時,如果是正確的姓名和姓氏,程序應該繼續做其他事情,但在這種情況下或示例中,您看不到程序的其他部分,因為它不那么重要。

這是控制台輸入

----- Name of doctors and their professions -----
---------------------------------------------
Name of 1. doctor: Samed
Surname of 1. doctor: Skulj
Profession of 1. doctor: Stomatolog
------------------------------------------

Name of 2. doctor: Adin
Surname of 2. doctor: Vrbic
Profession of 2. doctor: Hirurg
------------------------------------------

Name of 3. doctor: Ahmed
Surname of 3. doctor: Skulj
Profession of 3. doctor: Psihijatar
------------------------------------------

Enter the name of doctor you see:

Enter the name of doctor you see:這是主要問題。 當用戶輸入正確的名字時,它會再次要求用戶輸入姓名和姓氏,然后再一次,所以你可以看到我已經成功地進行了無限循環。

我的問題是如何所謂“比較”用戶輸入的字符串和從 .txt 文件中讀取的字符串? PS:我是 C++ 的初學者,如果這是一個愚蠢的問題,我很抱歉。 提前致謝

在 for 循環中

string temp1,temp2;
for (int i = 0; i < numbersOfDoctors; i++)
{
    // ...
    temp1+=doctors[i].name;
    temp2+=doctors[i].surname;
}

即使沒有嵌入空格,您也在一個字符串中累積姓名和姓氏。

所以這個循環

do
{
if (name1 != temp1 || surname1 != temp2)
{
    cout << "There is no name nor surname of the doctor you have entered. Enter again: ";
    cin >> name1 >> surname1;
}
}while(name1 != temp1 || surname1 != temp2);

永遠不會中斷它的迭代,直到您輸入一個字符串,該字符串將包含單個字符串中的所有名稱和姓氏。

也許您需要將 if 語句和循環中的條件更改為

( temp1.find( name1 ) == std::string::npos || temp2.find( surname1 ) == std::string::npos )

並且在邏輯上相對於 if 語句中使用的條件,消息應該看起來像

"There is no name or surname of the doctor you have entered. Enter again: "
                 ^^^

實際上使用變量temp1temp2沒有意義。 你應該刪除它們。 這部分程序可以如下所示

#include <algorithm> // this header is required for using std::find_if

//...

string name1,surname1;
cout << "Enter the name of doctor you see: ";
cin >> name1;
cout << "Enter the surname of doctor you see: ";
cin >> surname1;

bool success = false;
do
{
    success = std::find_if( doctors, doctors + numbersOfDoctors,
                            [&]( const auto &d )
                            {
                                return d.name == name1 && d.surname == surname1;
                            } ) != doctors + numbersOfDoctors;

    if ( not success )
    {
        cout << "There is no name or surname of the doctor you have entered. Enter again: ";
        cin >> name1 >> surname1;
    }
} while( not success );

這是一個演示程序。

#include <iostream>
#include <string>
#include <algorithm>

struct dataAboutDoctors
{
    std::string name;
    std::string surname;
    std::string profession;
};

int main() 
{
    const size_t N = 100;
    dataAboutDoctors doctors[N] =
    {
        { "Samed", "Skulj", "Stomatolog" },
        { "Adin",  "Vrbic", "Hirurg" },
        { "Ahmed", "Skulj", "Psihijatar" }  
    };

    size_t numbersOfDoctors = 3;

    std::string name1, surname1;

    std::cout << "Enter the name of doctor you see: ";
    std::cin >> name1;
    std::cout << "Enter the surname of doctor you see: ";
    std::cin >> surname1;

    bool success = false;
    do
    {
        success = std::find_if( doctors, doctors + numbersOfDoctors,
                            [&]( const auto &d )
                            {
                                return d.name == name1 && d.surname == surname1;
                            } ) != doctors + numbersOfDoctors;

        if ( not success )
        {
            std::cout << "There is no name or surname of the doctor you have entered. Enter again: ";
            std::cin >> name1 >> surname1;
        }   
    } while( not success ); 

    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM