回 帖 发 新 帖 刷新版面

主题:急救~~为什么第二个循环不输出或不运行~~~~

以下为一个电话号码查寻程序,但第二个循环不知哪里出了问题,

各位前辈指点一下哈,谢谢!

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

using namespace std;

int main()
{
    ifstream inFile;
    string ans,F,L,firstname,lastname,firstName,lastName,number;
    
    do
    {
        inFile.open("phonenumber.txt");
        do
        {
            cout<<"Enter the firstname you want to look up:";
            cin>>firstname;

            do
            {
                inFile>>firstName>>lastName>>number;
                if(firstname==firstName)
                {
                    cout<<firstName<<"  "<<lastName<<"  "<<number<<endl;
                    F=firstName;
                }
            }while(inFile);
            
            if(F!=firstname)
            {
                cout<<"The name is not in the tetephone directory."<<endl;
                break;
            }
            else
            {
                cout<<"Enter the lastname you want to look up:";
                cin>>lastname;
                do
                {
                    inFile>>firstName>>lastName>>number;
                    if(firstname==firstName&&lastName==lastname)
                    {
                        cout<<firstName<<"  "<<lastName<<"  "<<number<<endl;
                        L=lastName;
                    }
                }while(inFile);
            }
            if (F==firstname&&L!=lastName)
                cout<<"The name is not is in the tetephone directory."<<endl;
        }while(inFile);

        inFile.close();

        cout<<"Do you find it again?(Y/N)"<<endl;
        cin>>ans;
    }while(ans=="Y"||ans=="y");
    
    return 0;
}
以下为电话号码文件内容:(不用自己写的)
zhang shan 13435689
zhang hao 1564
li si 1245
li lan 15646
wang hao 156456

回复列表 (共1个回复)

沙发

while(ans=="Y"||ans=="y");初步看了下发现一个字符用'',字符串才用""

我来回复

您尚未登录,请登录后再回复。点此登录或注册