I have written the entire program myself, the program is suppose to simulate incoming calls on a cellphone. I have fixed every error on the program but I have never been able to successfully pull in the numbers line by line into the queues I have made. My question is what is the proper way for me to draw in the lines from the text using getline? Here is my program.
#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++;
}
}
I don’t think this code is supposed to compile. If it does compile, the problem is clearly what shouldn’t compile:
This line has two problems, one of them preventing it from compiling (you probably meant to write
myfile.eof(); note the extra parenthesis), the other being semantics (assuming the missing parenthesis are added): The stream cannot know ahead of time what is attempted to be read. Thus, it is always necessary to check after the attempt to read if the read was successful. This line should probably look like this instead: