Variable not incrementing

I am writing a program where whoever has the highest priority gets to go out first. So I am using a priority queue. The code is working fine so far, but when I want to view the elements in the queue, they dont increment. For example, it should say job#1, job#2, job#3,.....So I am I have tried multiple ways to change it up, but I cant seem to make it increment. Also, can someone help me figure out a way I should make it view out job1#Student. The one with the highest priority is the instructor then TA then Student.

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
#include"pqtype.h"

struct JobPosition
{
	int jobID;
	JobPosition();
};

JobPosition::JobPosition()
{
	jobID = 1;
}

int showMenu();
void addJobs(PQTYPE&, int&);
void printJobs(PQTYPE&, int&);
void viewJobs(PQTYPE);

int main()
{

	int option;
	JobPosition jp;
	PQTYPE pq(jp.jobID);

	do
	{
		option = showMenu();

		switch (option)
		{
		case 1: addJobs(pq, jp.jobID);
		break;
		case 2: printJobs(pq, jp.jobID);
		break;
		case 3: viewJobs(pq);
		break;
		case 4:
		break;
		default: std::cout << "Wrong input\n";
		break;
		}

	} while (option != 4);


	return 0;
}

int showMenu()
{
	int choice;
	std::cout << " 1.)Add Job\n";
	std::cout << " 2.)Print Job\n";
	std::cout << " 3.)View Jobs\n";
	std::cout << " 4.)Exit\n";
	std::cout << " Enter Choice: ";
	std::cin >> choice;

	return choice;
}

void addJobs(PQTYPE& pq, int& jp)
{
	char jobChoice;

	std::cout << "Who is the job for ( Instructor(i or I), TA(t or T), Student(s or S) :";
	std::cin >> jobChoice;

	if (jobChoice == 'S' || jobChoice == 's' || jobChoice == 'I' || jobChoice == 'i' || jobChoice == 't' || jobChoice == 'T')
	{
		pq.Enqueue(jp);
	}
}

void printJobs(PQTYPE& pq, int& jp)
{
	pq.Dequeue(jp);
}

void viewJobs(PQTYPE pq)
{
	pq.printElements();
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "heap.h"
#pragma once

class PQTYPE
{

private:
	Heap m_Items;
	int m_length;

public:
	PQTYPE(int);
	bool isFull() const;
	bool isEmpty() const;
	void Enqueue(int&);
	void Dequeue(int&);
	void printElements();
};


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
#include "pqtype.h"

PQTYPE::PQTYPE(int size)
{
	m_length = size;
} 

bool PQTYPE::isEmpty() const
{
	return m_Items.m_elements.empty();
}

bool PQTYPE::isFull() const
{
	return m_length == 10;
}

void PQTYPE::Enqueue(int& newItem)
{
	if (isFull())
		std::cout << "Jobs are full\n";
	else
	{
		
		newItem = m_length;
		m_Items.m_elements.push_back(newItem);
		m_Items.ReHeapUp(0, newItem- 1);
	}
}

void PQTYPE::Dequeue(int& item)
{
	if (isEmpty())
		std::cout << "No jobs to print\n";
	else
	{
		item = m_length;
		std::cout << "Now printing Job#" << m_Items.m_elements[m_length] << std::endl;
		m_Items.m_elements.pop_back();
		item--;
		m_Items.ReHeapDown(0, item-1);
	}
}

void PQTYPE::printElements()
{
	if (isEmpty())
		std::cout << "No jobs to print\n";
	else
	{
		for (int i = 0; i < m_Items.m_elements.size(); i++)
			std::cout << "Job#" << m_Items.m_elements[i] << ":\n";
	}
}


1
2
3
4
5
6
7
8
9
10
#pragma once
#include <vector>

struct Heap
{
	std::vector<int> m_elements;
	void ReHeapDown(int root, int bottom);
	void ReHeapUp(int root, int bottom);
	void Swap(int& a, int& b);
};


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
#include "heap.h"

void Heap::ReHeapDown(int root, int bottom)
{
	int maxChild, rightChild, leftChild;

	leftChild = root * 2 + 1;
	rightChild = root * 2 + 2;

	if (leftChild <= bottom)
	{
		if (leftChild == bottom)
			maxChild = leftChild;
		else
		{
			if (m_elements[leftChild] <= m_elements[rightChild])
				maxChild = leftChild;
			else
				maxChild = leftChild;
		}

		if (m_elements[root] < m_elements[maxChild])
		{
			Swap(m_elements[root], m_elements[maxChild]);
			ReHeapDown(maxChild, bottom);
		}
	}
}

void Heap::ReHeapUp(int root, int bottom)
{
	int parent;
	if (bottom > root)
	{
		parent = (bottom - 1) / 2;
		if (m_elements[parent] < m_elements[bottom])
		{
			Swap(m_elements[parent], m_elements[bottom]);
			ReHeapUp(root, parent);
		}
	}
}

void Heap::Swap(int& a, int& b)
{
	int temp;
	temp = m_elements[a];
	m_elements[a] = m_elements[b];
	m_elements[b] = temp;
}
Last edited on
it should say job#1, job#2, job#3


Let's take a look at the line of code and see why it isn't incrementing.

std::cout << "Job#" << m_Items.m_elements[i] << ":\n";

m_elements is just a vector of numbers, and you fill that vector with

1
2
newItem = m_length;
m_Items.m_elements.push_back(newItem);


and m_length is set to one at the start and never changes. So you have a vector of numbers, m_elements, in which every number is one.

Last edited on
Then how do I change that variable to increment by one everytime I add a new item to the vector. Because I have been trying all morning and can't seem to know how.
The variable m_length that you use appears to have nothing whatsoever to do with the length of anything. Remove it. If you ever need to know the length of the vector m_elements, just ask it; m_elements.size()

The number you're trying to put in there appears to have something to do with jobID. But every job you ever create has the exact same jobID. That makes no sense? Shouldn't every job have a different jobID?

You have a struct called jobPositon. That makes no sense. The position of a job is just its position in the queue. The struct jobPosition shouldn't exist.

I get the feeling that you wrote too much code without testing it, and without coming up with a design first (you really need to know in advance what you're trying to create; the odds of getting it right if you don't know what to do are very bad) and you've lost track of what you're trying to do and now you're trying to fix your code by randomly changing bits of it.

The solution to this is to start testing it properly. At every step, output the complete contents of the queue. Add ONE job to it. Output the whole queue. Add ONE more job. Output the whole queue. When you get that working, you'll be able to start getting more elaborate. Your design is confused and parts of it seem to exist just because when you typed it in, the compiler didn't complain about it.

PQTYPE pq(jp.jobID);
You create the priority queue, and pass to the consturctor... the jobID of some job, which the constructor uses as a size? That makes no sense. What does a jobID have to do with the size of the queue?

All your problems are coming from your design just making no sense. Stop typing, and work out your design first.
Last edited on
Topic archived. No new replies allowed.