0

I am learning C++ and trying to print out the following using nested for loops in C++ in two cycles. In the end there should be 16 ADD operations and 8 DELETE operations:

ADD operation : Caller ID= 1 , Selected Queue= 2
ADD operation : Caller ID= 2 , Selected Queue= 2
ADD operation : Caller ID= 3 , Selected Queue= 1
ADD operation : Caller ID= 4 , Selected Queue= 1
ADD operation : Caller ID= 5 , Selected Queue= 1
ADD operation : Caller ID= 6 , Selected Queue= 2
ADD operation : Caller ID= 7 , Selected Queue= 0
ADD operation : Caller ID= 8 , Selected Queue= 1
DELETE operation : Caller ID= 1 , Selected Queue= 2
DELETE operation : Caller ID= 7 , Selected Queue= 0
DELETE operation : Caller ID= 2 , Selected Queue= 2
DELETE operation : Caller ID= 3 , Selected Queue= 1

My code is:

#include <stdio.h> // For printf function
#include <stdlib.h> // For srand and rand functions
#include <time.h> // For time function
#include <queue> // For STL queue class
#define N 2 // Number of simulation cycles
#define K 3 // Number of queues
#define IC 8 // Number of incoming calls per cycle (Number of ADD operations in a cycle)
#define FC 4 // Number of finished calls per cycle (Number of DELETE operations in a cycle)
using namespace std; // C++ requirement

int main()
{
    queue <int> Q[3]; // Array of STL queues (3 queues), which will store caller ID numbers
    int selected_queue; // Index of a randomly selected queue
    int caller_ID = 1; // Caller ID number
    int tmp_caller_ID; // Temporary caller ID
    srand(time(NULL)); // Seed (initialize) the random number generator
    int i,j;
    for(i=0;i<N;i++);
    {
        for(j=0;j<IC;j++);
        {
            selected_queue = rand() % 3; // Randomly determine the index number of a queue
            Q[selected_queue].push(caller_ID); // Add a caller ID number to the selected queue
            printf("ADD operation : Caller ID= %d , Selected Queue= %d \n", caller_ID, selected_queue);
            caller_ID++;
        }
        for(j=0;j<FC;j++);
        {
            selected_queue = rand() % 3; // Randomly determine the index number of a queue
            if (! Q[selected_queue].empty() ) // Check if the queue is not empty
                {
                    tmp_caller_ID = Q[selected_queue].front(); // Get (without deleting) a caller ID from selected queue
                    Q[selected_queue].pop(); // Delete a caller ID from the selected queue
                    printf("DELETE operation : Caller ID= %d , Selected Queue= %d \n", tmp_caller_ID, selected_queue);
                }
            else
            printf("DELETE operation : Selected Queue= %d (Queue is empty) \n", selected_queue);
        }
    }

}

But the problem is that when I run this code I get:

ADD operation : Caller ID= 1 , Selected Queue= 2                                                                  
DELETE operation : Selected Queue= 0 (Queue is empty)
  • I'm very new to programming languages, I didn't know about that tool.Thank you! – user12974345 May 26 '20 at 11:14
  • 1
    Please explain what is disappointing about the output. That you only get one each (see answer below)? That the queue is empty (what surprises you about this)? That the shown desired output does not match your description? – Yunnosch May 26 '20 at 11:20
  • To add to the (now deleted) debugging recommendation, here is some helpful information: https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb https://stackoverflow.com/questions/8041614/how-to-debug-in-codeblocks/58050981#58050981 – Yunnosch May 26 '20 at 11:22

1 Answers1

2

You have an semicolon at the end of your for statements that doesn't belong there.

You loops work, but they only execute the code block afterwards, which is an empty statement in your example.

Remove the semicolons and it should work just fine.

e.g. for(i=0;i<N;i++);{ should read for(i=0;i<N;i++){

Simon Kraemer
  • 5,700
  • 1
  • 19
  • 49