473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

While(1)

2
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     char Name[SIZE], C;
  4.     char RegNo[Reg_N];
  5.     char CourceNo[cource_L];
  6.  
  7.  
  8.     while(1)
  9.     {
  10.         printf("Do you have details(Y or N):");
  11.         head1 = NULL;
  12.         scanf("%c",&C);
  13.  
  14.  
  15.         if (C=='N' || C=='n')
  16.         {
  17.             print_list();
  18.             break;
  19.         }
  20.         else if(C=='Y' || C=='y')
  21.         {
  22.             printf("Enter E no:");
  23.             scanf("%s",&RegNo);
  24.             printf("Enter Name:");
  25.             scanf("%s",&Name);
  26.  
  27.             printf("Enter Cources:\n");
  28.             scanf("%s",&CourceNo);
  29.  
  30.             while(strcmp(CourceNo, "END"))
  31.             {
  32.                          cource_list *n_cource = (cource_list *)malloc(sizeof(cource_list));
  33.  
  34. strcpy(n_cource ->cource, CourceNo);
  35. n_cource -> next_cource = head1;
  36.                 head1 = n_cource;
  37.                 scanf("%s",&CourceNo);
  38.             }
  39.             add_stu(Name, RegNo, head1);
  40.         }
  41.  
  42.     }
  43.  
  44. return 0;
  45.  
  46. }
  47.  
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. :)
May 1 '14 #1
6 1357
is that going to infinite while loop even if you give N or n?
May 2 '14 #2
donbock
2,426 Expert 2GB
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.
May 2 '14 #3
weaknessforcats
9,208 Expert Mod 8TB
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.
May 6 '14 #4
donbock
2,426 Expert 2GB
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.
May 6 '14 #5
weaknessforcats
9,208 Expert Mod 8TB
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.
May 7 '14 #6
Menaka
2
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!
May 8 '14 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Bob Alistar | last post by:
Hello! I have an IVR server controlling Dialogic hardware through a DLL exposing the C API to Python. The server farms out incoming calls to python scripts based on the telephone number that...
1
by: Sean | last post by:
Hi, I am trying to write a simple chat/text messaging program but I am having some problems. I am a rookie when it comes to socket programming so I am not sure if I am doing the write thing or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.