Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
This program takes integers as input from the user. It has to enter these integers into a queue and then delete the negative integers entered if any and print out the queue this time without the negative integers.
Program does compile but It doesnt terminate once I enter EOF to stop entering numbers.What should i do??
CODE:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "queues.h"int main(void)
{
int *number;
int count;//create two queues
QUEUE* Q1;
QUEUE* Q2;Q1 = createQueue();
Q2 = createQueue();while((int) number!= EOF)
{
number = (int*)malloc(sizeof(int));
printf("Enter a number or EOF to exit: ");
scanf("%d", number);enqueue(Q1, number); //enqueue into Q1.
}
count= queueCount(Q1); //hold the count of the queue
while (count > 0)
{
dequeue(Q1, (void**)& number); //dequeue each integer from the queueif(number >= 0) //checks if it a +ve int
{
enqueue(Q2, number); //if number is +ve then enqueue it in Q2
}
count = count - 1; //decrement the countprintf("count of Q1 is %d\n", count);
}
printf("Integers in Q2 are %d\n", * number); // print only +ve integers from the new queuereturn 0;
}

You can't assign a string to a number.
You can, however, use gets(), check for the EOF, then call atoi()

I modified the code. It now enters integers in a queue, and the while loop terminates when I use Ctrl D to print the queue.
The problem is that this program has a if loop that doesnt seem to operate. If loop checks for each number that is dequeued from the old queue. If the number is a +ve integer it is entered in d new queue if not then it is just dequeued from the old queue.
Can you please show me how to solve this issue??
MODIFIED CODE
-----------------------------
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<string.h>
#include "queues.h"int main(void)
{
bool done = false;
int *number;
int count;//create two queues
QUEUE* Q1;
QUEUE* Q2;Q1 = createQueue();
Q2 = createQueue();emptyQueue(Q1);
emptyQueue(Q2);while(!done)
{
number= (int*)malloc(sizeof(int));
printf("Enter a number or <EOF> to exit: ");
if((scanf("%d", number)) == EOF )
done = true;
else
enqueue(Q1, number); //enqueue into Q1.}
printf("\n\nContents of queue Q2 are now:\n");
count= queueCount(Q1); //will hold the count of the queuewhile (count > 0)
{
dequeue(Q1, (void**)&number); //dequeue each integer from the queueif(number >= 0 ) //checks if it a positive integer
{
enqueue(Q2, number); //if number is +ve then enqueue it in the new queue
}count = count - 1; //decrement the count
printf("%d\t", *number);
free(number);
}printf("\n");
destroyQueue(Q1);
destroyQueue(Q2);
return 0;
}

I changed my code, but it still makes no difference. If negative integers are entered by the user, they should be deleted but that doesnt happen.cant figure out why?

Question: When do you ever go though Q2? You go though Q1, sure, but not Q2. Also, you probably shouldn't be free()ing number.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |