0

How can I exit a while loop?

I tried NULL, '\0' and '\n' but nothing works. I am trying to exit the loop if user types empty line or null character. I used == NULL but it doesn't go into the loop.

How should I exit the fgets loop?

Code

typedef struct node{
    char *num;
    struct node *next;
}Node, *NodePtr;


NodePtr makeNode(char *n){

    
    NodePtr np = (NodePtr) malloc(sizeof(Node));
    np -> num = n;   // (*np).num
    np -> next = NULL;
    return np;
}
// prints all the items in the list
void printList(NodePtr np){
    while(np != NULL){ // as long as there's a node
        printf("%s\n",np ->num );
        np = np -> next; //go  on to the next node
    }
}// end print list


// main function

int main(void){

    char n[10];
    NodePtr top,np,last;
    top = NULL;

    if(fgets(n,sizeof n,stdin) != NULL){
    
        while(n != '\0'){
            np = makeNode(n);    // create a new node containing n
            if(top == NULL){     // set top if first node
                top = np;
            }
            else{
                last->next = np; // set last-> next for other nodes
            }
            last = np;           //keepin track of last node
            if(fgets(n,sizeof n,stdin) != NULL){
                //do  nothing
                
                printf("User enter null\n" );
            }
            

        }
    }
    printList(top);
    return 0;
} // end main
Community
  • 1
  • 1
riar
  • 7
  • 4
  • Always add language tag in your question so that experts of that language can see your question. I added "C", please check if it is the language you are using. – Infinite Recursion Apr 09 '15 at 04:10
  • Please don't use spaces around either the arrow `->` or dot `.` operators. They bind very, very tightly and should be written, for example, as `np = np->next;`. – Jonathan Leffler Apr 09 '15 at 04:32
  • The condition in `if(fgets(n,sizeof n,stdin) != NULL){ //do nothing printf("User enter null\n" );` is wrong. `fgets()` returns a NULL on error or EOF; otherwise, it returns the non-null pointer it was passed. Your code needs to create a copy of the string in `n` somewhere. As it stands, every line will overwrite each previous line, leaving you with just the last line in your list. – Jonathan Leffler Apr 09 '15 at 04:35

1 Answers1

1

It looks like n is a pointer to an array of char. Consider trying to check

while(!(n[0] == '\0') && !(n[0] == '\n'))

That checks for a new line and the null character

  • It works fine.can you please tell me how to print all nodes as my print function is printing empty lines. – riar Apr 09 '15 at 16:05
  • In make node you are never allocating memory for the `num`. In `makeNode()` add some code like `np->num = (char *)malloc(sizeof (char) * 10)`, and then copy the values of `n` using srncpy. ie `strncpy(np->num,n,10)`. ATM you are setting all of the nodes `num` to be the same address as the variable n. which is set to null, so in the end all of your nodes num variables are set to null. – Stewart Grant Apr 10 '15 at 02:22