C++ - 使用用户输入的字符串数据检查结构字符串数据(无限执行 while 循环)

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

本文关键字:字符串 数据 无限 执行 while 循环 结构 检查 用户 输入 C++      更新时间:2023-10-16

正如您从标题中看到的那样,我想将用户输入的字符串与从文件中读取的字符串"比较"。 我使用结构数据类型从文件中读取数据。

这是输入文件

Samed Skulj Stomatolog
Adin Vrbic Hirurg
Ahmed Skulj Psihijatar

在我们讨论问题之前,这里是代码

#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文件中读取的字符串?

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