Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Problem in C, please help

  1. Jan 29, 2008 #1
    Problem in C, please help :)

    Hello all, So I'm learning C nowadays and I got stuck at this rather simple program. I have no idea what the problem in the program is so I hope someone can help.

    The program is for reading the number of students in a class, likewise reading their names and scores in sci, math and english and then calculating the average and displaying the names and avg.


    #include<stdio.h>
    #include<conio.h>

    main()
    {
    char name[25];
    float Sci, Math, Eng, average;
    int n,i;
    printf("Enter the number of students\n");
    scanf("%d",&n);

    for(i=0; i<n; i++)
    {
    puts("\nEnter name");
    gets(name);
    printf("\nScience=");
    scanf("%f",&Sci);
    printf("\nMath= ");
    scanf("%f",&Math);
    printf("\nEnglish= ");
    scanf("%f",&Eng);
    average=(Sci+Math+Eng)/3;
    puts("\n\nName:");
    puts(name);
    printf("\nAverage Score= %f", average);
    }
    getch();
    }

    Now the problem is-
    When i run the program, it first asks me to enter the no of students, after that it prints 'enter name' and 'science=' at the same time instead of waiting for me to enter the student's name in response to the 'enter name'.

    So I only have the option of entering the scores and finally it displays 'Name:' and a blank line followed by the average.


    Another program with a for loop only for gets and puts works fine for a specified number of students works(I removed the score and average part), the problem comes in when i put the 'enter the number of students' statement and make the program for 'n' students instead of using a number there.

    I'm using Dev C++ for programming, please help me out if anyone knows what the problem is. Okay I tested it on Turbo C++ IDE and it still shows the same problem.
     
    Last edited: Jan 29, 2008
  2. jcsd
  3. Jan 29, 2008 #2
    Hi...ı have changed ur gets statemant..please check ıt..ıt works now well...

    Please chck code below..ı have modıfıed ıt..
     
  4. Jan 29, 2008 #3
    Yeah, the above post got it.

    puts, and gets are intrinsic functions, and unless you put a mechanism to pause the compiler will code in the next lines of instruction for the machine to operate.

    scanf, which normally interprets from the terminal, will automatically pause for input. It's part of its defined function.

    There's a funct. that pauses, but I can't recall it right now. And I don't have my reference handy.

    And the hacks are a lurchin' and I can't even pull off a decent search. Dreaded dead-end beatniks at the beck and call, but that's my prob. I guess.

    gets, though, is really designed to pull from another data stream than the std kbd input. Like a data file. Where it really shines, in its concise handling, and speed thereof.
     
  5. Jan 30, 2008 #4
    @brk235,
    Shouldn't it be scanf("%s",name); without the ampersand

    Anyway, this wouldn't allow me to enter a proper full name like 'John Smith'. When I executed the code, it let me enter the name but not the scores in the first loop :-/

    The output looked like this:

    Enter the number of students:
    3

    Enter Name
    John Smith

    Science=
    Math=
    English=

    Name:
    John

    Average=some garbage value

    Enter name
    (doesn't let me type the string)
    Science=
    20
    Math=
    20
    English=
    20[/U]

    Name:
    Smith (from the string that I entered the first time)

    Average =20.000
    Enter Name
    ___

    Thanks for helping..
     
  6. Jan 30, 2008 #5
    @delta_moment

    Is system("pause"); the command you're talking about? Hmm it doesn't work. The gets(name); isn't being executed by the compiler. It just skips that statement:/

    But a simple program like the one below works just fine.
    main()
    {
    char name[25]
    int i;
    for(i=0;i<3,i++)
    {
    puts("Enter name");
    gets(name);
    puts("Name:");
    puts(name);
    }
    getch();
    }

    Thanks for helping..
     
  7. Jan 30, 2008 #6
    Could be your compiler, or configuration, or platform.

    You may have to point the gets statement at the std. input. I don't know.

    There's so many variables these days.

    From looking at the code in the latter post, the problem appears when you combine scanf and gets. I'd just say you're throwing the compiler.
    It's easy to do, I do it all the time. Just remember, that when a C program is compiled, it's converted to machine language, and the memory/drive/CPU access is all dynamic. Among many other data transfers. And, it's easy to trip out the computer, or yourself.

    Some compilers will catch things for you and make assumptions, others will not.
    I'm not sure, but since the combination seems to be what's throwing things, for now, I'd just avoid it, and stick to one or the other.
     
  8. Feb 15, 2008 #7
    This works:

    Code (Text):
    #include<stdio.h>

    main()
    {
            char name[25];
            float Sci, Math, Eng, average;
            int n, i;
            printf("Enter the number of students:  ");
            scanf("%d",&n);

            for(i=0; i < n; i++)
            {
                    printf("\nEnter name: ");
                    scanf("%s", name);

                    printf("\nScience: ");
                    scanf("%f", &Sci);

                    printf("\nMath: ");
                    scanf("%f", &Math);

                    printf("\nEnglish: ");
                    scanf("%f", &Eng);

                    average=(Sci+Math+Eng)/3;
                    puts("\n\nName:");
                    puts(name);
                    printf("\nAverage Score = %f\n", average);
            }
    }
    Whoever said you don't need to use an ampersand when using scanf with an array is correct, as 'number' is already a pointer to number[0]. Ampersands says the address of n, which gives scanf the address of n. Scanf is a bad function to use to, as it's possible to overflow buffers. Try using sscanf as you can specify input length, format etc.

    http://www.cplusplus.com/reference/clibrary/cstdio/sscanf.html

    Code (Text):
    Enter the number of students:  12

    Enter name: Karl

    Science: 13.1

    Math: 11.1

    English: 2.2


    Name:
    Karl

    Average Score = 8.800000
     
    The next thing to learn, would be to use a struct to do the same thing.
     
    Last edited: Feb 15, 2008
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Problem in C, please help
  1. Please help in c++ (Replies: 6)

  2. Help with C++ problem (Replies: 7)

Loading...