While(1)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Menaka
    New Member
    • May 2014
    • 2

    While(1)

    Code:
    int main()
    {
    	char Name[SIZE], C;
    	char RegNo[Reg_N];
    	char CourceNo[cource_L];
    
                 
    	while(1)
    	{
    		printf("Do you have details(Y or N):");
    		head1 = NULL;
    		scanf("%c",&C);
    		
    
    		if (C=='N' || C=='n')
    		{
    			print_list();
    			break;
    		}
    		else if(C=='Y' || C=='y')
    		{
    			printf("Enter E no:");
    			scanf("%s",&RegNo);
    			printf("Enter Name:");
    			scanf("%s",&Name);
    
    			printf("Enter Cources:\n");
    			scanf("%s",&CourceNo);
    			
    			while(strcmp(CourceNo, "END"))
    			{
                             cource_list *n_cource = (cource_list *)malloc(sizeof(cource_list));
    
    strcpy(n_cource ->cource, CourceNo);
    n_cource -> next_cource = head1;
    				head1 = n_cource;
    				scanf("%s",&CourceNo);
    			}
    			add_stu(Name, RegNo, head1);
    		}
    		
    	}
    	
    return 0;
    
    }
    I have a problem with this code. It prints 2 "Do you have detail:" from the second time onwards. If u guys have a solution for this please share. Thank you. :)
  • maya29988
    New Member
    • May 2014
    • 7

    #2
    is that going to infinite while loop even if you give N or n?

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      If C is anything other than Y or N then "Do you have detail" gets printed. I wonder if you are receiving spurious characters like maybe a newline. I suggest you add an else leg that prints out the spurious character so you can see what's happening.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Be aware than scanf processes to the first whitespace character, then stops. The whitespace character is not processed.

        Which means your enter key is still in the input buffer waiting for the next scanf to trip over it.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          It is generally a good idea to flush stdin just before prompting for new input. This insures that your program doesn't react to input from before the prompt was printed. Use of fflush involves implementation-defined behavior and hence isn't portable. You could instead loop on getchar.

          An independent issue is dealing with the newline that terminates scanf input. You can make scanf consume the newline by adding an explicit newline to the format string: scanf("%c\n",&C ). However, since you only want a single character -- perhaps you should be using getchar instead of scanf in the first place. But refer to setvbuf.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Generally, you cannot flush an input stream because you have no control over the buffer. That is, you can't tell when input is complete. Maybe a read buffer is flushed and maybe it isn't. Maybe there's more input beyond the buffer and maybe there isn't.

            Output buffers, on the other hand, can be flushed because your program loaded the buffer in the first place.

            Parsing input data is a complex tricky business and I advise students to enter friendly data in the expected format. Later when your C/C++ skills rival those of a cardiac surgeon you can mess with the input format.

            Comment

            • Menaka
              New Member
              • May 2014
              • 2

              #7
              Thanks guys for your valuable comments. I think i got some thing. I'm a student doing computer engineering. I think this help would be helpful for my future. Also I wish to keep logged in.... Thank you all again!

              Comment

              Working...