Thread: Where is it going wrong?

  1. #1
    Registered User
    Join Date
    Jun 2011
    Location
    Bolpur, India
    Posts
    42

    Where is it going wrong?

    Coding:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<string.h>
    
    int main()
    {
    	 struct student
    	 {
    		  int roll;
    		  char name[50];
    		  char department[10];
    		  char course[20];
    		  int yoj;
    	 }s[10];
    	 int n,year=0,i=0,choice;
    	 printf("\n Enter The no. of students you wanna save detail of->");
    	 scanf("%d",&n);
    	 for(i=0;i<n;i++)
    	 {
    		  printf("\n Enter Name of the student:");
    		  gets(s[i].name);
    		  printf("\n Enter details of Mr./Ms:");
    		  puts(s[i].name);
    		  printf("\n Enter his/her roll no:");
    		  scanf("%d",&s[i].roll);
    		  printf("\n Enter Department of the student:");
    		  gets(s[i].department);
    		  printf("\n Enter Course name of the student:");
    		  gets(s[i].course);
    		  printf("\n Enter Year of joining of the student:");
    		  scanf("%d",&s[i].yoj);
    	 }
    	 printf("\n 2 THINGS ARE THERE NOW YOU CAN DO!!!");
    	 printf("\n 1.Print The name of all the students who joined in a particualr year!!!");
    	 printf("\n 2.Exit!!!");
    	 printf("\n Ener your choice->");
    	 scanf("%d",&choice);
    	 do
    	 {
    
    		 switch(choice)
    		 {
    			  case 1: printf("\n Enter the year of joining->");
    						 scanf("%d",year);
    						 for(i=0;i<n;i++)
    						 {
    
    							  if(s[i].yoj==year)
    							  {
    									puts(s[i].name);
    							  }
    						 }break;
    			 case 2: printf("\nApplication is Exiting");
    						exit(0);
    						break;
    			 default:printf("\n Wrong Choice!!");
    						  break;
    		  }
    	  }while(choice!=2);
    
     return 0;
    }
    When running the program is not taking any input for name...Neither is it printing the name of sudents of the same year.It is not also taking input of department.I apprehend it is doe to not knowing the proper format of gets() that is creating problem.Please help me how to write gets() in case of array of structures.
    Last edited by mistu4u; 06-19-2011 at 12:09 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    1. When you use "scanf()", I believe that the newline character remains in the buffer when you hit ENTER. Therefore, it is picked up automatically by the next input function, "gets()". A quick bandaid for this is to put a "getchar()" right after the first "scanf()" funtion.

    2. Just for your information, "gets()" is dangerous to use.
    Cprogramming.com FAQ > Why gets() is bad / Buffer Overflows

    3. One of the typos might be confusing to the user:

    Code:
    printf("\n 3.Exit!!!");
    should be
    printf("\n 2.Exit!!!");
    4. Your "do-while" loop will be infinite if the user enters an invalid value. The "default" case should handle this somehow. A solution I like to use is to check the user input of "choice" and, if not an expected value, defaults to an expected value. Something like:

    Code:
    printf("\n Ener your choice->");
    	 scanf("%d",&choice);
    if((choice != 1)&&(choice != 2))
    	choice = 2;		// defaults to "exit" for invalid integer input
    There are, of course, many other ways of handling this, and my suggestion is probably not the best in this situation. Others more experienced here will probably have better input on this particular point.

    - Matt

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Note to members: I'm not very advanced in 'C' quite yet, though these forums are very helpful. I ordinarily wouldn't volunteer so much help when there are others much more capable around, but I'm a new member and was told I needed a certain number of posts before I could access my profile/etc. I could have achieved this with some quick remarks sprinkled about, but my programmer mentality and respect for this site makes me want to follow the proper protocol. Therefore, I've tried to make my posts relevant and as helpful as possible. I apologize for this quick aside, and thank everyone for their patience, assistance, and corrections.

    - Matt

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    As Matt said, your loop should handle some default case preferably by asking the user to re-input the choice in the default section of the switch statement. Secondly, there is no need to exit(0) from the second case. The switch will break, the choice will still be 2 and the loop will finish gracefully. Your way of doing it is sort of like breaking the car window when you could just open the door.

    Also, fix all the gets() by removing it and replacing it with some fgets/sscanf combination or whatever works out for you that doesn't lead to quick and dirty overflows.
    Last edited by claudiu; 06-19-2011 at 12:38 PM.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    I ordinarily wouldn't volunteer so much help when there are others much more capable around..
    Think in binary, either you are capable or you are not; when you have a good idea, it IS the former.
    Even if something turns out wrong, it would be a good idea to have that corrected.

    btw..
    Shouldn't

    Code:
    scanf("%d",year);
    be
    Code:
    scanf("%d",&year);
    ?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Matticus View Post
    Note to members: I'm not very advanced in 'C' quite yet, though these forums are very helpful. I ordinarily wouldn't volunteer so much help when there are others much more capable around, but I'm a new member and was told I needed a certain number of posts before I could access my profile/etc.
    It might be better to build your count by asking questions about your various exercises and projects than to offer bad or incorrect advice to other beginners...

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by CommonTater View Post
    It might be better to build your count by asking questions about your various exercises and projects than to offer bad or incorrect advice to other beginners...
    (1) I'm generally able to find answers myself, though I will definitely ask them here if I ever get stuck. I certainly don't want to get flamed for asking the sort of things that could be found through some searching.

    (2) Have I offered bad or incorrect advice?

    And I never said I was a beginner; I've been programming for years. I just haven't dove as deep into 'C' as many experts here have (you'll notice from my posts that I try to be humble). In fact, I'm taking that dive now. Thanks.

    - Matt

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Actually, the advice was very good -- the OP definitely needs to forget that (s)he ever knew of gets. Either all fgets() or all scanf() is usually the way to go, so that you don't have to worry about all the different ways \n can make your life miserable. Checking return values of scanf can be handy as well -- this way if the user types a character when a number is required, you don't get stuck spinning your wheels, or worse, move on with half-correct and half-incorrect data.

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Just as a short addition to what tabstop said, scanf() should also be used carefully as it can easily create the same problems as gets() when reading strings if a width is not specified.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Matticus View Post
    And I never said I was a beginner; I've been programming for years. I just haven't dove as deep into 'C' as many experts here have (you'll notice from my posts that I try to be humble). In fact, I'm taking that dive now. Thanks.
    Matt
    I'm doing the same thing with C++ ... OOP and I somehow just don't get along very well... But even in cases where I know the answer I will most often defer to the more advanced programmers in that forum. I know C, used Pascal for years... but in C++ I'm still at the asking questions level... So I let be others deal with it and learn from reading...

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by CommonTater View Post
    So I let be others deal with it and learn from reading...
    And there I shall most likely remain. As I said before, I tend to be more of a lurker on forums in general. But I only gave advice on things I understood, and for reasons I've already stated.

    I will say, though, researching my answers for accuracy before posting has helped my own understanding immensely. Anyone who is still on the learning curve will do well to remember: asking questions will get you answers - answering questions will give you understanding.

    Though in this case, answers do not necessarily have to be posted. Many times I crafted answers to questions for my own benefit, while taking CommonTater's excellent advice of letting the more advanced programmers actually post the answers.

    - Matt

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    o_o

    Well I don't really care how experienced people are, anyone can have the right answer. I think the winning strategy at this board is: Don't force yourself to contribute. I've seen people here start a thread, and then provide a detailed answer or two in other threads at the same time.

    If you don't know the answer then don't post. Don't let CommonTater tell you that you can't answer questions yet.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by whiteflags View Post
    Don't let CommonTater tell you that you can't answer questions yet.
    Ahem... that was not the intent and if that's how you read it you are mistaken.
    I was in fact saying something very close to what you said...

    My advice was simple: If you know the answer with reasonable confidence, go ahead but don't be posting stuff you're not sure of just to bolster your post count.

    And yes, I've asked questions while answering questions in other threads. I've never said I know everything... in fact I'm pretty sure I don't.

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by CommonTater View Post
    My advice was simple: If you know the answer with reasonable confidence, go ahead but don't be posting stuff you're not sure of just to bolster your post count.
    Not to beat a dead horse here, but how was this relevant to me? I only contributed when it was something I understood, and I received only positive feedback. Perhaps you mistook my cautious wording for uncertainty. This is a result of posting on an unrelated intellectual forum where users would shred you to pieces if you were not clear and thorough.

    Just wanted to clarify this. I'm not offended or upset with anyone, my feelings aren't hurt, and I feel really bad about derailing this thread so much, so I will drop it here.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Matticus View Post
    Not to beat a dead horse here, but how was this relevant to me?

    You did write...
    "I ordinarily wouldn't volunteer so much help when there are others much more capable around, but I'm a new member and was told I needed a certain number of posts before I could access my profile/etc."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wrong wrong with my xor function?
    By Anddos in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2009, 01:38 PM
  2. What am I doing wrong?
    By skaldicpoet9 in forum C Programming
    Replies: 11
    Last Post: 02-07-2008, 04:17 AM
  3. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  4. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  5. What is wrong here?????
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2002, 10:51 AM