Code Gives No Errors but Does Nothing in Command Prompt - c++

I am having trouble getting my code to run on command prompt, I am getting no errors but when I run the code nothing happens.
#include <iostream>
#include <stdexcept>
using namespace std;
//defines the maximum queue size
#define MAX_QUE_SIZE 10
//creates the "rules" for the queue
class queue {
private:
int A[MAX_QUE_SIZE];
int front;
int rear;
public:
queue() {
front = -1;
rear = -1;
}
//checks to see if the queue is empty
bool isEmpty() {
return (front == -1 && rear == -1);
}
//checks to see if the queue if full
bool isFull() {
return (rear + 1) % MAX_QUE_SIZE == front ? true : false;
}
//checks to see if the queue is full, if not then it adds to the queue.
//if so it gives an error message.
void enqueue(int element) {
if (isFull()) {
throw std::overflow_error("QUEUE FULL");
}
if (isEmpty()) {
front = 0;
rear = 0;
}
else {
rear = (rear + 1) % MAX_QUE_SIZE;
}
A[rear] = element;
}
//checks to see if the queue is empty, if not then it deletes from the queue
//if sos it gives an error message.
void dequeue() {
if (isEmpty()) {
throw std::underflow_error("QUEUE EMPTY");
}
else if (front == rear) {
rear = -1;
front = -1;
}
else {
front = (front + 1) % MAX_QUE_SIZE;
}
}
//checks to see if the queue is empty, if so it gives a message saying so
//if not then it prints all the items in the queue
void printqueue() {
if (isEmpty()) {
cout << "EMPTY QUEUE";
}
else {
int count = (rear + MAX_QUE_SIZE - front) % MAX_QUE_SIZE + 1;
cout << "Queue : ";
for (int i = 0; i < count; i++) {
int index = (front + i) % MAX_QUE_SIZE;
cout << A[index] << " ";
}
cout << "\n\n";
}
}
};
int main()
{
queue Q; // creating an instance of Queue.
int i;
int k = 0;
int x;
std::cout << "Please enter some integers (enter 0 to exit):\n";
//a do-while statement that adds to the queue
do {
std::cin >> i;
//tries to add to the queue, if the queue is full it gives and overflow error
try {
Q.enqueue(i);
}
catch (std::overflow_error e) {
std::cout << e.what() << endl;
}
} while (i != 0);
std::cout << endl;
Q.printqueue();
std:cout << "How many values do you want to dequeue:\n";
std::cin >> x;
cout << endl;
//a for loop that dequeues the number of items the user wants to delete
//try the foor loop and dequeue function, if the queue is empty then it gives an underflow error
try {
for (int k = 0; k < x; k++) {
Q.dequeue();
}
}
catch (std::underflow_error e) {
std::cout << e.what() << endl;
}
Q.printqueue();
return 0;
}
I am also typing in g++ -o ehQue ehQue.cpp to compile it. I am not sure if this is causing the error or if my code itself is causing the error. Any amount of help will be appreciated.

I suspect you're just not executing your code. It compiles and runs.
You're compiling the program (not executing it) with:
g++ -o ehQue ehQue.cpp
The command can be understood as calling the program "g++" which should be just an alias to "gcc" which is the compiler. It takes sources code and produces object code, which is then linked to produce an executable binary (program.)
-o ehQue
Is the command parameter to specify the output file name. The compiler will take the provided files and (attempt to) produce a working executable called "ehQue".
ehQue.cpp
Is your source code, which you specified to the compiler.
Within your terminal (where you typed the g++ command) you will, also, need to call the program using a command such as:
./ehQue
Or to be specific to the Windows command prompt:
ehQue
Where you should find that your program runs.
(Tangential) Unless you specifically need to re-invent the wheel, one of CPP's defining features is the Standard Template Library (STL) which is part of the core specification... wrapping a std::deque in a class with your print functions would be advisable.

Related

C++ binary search tree creates segmentation fault

I'm trying to make a program that identifies AVR assembly instructions by opcode, since those are just a list of 1's and 0's I thought it would be a good project to make a binary search tree for.
Sadly I keep getting segmentation faults when trying to search through the tree. As I understand it a seg fault is usually the result of trying to do stuff with a pointer that doesn't point to anything, but since I have a Boolean that I check first that should never happen.
I'm pretty sure it has something to do with the way I use pointers, as I'm not very experienced with those. But I can't seem to figure out what's going wrong.
Below is the code involved (SearchTree is only a global variable in this minimal example, not in the real program.):
The code:
#include <iostream>
void ADD(short &code) {std::cout << code << "\n";}
void LDI(short &code) {std::cout << code << "\n";}
void SBRC(short &code){std::cout << code << "\n";}
struct node
{
void(* instruct)(short &code);
bool hasInst = false;
struct node *zero;
bool hasZero = false;
struct node *one;
bool hasOne = false;
};
node SearchTree;
auto parseOpcode(short code, node *currentRoot)
{
std::cout << "Looking for a: " << ((code >> 15) & 0b01 == 1) << std::endl;
std::cout << "Current node 1: " << (*currentRoot).hasOne << std::endl;
std::cout << "Current node 0: " << (*currentRoot).hasZero << std::endl;
// Return instruction if we've found it.
if ((*currentRoot).hasInst) return (*currentRoot).instruct;
// Case current bit == 1.
else if ((code >> 15) & 0b01 == 1)
{
if ((*currentRoot).hasOne) return parseOpcode((code << 1), (*currentRoot).one);
else throw "this instruction does not exist";
}
// Case current bit == 0.
else {
if ((*currentRoot).hasZero) return parseOpcode((code << 1), (*currentRoot).zero);
else throw "this instruction does not exist";
}
}
void addopcode(void(& instruct)(short &code), int opcode, int codeLength)
{
node *latest;
latest = &SearchTree;
for (int i = 0; i <= codeLength; i++)
{
// Add function pointer to struct if we hit the bottom.
if (i == codeLength)
{
if ((*latest).hasInst == false)
{
(*latest).instruct = &instruct;
(*latest).hasInst = true;
}
}
// Case 1
else if (opcode >> (codeLength - 1 - i) & 0b01)
{
if ((*latest).hasOne)
{
latest = (*latest).one;
}
else{
node newNode;
(*latest).one = &newNode;
(*latest).hasOne = true;
latest = &newNode;
}
}
// Case 0
else {
if ((*latest).hasZero)
{
latest = (*latest).zero;
}
else{
node newNode;
(*latest).zero = &newNode;
(*latest).hasZero = true;
latest = &newNode;
}
}
}
}
int main()
{
addopcode(ADD, 0b000011, 6);
addopcode(LDI, 0b1110, 4);
addopcode(SBRC, 0b1111110, 7);
short firstOpcode = 0b1110000000010011;
void(* instruction)(short &code) = parseOpcode(firstOpcode, &SearchTree);
instruction(firstOpcode);
return 0;
}
EDIT: I still had some #includes at the top of my file that linked to code I didn't put on StackOverflow.
The error happened because I forgot to use the new keyword and was therefor populating my search tree with local variables (which were obviously now longer around by the time I started searching through the tree).
Fixed by using:
node *newNode = new node();
(*latest).one = newNode;
(*latest).hasOne = true;
latest = newNode;
Instead of:
node newNode;
(*latest).one = &newNode;
(*latest).hasOne = true;
latest = &newNode;

Returning name of lowest node

First of all, this is part of a university course, so whilst a copy-paste solution would do, I'm looking for a bit more depth. I'll be seeing my supervisor tomorrow anyways though.
Now onto the problem. I am implementing Dijkstra's algorithm for 5 linked nodes, A-E, which have their associated costs and links stored in a vector;
struct Node
{
char nodeLink; //adjacent link
int cost; //cost of a link
}; //to use in Dijkstra algorithm
class HeadNode
{
public:
char Name;
bool Visited;
vector<Node> nodes;
HeadNode(char x) { Name = x; Visited = false; }
};
class Graph
{
char Start = 'A';
char StartNode;
char CurrentNode;
char Destination = 'E';
int TotalCost = 0;
vector<HeadNode> hnode;
vector<char> path;
vector<int> weight;
public:
Graph();
void createHeadNode(char X);
void createAdjMatrix();
char LeastDistance(char node);
void printAdjMatrix();
void Dijkstra(char StartNode);
char GetStartNode();
};
int main()
{
Graph graph;
graph.createHeadNode('A');
graph.createHeadNode('B');
graph.createHeadNode('C');
graph.createHeadNode('D');
graph.createHeadNode('E');
graph.createAdjMatrix();
//graph.printAdjMatrix();
graph.Dijkstra(graph.GetStartNode());
system("pause");
return 0;
}
Graph::Graph()
{
}
void Graph::createHeadNode(char x)
{
hnode.push_back(x);
}
In order to properly implement the algorithm, I have created a precursor function, LeastDistance(), within the class graph. I also have a function to get the start node, but that isn't particularly important here;
char Graph::LeastDistance(char node)
{
int smallest = 9999;
char smallestNode;
for (int i = 0; i < hnode.size(); i++)
{
for (int j = 0; j < hnode[i].nodes.size(); ++j)
{
if ((node == hnode[i].Name) && (hnode[i].nodes[j].cost <= smallest) && (hnode[i].Visited == false))
{
smallest = hnode[i].nodes[j].cost;
smallestNode = hnode[i].nodes[j].nodeLink;
}
else
{
hnode[i].Visited = true;
break;
}
}
}
TotalCost = TotalCost + smallest;
return(smallestNode);
}
void Graph::Dijkstra(char StartNode)
{
CurrentNode = StartNode;
if (CurrentNode == Destination)
{
cout << "the start is the destination, therefore the cost will be 0." << endl;
}
else
{
while(true)
{
if (CurrentNode != Destination)
{
CurrentNode = LeastDistance(StartNode);
cout << CurrentNode << "<-";
}
else if (CurrentNode == Destination)
{
cout << endl;
cout << "The total cost of this path is:" << TotalCost;
TotalCost = 0;//reset cost
break;
}
}
}
}
My problem is that the LeastDistance fucntion appears always to return node C, leading to it being printed over and over, so it fills the console. So far, I have tried to debug using visual studio 2017, but I cant make much sense out of the watches. I have also tweaked the order of the breaks around, and tried to make sure the visited flag is being set to true. whether any precedence of operations is affecting this I am not sure.
Thanks in advance.
I would contend that there are multiple problems with the way you implement this... but I think the one that's causing you the problem you describe is the statement right here:
if (CurrentNode != Destination)
{
CurrentNode = LeastDistance(StartNode);
cout << CurrentNode << "<-";
}
Think about what this does. Let's say your first node isn't the one you're looking for, then you call least distance and find the next smallest node. Then you print it. Then you iterate on the while loop again only to find that CurrentNode isn't the one you're looking for, so you call LeastDistance(StartNode) again, which will return the exactly same value. Thus, you'll keep printing the same result which apparently is c.
Assuming everything else is correct, I think you want:
CurrentNode = LeastDistance(CurrentNode);

Why does my recursion not return to previous pointer?

I am working on an assignment in which we must create a 20-questions type game from a binary search tree. We read the tree in from a text file that is formatted like this:
Does it walk on 4 legs?
Does it fly?
*centipede?
Is it an insect?
*bird?
*butterfly?
Does it purr?
Does it howl?
*mouse?
*dog?
*cat?
Later, I am going to allow the user to add to this list. At the moment, however, I am unable to accurately read the list into a binary search tree. I have set it up so that (I think) it will use recursion and return to the previous "current" node pointer when it ends a loop of the function. Currently, however, the current node pointer remains the same.
The below function is passed a vector of the strings from the text file.
string line;
string guess;
bool start = true;
void buildTree(vector<string> gameData, Node* current, int &counter)
{
//fill node with question or answer
//recursive:
// add to the left until we encounter an asterisk
// add to the right
line = gameData[counter];
//if a question
if (line[0] != '*')
{
if (current->getData().empty())
{
current->setData(line);
cout << current->getData() << endl;
}
if (!start)
{
//if noChild is empty AND current isn't a guess, go to noChild
if ((current->getNo()->getData().empty())
&& (current->isGuess() == false))
{
current = current->getNo();
}
//otherwise, go to yes
else {
current = current->getYes();
}
}
while (counter < gameData.size())
{
if (!start) { counter++; }
start = false;
buildTree(gameData, current, counter);
}
}
//if a guess
else
{
//if data is full, go to no
if (current->getData().empty() == false)
{
current = current->getNo();
}
//otherwise, go to yes
else
{
//current = current->getYes();
for (int i = 1; i < line.size(); i++)
{
guess.push_back(line[i]);
}
current->setData(guess);
guess.clear();
cout << current->getData() << endl;
counter++;
current->setGuess(true);
}
}
}

How can I trace back the error

I was assigned to create an array check (to see if the array is increasing, decreasing, or neither [then exiting if neither]) and a recursive binary search for one of my assignments. I was able to do these things after some help from my peers, but I need help in finding what seems to be causing the error
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
Aborted
when running the code. I Googled this error and this error seems to be vague or I just am not understanding. It compiles without errors, but I need help in what finding what I did wrong. It is able to run without the binarySearchR function and its associating code, as the array check on its own was the previous assignment. Below is the code, and I thank you so much in advance!
#include <iosteam>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;
int checkArraySort (string *fileLines, int numberOfLines);
int binarySearchR (string *fileLines, string searchKey, int iMin, int iMax);
int main ()
{
int numberOfLines = 0;
string searchKey = 0;
cout << "Input search key: ";
cin >> searchKey;
ifstream fileIn;
fileIn.open("words_in.txt");
string line;
if (fileIn.eof()) /* Checks file to see if it is blank before proceeding */
{
exit (EXIT_SUCCESS);
}
else
{
while(!(fileIn.eof()))
{
fileIn >> line;
numberOfLines++;
}
fileIn.close(); /* closes fileIn, need to reopen to reset the line location */
fileIn.open("words_in.txt");
string *fileInLines;
fileInLines = new string[numberOfLines];
for (int i = 0; i < numberOfLines; i++)
{
fileIn >> line;
fileInLines[i] = line;
}
fileIn.close(); /* closes fileIn */
int resultingCheck = checkArraySort(fileInLines, numberOfLines);
if (resultingCheck == -1)
{
cout << "The array is sorted in descending order." << endl;
}
else if (resultingCheck == 1)
{
cout << "The array is sorted in ascending order." << endl;
}
else
{
cerr << "ERROR: Array not sorted!" << endl;
exit (EXIT_FAILURE);
}
int searchResult = binarySearchR (fileInLines, searchKey, 0, numberOfLines);
if (!searchResult == -1)
{
cout << "Key found at index " << searchResult << "." << endl;
}
else
{
cout << "Key not found at any index." << endl;
}
exit (EXIT_SUCCESS);
}
}
int checkArraySort (string *fileLines, int numberOfLines)
{
int result = 1; /* Ascending by default */
for (int i = 1; i < numberOfLines; i++) /* Checks if decending */
{
if (fileLines[i] < fileLines[i-1])
{
result = -1;
}
}
if (result == -1) /* Makes sure it is descending (or if it is neither) */
{
for (int i = 1; i < numberOfLines; i++)
{
if (fileLines[i] > fileLines[i-1])
{
result = 0;
}
}
}
return result;
}
int binarySearchR (string *fileLines, string searchKey, int iMin, int iMax)
{
// so, its gotta look at the center value and each times, it discards half of the remaining list.
if (iMax < iMin) /* If the minimum is greater than the maximum */
{
return -1;
}
else
{
int iMid = (iMin + iMax) / 2;
if (fileLines[iMid] > searchKey) /* If the key is in the lower subset */
{
return binarySearchR (fileLines, searchKey, iMin, iMid - 1);
}
else if (fileLines[iMid] < searchKey) /*If the key is in the upper subset */
{
return binarySearchR (fileLines, searchKey, iMin, iMid + 1);
}
else /*If anything else besides the two */
{
return iMid;
}
}
}
The easy way: add a bunch of cout s to see where you program goes and what the values are.
Pros
Easy to do
Cons
Requires a recompile each time you want to add more info
The hard way: Learn to use a debugger
Pros
Can inspect "on the fly"
Don't need to rebuild
Can use what you learn in every other C++ program
Cons
Requires a bit of research to learn how to do it.

How to reverse a queue and display it

I have made a queue class and I have a queue and I want to reverse it,however when I implement the function it doesn't display anything.
Here's it
void reverse(Queue <T> &queue) {
if(!queue.empty())
{
int temp=queue.queue_front;
queue.pop(queue.queue_front);
reverse(queue);
queue.push(temp);
}
}
The pop function I'm using requires a value,that's why there is queue.queue_front.I'm trying to do it with a recursion.Here's my print function
void display() {
for(int current = queue_front+1; current < queue_length; current++)
{
cout << "[" << current << "]=" << queue_array [current] << " ";
}
}
Now that's what I'm doing in the main function
Queue <int>queue1(10);
queue1.push(16);
queue1.push(14);
queue1.push(6);
queue1.push(60);
queue1.reverse(queue1);
queue1.display();
Here's the pop function
void pop(T& item) {
if (empty()) {
cout << "The Queue is empty!";
exit(1);
}
else {
queue_front = (queue_front + 1) % queue_size;
item = queue_array[queue_front];
queue_length--;
}
}
It doesn't display anything.Thanks.
You're testing with a queue of integers, which hides quite a few bugs.
You should have tested with a Queue<std::string>.
First, here:
int temp=queue.queue_front;
you're saving an index, not an element.
What you intend must be
T temp = queue.queue_array[queue.queue_front];
but there's no reason to mess with member variables (see below).
Here,
queue.pop(queue.queue_front);
you're passing a reference to the queue's member to pop.
The parameter to pop is where the top element is stored after it's popped.
In other words, after that line, queue.queue_front will be the element that was at the front of the queue, not the index of the new front of the queue.
This would probably work:
void reverse(Queue <T> &queue) {
if(!queue.empty())
{
T temp;
queue.pop(temp);
reverse(queue);
queue.push(temp);
}
}