如何将字符串变量与从文件创建的字符串数组进行比较

How do I compare a string variable to an array of strings created from a file

本文关键字:字符串 数组 文件创建 比较 变量      更新时间:2023-10-16

你好,我一直在做一个程序,我有一个文件,有三列,其中包括总统的就职年份和离任年份,以及总统的姓名。我试图让用户输入一个总裁,并让程序返回开始和停止年份。我首先打开文件(正确打开),创建3个数组、2个整数数组和一个字符串数组。程序运行,但当我按2时,无论我输入什么名称,bool都保持为false。该文件可在以下位置找到:http://pastebin.com/8h3BJxGD

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
char junk, x;
int start[100],stop[100],year,i,count=0;
string names[100],president;
ifstream file;
file.open("presidents.txt");

if(file.fail())
    cout<<"failed to open file"<<endl;

for(i=0;file>>start[i];i++)
    {
    file>>stop[i];
    file.get(junk);
    getline(file,names[i]);
    cout<<start[i]<<stop[i]<<names[i]<<endl;
    count++;
    }
do
{
     cout<<"What would you like to know?"<<endl;
     cout<<"Press 1 for who was President in what year"<<endl;
     cout<<"Press 2 for the years served by a President"<<endl;
     cout<<"Press 3 to stop"<<endl;
     cin>>x;
     if(x=='1')
         {
         bool valid=false;
         cout<<"Enter a year: "<<endl;
         cin>>year;
         for(i=0;i<count;i++)
             {
             if(start[i]<=year&&stop[i]>=year)
                 {
                 cout<<names[i]<<endl;
                 cout<<endl;
                 valid=true;
                 }
             }
         if(valid==false)
             {
            cout<<"Invalid year"<<endl;
             cout<<endl;
             }
         }

   if(x=='2')
         {
         bool valid=false;
         cout<<"Enter a President: "<<endl;
         cin>>president;
         getline(cin,president);
         for(i=0;i<count;i++)
             {
             if(president==names[i])  
                 {
                 cout<<start[i]<<"-"<<stop[i]<<endl;
                 cout<<endl;
                 valid=true;
                 }
             }
         if(valid==false)
             {
             cout<<"Please be more percise"<<endl;
             cout<<endl;
             }
         } 
     }
     while (x!='3');
cin>>junk;
return 0;
}   

这里,问题不在于比较,而是将字符串输入到总统变量中,尝试打印总统变量的值,您就会理解问题。

你需要在阅读x.后添加以下行

cin.ignore(); //add this line after cin>>x;

这将从输入缓冲区中删除,并且在读取总统字符串时不会引起任何问题。在将格式化输入(即>>)与非格式化输入(如get()、getline()等)结合使用时,您需要注意这些问题。

以下是修改后的代码:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
char junk, x;
int start[100],stop[100],year,i,count=0;
string names[100],president;
ifstream file;
file.open("president.txt");

if(file.fail())
    cout<<"failed to open file"<<endl;

for(i=0;file>>start[i];i++)
    {
file>>stop[i];
file.get(junk);
getline(file,names[i]);
cout<<start[i]<<stop[i]<<names[i]<<endl;
count++;
}
do
{
 cout<<"What would you like to know?"<<endl;
 cout<<"Press 1 for who was President in what year"<<endl;
 cout<<"Press 2 for the years served by a President"<<endl;
 cout<<"Press 3 to stop"<<endl;
 cin>>x;
 cin.ignore();
 if(x=='1')
     {
     bool valid=false;
     cout<<"Enter a year: "<<endl;
     cin>>year;
     for(i=0;i<count;i++)
         {
         if(start[i]<=year&&stop[i]>=year)
             {
             cout<<names[i]<<endl;
             cout<<endl;
             valid=true;
             }
         }
     if(valid==false)
         {
        cout<<"Invalid year"<<endl;
         cout<<endl;
         }
     }

  if(x=='2')
     {
     bool valid=false;
     cout<<"Enter a President: ";
     getline(cin,president);
     for(i=0;i<count;i++)
         {
         if(president==names[i])  
             {
             cout<<start[i]<<"-"<<stop[i]<<endl;
             cout<<endl;
             valid=true;
             }
         }
     if(valid==false)
         {
         cout<<"Please be more percise"<<endl;
         cout<<endl;
         }
     } 
 }
 while (x!='3');
cin>>junk;
return 0;
} 

以下是输出:

What would you like to know?
Press 1 for who was President in what year
Press 2 for the years served by a President
Press 3 to stop
2
Enter a President: Theodore Roosevelt
1901-1909
if(president==)

如果总统==什么???

此外,

getline(cin,president);  //why this line?

如果你已经读取了文件并放入数据结构中,那么只需在数据结构中搜索用户输入的总统的名字。。。如果你找到了,像你做的那样跟踪索引,并在同一索引处打印出开始[]和结束[]。。。