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

标签 c++ arrays string comparison text-files

你好,我一直在研究一个程序,我有一个包含 3 列的文件,其中包含总统的就职年份和离职年份,以及总统的姓名。我试图让用户输入总统并让程序返回开始和结束年份。我首先打开文件(正确打开)并创建 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;

这将从输入缓冲区中删除\n 并且在读取总统字符串时不会导致任何问题。在结合使用格式化输入(即 >>)和非格式化输入(即 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

关于c++ - 如何将字符串变量与从文件创建的字符串数组进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20366897/

相关文章:

c++ - 在没有 JavaScript 的情况下隐藏 Google map 中的标记

Java 数据结构 - 复数数组

javascript - 将 localStorage 值打印到 HTML 页面

java - java字符串如何不可变?

c - 为什么我会遇到此代码的段错误?

ios - 检查字符的 Unicode 值范围

c++ - 如何独立检查多个变量?

c++ - 可以将Boost消息队列文件重定向到用户指定的位置

c++ - 为大节点树动态分配内存

c++ - 在 C++ 中将整数存储到 char* 中