-1

我自己编写了整个程序,该程序假设模拟手机上的来电。我已经修复了程序上的每个错误,但我从来没有能够成功地将数字逐行拉入我制作的队列中。我的问题是我使用 getline 从文本中画线的正确方法是什么?这是我的程序。

#include <string>
#include <queue>
#include <deque>
#include <iostream>
#include <fstream>
#include <stack>
using namespace std;

class cell
{
    public:
        cell();
        void upload( string left );//Insert in order.
        void printnew();//Print call just recieved.
        void printlater();//Print calls for later.
        void decidenew();// Prompts user to decide what to do with recent calls.
        void decidelater();// Prompts user to decide what to do with call back numbers.
        void correctionnew();//Corrects the amount of numbers in new calls list.
        void correctionlater();//Correxts the amount of number in the call back list.

    private:
        queue<string> N;
        queue<string> O;
        queue<string> T;
        stack<string> Tem;

};

int main()
{
    string left;
    int i=1;
    cell num;
    num.upload( left );
    int n=1;

    while(i != 3)
    {
            cout << "Please check for recent calls." << endl;
            num.upload(left);
            cout << "This is the main menu and here are your options:" << endl;
            cout << "Press 1 to view your recent calls" << endl;
            cout << "Press 2 to view your call back list" << endl;
            cout << "Press 3 to turn off cellphone" << endl;

            cin >> i;
            if(i = 1)
            {
                 num.printnew();
                 num.decidenew();
                 num.correctionnew();
                 num.correctionlater();
            }
            if(i = 2)
            {
                 num.printlater();
                 num.decidelater();
            }
    }


    system("Pause");
    return 0;
}

void cell::printnew()
{
     cout << "Here is the list of the latest callers:" << endl;
     while(N.empty())
     {
          cout << N.front();
          T.push(N.front());
          N.pop();
     }

     while(T.empty())
     {
          N.push(T.front());
          T.pop();
     }
}

void cell::printlater()
{
     cout << "Here is the list of your call back list:" << endl;
     while(!O.empty())
     {
          cout << O.front();
          T.push(O.front());
          O.pop();
     }

     while(!T.empty())
     {
          O.push(T.front());
          T.pop();
     }
}
void cell::decidenew()
{
     int p=0;
     while(!N.empty() || p != 4)
     {
         cout << "What would you like to do with" << N.front() << "?" << endl;
         cout << "You can:" << endl;
         cout << "Press 1 to call back." << endl;
         cout << "Press 2 to ignore the call." << endl;
         cout << "Press 3 to Send to call back list." << endl;
         cout << "Press 4 to return to the main menu." << endl;
         cin >> p;

         if(p = 1)
         {
             N.pop();
         }
         else if(p = 2)
         {
             N.pop();
         }
         else if(p = 3)
         {
             O.push(N.front());
             N.pop();
             while(!O.empty() || N.front() != O.front())
             {
                 T.push(O.front());
                 O.pop();
             }
             if (N.front() != O.front())
             {
                 O.push(N.front());
                 N.pop();
             }
             while(!O.empty())
             {
                 T.push(O.front());
                 O.pop();
             }
             while(!T.empty())
             {
                 O.push(T.front());
                 T.front();
             }              
         }
         else if(p = 4)
         {
         }
         else
         {
             cout << "That is an incorrect input, please try again." << endl;
         }

     }
}
void cell::decidelater()
{
     int dummy = 0;
     while(!O.empty() || dummy != 4 )
     {
         cout << "What would you like to do with" << O.front() << "?" << endl;
         cout << "You can:" << endl;
         cout << "Press 1 to call back." << endl;
         cout << "Press 2 to delete number." << endl;
         cout << "Press 3 to keep and call later." << endl;
         cout << "Press 4 to go back to main menu." << endl;
         cin >> dummy;

         if( dummy = 1)
         {
             O.pop();
         }
         else if( dummy = 2)
         {
              O.pop();
         }
         else if( dummy = 3)
         {
              T.push(O.front());
              O.pop();
         }
         else if( dummy = 4)
         {
         }
         else
         {

             cout << "That is an incorrect input, please try again." << endl;
         }
         while(!T.empty())
         {
             O.push(T.front());
             T.pop();
         }
     }
}
void cell::upload( string left)
{
    int g = 0;
    string item;
    fstream myfile;
    myfile.open("CALLS.txt");
    while(!myfile.eof || g != 3)
    {
        getline( myfile, left);
        while(!N.empty() || left != N.front())
        {
            T.push(N.front());
            N.pop();
        }
        if(left != N.front())
        {
        N.push(left);
        }
        while(!N.empty())
        {
            T.push(N.front());
            N.pop();
        }
        while(!T.empty())
        {
           N.push(T.front());
           T.pop();
        }

        g++;
    }
    while(!N.empty())
    {
        Tem.push(N.front());
        N.pop();
    }
    while(!Tem.empty())
    {
        N.push(Tem.top());
        Tem.pop();
    }
    g=0;
}
void cell::correctionnew()
{
     int l = 0;
     while(!N.empty())
     {
         T.push(N.front());
         N.pop();
     }
     while(!T.empty() || l != 8)
     {
         N.push(T.front());
         T.pop();
         l++;
     }
}
void cell::correctionlater()
{
     int m = 0;
     while(!O.empty())
     {
         T.push(O.front());
         O.pop();
     }
     while(!T.empty() || m != 3)
     {
         O.push(T.front());
         T.pop();
         m++;
     }
}
4

1 回答 1

0

我认为这段代码不应该编译。如果它确实编译,问题显然是不应该编译的:

while (!myfile.eof || g != 3)

这一行有两个问题,一个是阻止它编译(你可能打算写myfile.eof();注意额外的括号),另一个是语义(假设添加了缺少的括号):流无法提前知道试图做什么被阅读。因此,总是需要在尝试读取之后检查读取是否成功。这条线应该看起来像这样:

while (g != 3 && std::getline(myfile, left))
于 2012-12-02T00:48:29.690 回答