C, doubly linked list problem
I'm trying to read input from the user about a开发者_开发百科 customer.
Input: mike,404 forbidden st,raleigh,nc,27607,123.78
Then add that customer to a alphabetically sorted, doubly linked list. The user can choose to insert an entry, delete and entry, or view the list. After adding the user control...my sscanf doesn't work properly anymore. I can not figure out why. I don't understand why I'm not able to print customer values in the user control version.
Also, any advice/links about the syntax of updating a nodes previous field would be very helpful :P
Thank you in advance
Working (without user control):
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXNAMELEN 19
#define MAXADDRLEN 49
#define MAXCITYLEN 19
#define MAXSTATELEN 2
struct aNode {
char name[MAXNAMELEN];
char address[MAXADDRLEN];
char city[MAXCITYLEN];
char state[MAXSTATELEN];
int zip;
float balance;
struct aNode *next;
struct aNode *prev;
};
typedef struct aNode node;
struct aNode *dLList;
void read();
void input();
void print();
void delete();
int insertSort(node*);
int main() {
//from text file
read();
input();
/*
int choice;
while(1) {
printf("\n1.INSERT\n2.DELETE\n3.DISPLAY\n4.END\n");
printf("Enter choice:");
scanf("%d",&choice);
switch(choice) {
case 1:
input();
break;
case 2:
delete();
break;
case 3:
print();
break;
case 4:
exit(0);
}
}
*/
return(0);
}
//TODO
void read() {
}
void input() {
struct aNode *current;
current = (struct aNode *)malloc(sizeof(struct aNode));
int buff = 120;
char temp[buff];
printf("Enter data: ");
fgets(temp, buff, stdin);
sscanf(temp, "%[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %d%*c %f",
current->name, current->address, current->city, current->state,
¤t->zip, ¤t->balance);
insertSort(current);
//only for testing
printf("%s, %s, %s, %s, %5d, $%9.2f\n",
current->name, current->address, current->city,
current->state, current->zip, current->balance);
}
void print() {
struct aNode *p;
for(p = dLList; p != NULL; p = p->next) {
printf("%s, %s, %s, %s, %5d, $%9.2f\n",
p->name, p->address, p->city,
p->state, p->zip, p->balance);
}
}
//TODO
void delete() {
}
int insertSort(node * current) {
struct aNode *p;
struct aNode *q;
p = (struct aNode *)malloc(sizeof(struct aNode));
p = current;
//need to link to previous node
if(dLList == NULL || strcmp(dLList->name, p->name) > 0) {
p->next = dLList;
p->prev = NULL;
return(0);
}else {
q = dLList;
while(q->next != NULL && strcmp(q->next->name, q->name) < 0) {
q = q->next;
}
p->next = q->next;
q->next = p;
return(0);
}
}
Output: mike, 404 forbidden st, raleigh, nc, 27607, $ 123.78
Not Working (with user control):
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXNAMELEN 19
#define MAXADDRLEN 49
#define MAXCITYLEN 19
#define MAXSTATELEN 2
struct aNode {
char name[MAXNAMELEN];
char address[MAXADDRLEN];
char city[MAXCITYLEN];
char state[MAXSTATELEN];
int zip;
float balance;
struct aNode *next;
struct aNode *prev;
};
typedef struct aNode node;
struct aNode *dLList;
void read();
void input();
void print();
void delete();
int insertSort(node*);
int main() {
//from text file
read();
//input();
int choice;
while(1) {
printf("\n1.INSERT\n2.DELETE\n3.DISPLAY\n4.END\n");
printf("Enter choice:");
scanf("%d",&choice);
switch(choice) {
case 1:
input();
break;
case 2:
delete();
break;
case 3:
print();
break;
case 4:
exit(0);
}
}
return(0);
}
//TODO
void read() {
}
void input() {
struct aNode *current;
current = (struct aNode *)malloc(sizeof(struct aNode));
int buff = 120;
char temp[buff];
printf("Enter data: ");
fgets(temp, buff, stdin);
sscanf(temp, "%[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %d%*c %f",
current->name, current->address, current->city, current->state,
¤t->zip, ¤t->balance);
insertSort(current);
//only for testing
printf("%s, %s, %s, %s, %5d, $%9.2f\n",
current->name, current->address, current->city,
current->state, current->zip, current->balance);
}
void print() {
struct aNode *p;
for(p = dLList; p != NULL; p = p->next) {
printf("%s, %s, %s, %s, %5d, $%9.2f\n",
p->name, p->address, p->city,
p->state, p->zip, p->balance);
}
}
//TODO
void delete() {
}
int insertSort(node * current) {
struct aNode *p;
struct aNode *q;
p = (struct aNode *)malloc(sizeof(struct aNode));
p = current;
//need to link to previous node
if(dLList == NULL || strcmp(dLList->name, p->name) > 0) {
p->next = dLList;
p->prev = NULL;
return(0);
}else {
q = dLList;
while(q->next != NULL && strcmp(q->next->name, q->name) < 0) {
q = q->next;
}
p->next = q->next;
q->next = p;
return(0);
}
}
Output: , , , , 0, $ 0.00
The problem is that scanf
doesn't consume the newline at the end of the choice string. It is then read by the fgets
call in input()
, so the input string is empty (apart from the \n
character).
This worked for me:
fgets(temp, buff, stdin);
sscanf(temp, "%d",&choice);
Is dLList
being initialized to NULL anywhere?
And if even if it is, is does not appear to get set to the first node when you insert a new node into an empty list.
精彩评论