Queue Input Problem Postup
Nov 9, 2015 at 4:42pm UTC
Hi, I'm having trouble getting user input to work properly with my Queue.
The queue is working as it should. However, I cant get the user input right. Any help would be much appreciated.
Edit: I've resolved the issue. The solution was embarrassingly simple..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
const static unsigned MAX_ITEMS = 4;
class Queue{
public :
Queue();
unsigned size() const ;
void add(const string & item);
string remove();
void show() const ;
private :
string data[MAX_ITEMS];
unsigned first;
unsigned elements;
};
bool die(const string & msg);
int main(){
Queue test;
unsigned x = 0, Size;
//Hard Coded Input
//Hard coded Input
/*
test.add("Zero");
test.add("One");
test.show();
cout << "Size: " << test.size() << endl;
test.add("Two");
test.add("Three");
test.show();
cout << "Size: " << test.size() << endl;
test.remove();
test.remove();
test.show();
cout << "Size: " << test.size() << endl;
return
*/
// User Input
bool done = false ;
while (!done){
string Input;
cin >> Input;
if (Input == "." ){
done = true ;
}
else {
test.add(Input);
}
}
test.show();
return 0;
}
Queue::Queue(){
elements = 0;
first = 0;
}
unsigned Queue::size() const {
return elements;
}
void Queue :: add(const string & item){
if (elements == MAX_ITEMS){
die("Queue overflow" );
}
data[(first + elements) % MAX_ITEMS] = item;
elements++;
}
copy of its value.
string Queue::remove(){
if (!elements){
die("Queue underflow" );
}
elements--;
unsigned tempFirst = first;
first = (first + 1) % MAX_ITEMS;
return data[tempFirst];
}
void Queue::show() const {
cout << "Queue elements: " ;
for (unsigned x = 0; x < elements; x++){
cout << "[" << x << "] " << data[(first + x) % MAX_ITEMS] << " " ;
}
cout << endl;
}
bool die(const string & msg){
cout << "Error: " << msg << "The program will now self destruct." << endl;
exit(EXIT_FAILURE);
}
Last edited on Dec 15, 2015 at 6:37am UTC
Topic archived. No new replies allowed.