Beginner help please

This is a discussion on Beginner help please within the C Programming forums, part of the General Programming Boards category; The aim of the code is to calculate the percentage of the student mark until the student enters N (when ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    Beginner help please

    The aim of the code is to calculate the percentage of the student mark until the student enters N (when asked if there are anymore marks to calculate).



    #include <stdio.h>
    #include <string.h>
    main()
    {
    int mark, pmark;
    char reply;
    reply = 'Y';
    while (reply != 'N')
    {
    printf("What is your mark out of ten? \n");
    scanf("%d", &mark);

    pmark = mark * 10;

    printf("Your percentage mark is %d\n", pmark);

    printf("Any more marks to calculate? \n");
    scanf("%c", &reply);
    }
    system ("pause");
    return 1;
    }


    but instead it just loops and keeps asking for the student's mark.
    Anyone able to help?
    Thanks silas.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Hello there, just a few pointers:
    Use int main instead of main
    return 0 , not 1 at the end
    put your code in code quotes and ident it properly

    About your question:
    C is a case-sensitive language. Are you sure that you type "N" and not "n"? Are you sure that you use English when typing?
    I can't detect any problem in your code, that's why i'm asking you the above.
    Devoted my life to programming...

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    The problem is your usage of scanf(). When you're reading in the mark, it reads in the integer and then leaves the newline character on the input buffer. Then when you read in the reply it reads in the newline as your response instead of the character you typed.

    You can correct the problem by just adding 'getchar();' before your scanf() call to read in the reply. Almost every beginner runs into scanf() issues and I don't know why they continue to teach it at the beginner level.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Is that it? I thought scanf ignored the newline, as it ignores space, tab etc.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    Quote Originally Posted by itsme86 View Post
    The problem is your usage of scanf(). When you're reading in the mark, it reads in the integer and then leaves the newline character on the input buffer. Then when you read in the reply it reads in the newline as your response instead of the character you typed.

    You can correct the problem by just adding 'getchar();' before your scanf() call to read in the reply. Almost every beginner runs into scanf() issues and I don't know why they continue to teach it at the beginner level.
    Thank you very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ADO.NET in VS2008 or gdi+ (beginner question)
    By robgar in forum C# Programming
    Replies: 3
    Last Post: 10-04-2009, 03:40 AM
  2. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 12:48 PM
  3. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 03:02 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21