C ++奇怪的“格式”输出字符串

时间:2012-05-20 12:25:45

标签: c++ borland-c++

我正在编写一个使用文件的C ++应用程序。我已经实现了标准操作,如从文件写入和读取,一些搜索功能等。

执行某些操作后,我的输出字符串以一种非常奇怪的方式出现。例如,文本“地址 - ”,来自“?п&& ...&”:

enter image description here

enter image description here

C ++中是否有任何可以通过文件操作填充的缓冲区,并且它正在执行此输出,因此我需要将其清空或清除它?

编辑:当我编写一个从我的文件中读取记录的函数时,出现问题,根据用户标准选择其中一些,然后对它们进行排序。

这是我的代码:

Stucture:

    struct Appartament
    {
         char address[50];
         char telephoneNumber[20];
         char view[10];

         double price;
         double distanceFromCenter;

         int roomCount;
     };

使用该文件的功能:

    //Search by room count and order by price (rising)
    void AdvanceSearch()
    {
        clrscr();

        Appartament currentRecord;
        int recordsCount=0;

        fstream currentFile("Records.dat",ios::binary|ios::in);

        if(!currentFile)
        {
            cout<<"Error - the file could not be opened."<<endl;
            return;
        }
        else
        {
            //Array with apartments records
            Appartament CurrentRecords[MaxRecords];

            currentFile.seekg(0L,ios::end);
            long int length=currentFile.tellg();
            currentFile.seekg(0L,ios::beg);

            int isAppartamentFound=0;

            if(length==0)
            {
                cout<<"The file is empty."<<endl;
                return;
            }
            else
            {
                int userRoomCount;

                do
                {
                    clrscr();
                    cout<<"Enter apartment room count - ";
                    cin>>userRoomCount;
                }while(userRoomCount<0);

                clrscr();

                cout<<endl<<"Apartments with "<<userRoomCount<<" rooms order by price:";

                currentFile.read((char*)(&currentRecord),sizeof(Appartament));

                while(!currentFile.eof())
                {
                    if(currentRecord.roomCount==userRoomCount)
                    {
                        CurrentRecords[recordsCount]=currentRecord;
                        recordsCount++;
                        isAppartamentFound=1;
                    }

                    currentFile.read((char*)(&currentRecord),sizeof(Appartament));
                }

                currentFile.close();
            }

            if(isAppartamentFound==0)
            {
                cout<<endl<<"There are no matches!"<<endl;
            }
            else
            {
                //If only one apartment is found
                if(recordsCount==1)
                {
                    cout<<endl;
                    ShowRecord(currentRecord);

                }
                else
                {
                    //Sort the records

                    Appartament tempApartament;

                    int isChangeMade=1;

                    for(int index=0;index<recordsCount;index++){

                          //ShowRecord(CurrentRecords[index]);
                    }

                    while(isChangeMade==1)
                    {
                        isChangeMade=0;

                        for(int index=0;index<recordsCount-1;index++)
                        {
                            if(CurrentRecords[index].price>CurrentRecords[index+1].price)
                            {
                                isChangeMade=1;

                                tempApartament=CurrentRecords[index];
                                CurrentRecords[index]=CurrentRecords[index+1];
                                CurrentRecords[index+1]=tempApartament;
                            }
                        }
                    }

                    for(index=0;index<recordsCount;index++)
                    {
                        ShowRecord(CurrentRecords[index]);
                    }
                }
            }
         }
    }

显示记录的功能:

    void ShowRecord(Appartament Record)
    {
        cout<<"Apartment Information"<<endl;
        cout<<"The address is - "<<Record.address<<" and it is only "<<Record.distanceFromCenter<<" miles away from the center."<<endl;
        cout<<"The price is "<<Record.price<<" $ for "<<Record.roomCount<<" rooms with "<<Record.view<<" view."<<endl;
    }

这是可能的输出:

enter image description here

我已经在其他机器上使用其他版本的Borland运行我的程序,它运行正常。这就是为什么问题是我的编译器出了什么问题:?

0 个答案:

没有答案