Thread: Newbie college student :)

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    77

    Newbie college student :)

    Hi guys,

    I am learning C. And made a program and it's giving me Segmentation fault when I run it . Please I need help. I run it under munro unix. Can anyone tell me what am I doing wrong.

    Code:
    #include <stdio.h>
    
    #define A 100
    #define B 90
    #define C 80
    #define D 70
    #define E 60
    #define ZERO 0
    
    int main()
    
    {
    
    char ch;
    char firstname, lastname;
    char low=3;
    char high=12;
    int mark=0, n=0;
    
    int randNumber;
    
    srand(time(0));
    
    randNumber=rand() % 10 + 3;
    
    printf("The number of students in the class is %d.\n", randNumber);
    
    while (n != randNumber)
    {
    
    
    n++;
    printf ("Please enter your first name: \n");
    scanf ("%s", &firstname);
    fflush(stdin);
    printf ("Please enter your last name: \n");
    scanf ("%s", &lastname);
    fflush(stdin);
    printf ("Please enter your marks: \n");
    scanf ("%d", &mark);
    fflush(stdin);
    
    if (mark >= B || mark <= A)
    
    printf("%s %s %d Grade A\n", firstname, lastname, mark);
    
    
    else if (mark >= C || mark <= B)
    
    
    printf("%s %s %d Grade B\n", firstname, lastname, mark);
    
    
    else if (mark >= D || mark <= C)
    
    
    printf("%s %s %d Grade C\n", firstname, lastname, mark);
    
    
    else if (mark >= E)
    
    
    printf("%s %s %d Grade D\n", firstname, lastname, mark);
    
    
    else if (mark < E)
    
    
    printf("%s %s %d Grade F\n", firstname, lastname, mark);
    
    
    else
    
    printf("Marks are invalid./n");
    
    return 0;
    
    }
    }
    Thanks a lot guys...

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    68
    Try this you still need to modify a few things like taking the \n out of the string firstname and lastname and not use fflush to clear the stdin buffer

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define A 100
    #define B 90
    #define C 80
    #define D 70
    #define E 60
    #define ZERO 0
    
    int main()
    {
    
    	/*char ch;*/
    	char firstname[80], lastname[80];
    	/*char low=3;
    	char high=12;*/
    	int mark=0, n=0;
    
    	int randNumber;
    
    	srand(time(0));
    
    	randNumber=rand() % 10 + 3;
    
    	printf("The number of students in the class is %d.\n", randNumber);
    
    	while (n != randNumber)
    	{
    		n++;
    		printf ("Please enter your first name: \n");
    		fgets(firstname, sizeof firstname, stdin);
    		fflush(stdin);
    		printf ("Please enter your last name: \n");
    		fgets(lastname, sizeof lastname, stdin);
    		fflush(stdin);
    		printf ("Please enter your marks: \n");
    		scanf ("%d", &mark);
    		fflush(stdin);
    
    		if (mark >= B && mark <= A)
    			printf("%s %s %d Grade A\n", firstname, lastname, mark);
    		else if (mark >= C && mark <= B)
    			printf("%s %s %d Grade B\n", firstname, lastname, mark);
    		else if (mark >= D && mark <= C)
    			printf("%s %s %d Grade C\n", firstname, lastname, mark);
    		else if (mark >= E)
    			printf("%s %s %d Grade D\n", firstname, lastname, mark);
    		else if (mark < E)
    			printf("%s %s %d Grade F\n", firstname, lastname, mark);
    		else
    			printf("Marks are invalid./n");
    	}
    	
    	return 0;
    }
    [edit]
    Your logic was out too you use || instead of && its now changed
    [/edit]
    Last edited by gsoft; 01-22-2005 at 06:15 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    ok let me try that. Thanks

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    No it doesn't work . It giving me different errors now. When I compile it.

    Code:
    test5.c:1: syntax error before '<' token
    In file included from test5.c:2:
    /usr/include/stdlib.h:137: syntax error before "__ctype_get_mb_cur_max"
    In file included from /usr/include/sys/types.h:266,
                     from /usr/include/stdlib.h:416,
                     from test5.c:2:
    /usr/include/bits/pthreadtypes.h:48: syntax error before "size_t"
    /usr/include/bits/pthreadtypes.h:51: syntax error before "__stacksize"
    In file included from test5.c:2:
    /usr/include/stdlib.h:433: syntax error before "size_t"
    /usr/include/stdlib.h:462: syntax error before "size_t"
    /usr/include/stdlib.h:556: syntax error before "__size"
    /usr/include/stdlib.h:558: syntax error before "__nmemb"
    /usr/include/stdlib.h:567: syntax error before "size_t"
    In file included from /usr/include/stdlib.h:578,
                     from test5.c:2:
    /usr/include/alloca.h:33: syntax error before "__size"
    In file included from test5.c:2:
    /usr/include/stdlib.h:583: syntax error before "__size"
    /usr/include/stdlib.h:739: syntax error before "size_t"
    /usr/include/stdlib.h:743: syntax error before "size_t"
    /usr/include/stdlib.h:812: syntax error before "size_t"
    /usr/include/stdlib.h:815: syntax error before "size_t"
    /usr/include/stdlib.h:819: syntax error before "size_t"
    /usr/include/stdlib.h:822: syntax error before "size_t"
    /usr/include/stdlib.h:830: syntax error before "size_t"
    /usr/include/stdlib.h:834: syntax error before "size_t"
    /usr/include/stdlib.h:841: syntax error before "mbstowcs"
    /usr/include/stdlib.h:842: syntax error before "size_t"
    /usr/include/stdlib.h:844: syntax error before "wcstombs"
    /usr/include/stdlib.h:845: syntax error before "size_t"
    In file included from test5.c:3:
    /usr/include/time.h:197: syntax error before "strftime"
    /usr/include/time.h:197: syntax error before "size_t"
    test5.c: In function `main':
    test5.c:33: `stdin' undeclared (first use in this function)
    test5.c:33: (Each undeclared identifier is reported only once
    test5.c:33: for each function it appears in.)

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    oh sorry hold my fault I forgot to put #include at the top, Please wait let me run it again

    sorry

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    It's working now . Thanks...Just one question, fgets is better then scanf()?. Teacher was telling me to use scanf(). we are not allowed to use fgets () right now.

    thanks

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    Gsoft,

    I tried scanf() in place of fgets() and it's working fine.

    Thanks

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    68
    scanf allows you to go over the allocated buffer something I dont think you should do. Scanf() is good but I wouldnt use it for strings.

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    scanf() is not the function of choice for a many C programmers. It just opens a can of very stinky worms. If you must use it, remember that it returns a value, which corresponds to the amount of items correctly converted. This allows a small measure of error checking. For example:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        int age;
        char fname[10];
    
        printf("Enter your first name: ");
        fflush(stdout);
        if (scanf("%s", fname) != 1) {
            puts("scanf() error!");
            exit(EXIT_FAILURE);
        }
        printf("Enter your age: ");
        fflush(stdout);
        if (scanf("%d", &age) != 1) {
            puts("scanf() error!");
            exit(EXIT_FAILURE);
        }
        printf("Your first name is %s, and your age %d\n", fname, age);
        return 0;
    }
    ~/
    Last edited by kermit; 01-22-2005 at 06:51 PM.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    kermit,

    so if "fname" is not equal to "1" it will exit?. So what does the vaule "1" stand for?. Sorry I am new to this, so trying to understand the code that you wrote .

    Thanks

  11. #11
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >>So what does the vaule "1" stand for?
    scanf returns the number of items successfully converted. Because the call is only converting one item, a successful return would be 1.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    77
    Ahhh, ok.... Thanks

  13. #13
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by jat421
    kermit,

    so if "fname" is not equal to "1" it will exit?. So what does the vaule "1" stand for?. Sorry I am new to this, so trying to understand the code that you wrote .

    Thanks
    As Sake pointed out, the value 1 corresponds to the amount of arguments successfully converted by scanf().

    Many functions return a value, which are free to be used, or ignored. A good example of an ignored return value would be the one returned from the printf() function - printf() returns the amount of characters printed, yet you will probably not see that value used very often. In the case of scanf(), as has been mentioned, we can choose to ignore or not ignore its return value, and instead use it to see if there was any problem with conversion.

    Now consider how the return value from scanf() is obtained. When you call a function which returns a value, you can assign it to a variable like so:

    Code:
    int ret_val;  /* will hold whatever value scanf() returns */
    ret_val = scanf("%s", fname);
    To see if scanf() was successful, you would check the value stored in the variable ret_val against the number of arguments passed to scanf() (which in this case is 1):

    Code:
    int ret_val;
    ret_val = scanf("%s", fname);
    if(ret_val != 1) {
    /* do some stuff */
    }
    Of course, writing code in the last method is awkward at best. Not much sense in declaring an extra variable if its not necessary.

    Now then, if you had a call to scanf() that passed 2 arguments, you would check to see that the return value of scanf() equalled 2. Any number other than 2, indicates a problem. For example:

    Code:
    char fname[10];
    char lname[10];
    
        printf("Enter your first and last name: ");
        fflush(stdout);
        if (scanf("%s%s", fname, lname) != 2) {
            puts("scanf() error!");
            exit(EXIT_FAILURE);
        }
    Finally, in the examples I have given, if scanf() fails, for whatever reason, the program exits.

    ~/
    Last edited by kermit; 01-22-2005 at 08:13 PM.

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    char fname[10];
    char lname[10];
    
        printf("Enter your first and last name: ");
        fflush(stdout);
        if (scanf("%s%s", fname, lname) != 2) {
            puts("scanf() error!");
            exit(EXIT_FAILURE);
        }
    Never use scanf's %s directive without supplying width. Otherwise you've created something similar to gets.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You know its funny Dave, I was just thinking about people who know how to use scanf() better than me, and you came to mind. Of course using scanf() like that is a lovely way to over run the bounds of an array, and I should have mentioned it.
    Last edited by kermit; 01-22-2005 at 09:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updating in a sequential file?
    By Ronnyv1 in forum C Programming
    Replies: 1
    Last Post: 03-24-2009, 04:41 PM
  2. Database assignment is Killing me!
    By Boltrig in forum C Programming
    Replies: 2
    Last Post: 11-29-2007, 03:56 AM
  3. Newbie college student
    By jat421 in forum C Programming
    Replies: 3
    Last Post: 02-05-2005, 06:07 PM
  4. LinkList Sorting in C
    By simly01 in forum C Programming
    Replies: 3
    Last Post: 11-25-2002, 01:21 PM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM