C++ - 使用用户输入的字符串数据检查结构字符串数据(无限执行 while 循环)
C++ - Checking struct string data with user entered string data (infinite do while loop)
正如您从标题中看到的那样,我想将用户输入的字符串与从文件中读取的字符串"比较"。 我使用结构数据类型从文件中读取数据。
这是输入文件
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: "
^^^
事实上,使用变量temp1
和temp2
是没有意义的。您应该删除它们。程序的这一部分可以如下所示
#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;
}
相关文章:
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- C++ - 使用用户输入的字符串数据检查结构字符串数据(无限执行 while 循环)
- 如果我在块中编写字符串文字,是否会从数据部分复制整个字符串数据?
- 无法在 c++ 中将字符串分配给字符串数据类型
- 将特定字符串数据解析为变量
- C++ 将字符串数据包转换为 iphdr语言 - 字符串数据包的格式应该是什么?
- 基于给定字符串数据类型的链表删除节点
- 插入了 C++ 数组字符串数据,但在显示输出时不显示
- 使用来自串行端口的字符串数据来操作振镜扫描仪在更高的速度下会出错
- 将元音与字符串数据类型的字符串分开,并将其存储在新字符串中
- 放置在外部 DLL 中的类中的字符串数据的内存损坏
- 创建字符串数据串起
- 如何在C++中将字符串数据存储在字符串 2D 数组中
- C++程序不返回文件中文本和字符串数据的值
- C++:在字符串数据类型中创建单词"letter after letter"
- 将向量中的字符串数据解析为适当的数据类型 c++
- 如何将提取的字符串数据转换为字符数组
- 字符串数据类型变量初始化
- 如何从本机空间中的宗地中检索字符串数据
- 在C 中使用字符串数据类型的问题