我自己编写了整个程序,该程序假设模拟手机上的来电。我已经修复了程序上的每个错误,但我从来没有能够成功地将数字逐行拉入我制作的队列中。我的问题是我使用 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++;
}
}