Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8645125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:28:07+00:00 2026-06-12T12:28:07+00:00

I am writing a super simple command line based program in C. It’s just

  • 0

I am writing a super simple command line based program in C. It’s just a small test and the code is very simple. So what it is meant to do is to ask the user for their name, maths grade, english grade, computing grade. Then it figures out their average grade and also tells them the name they entered. Yes I know this is an extremely simple program, but I’m still doing something wrong.

The problem is, one part of my code will run first telling the user to enter their name and then once they do this and press enter the rest of my code will run all at once and then stop working. It’s weird I just don’t understand what is wrong.

#include <stdio.h>

int main(int argc, const char * argv[])
{
    char chr;
    char firstname;
    int mathsmark, englishmark, computingmark, averagemark;

    printf("What is your name?\n");
    scanf("%c", &firstname);
    printf("\n");

    printf("What is your maths mark?\n");
    scanf("%d", &mathsmark);
    printf("\n");

    printf("What is your english mark?\n");
    scanf("%d", &englishmark);
    printf("\n");

    printf("What is your computing mark?\n");
    scanf("%d", &computingmark);
    printf("\n");

    printf("Your name is: %c", firstname);
    printf("\n");

    averagemark = (mathsmark + englishmark + computingmark) / 3;
    printf("%d", averagemark);
    printf("\n");

    chr = '\0';

    while (chr != '\n') {

        chr = getchar ();
    }

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:28:09+00:00Added an answer on June 12, 2026 at 12:28 pm

    One major problem is that you’ve declared firstname to be a single character long, and when you try to read the name from the console, you’re using the %c conversion specifier, which reads the next single character from the input stream and stores it to firstname. The remainder of the name is left in the input stream to foul up the remaining scanf calls.

    For example, if you type “Jacob” as a first name, then the first scanf call assigns J to firstname, leaving "acob\n" in the input stream.

    The next scanf call attempts to convert "acob\n" to an integer value and save it to mathsmark, which fails ("acob\n" is not a valid integer string). Same thing happens for the next two scanf calls.

    The last loop

    while (chr != '\n')
    {
       chr = getchar();
    }
    

    finally consumes the rest of "acob\n", which contains the newline character (because you hit Enter after typing the name), causing the loop and program to exit.

    How do you fix this?

    First, you need to declare firstname as an array of char:

    char firstname[SOME_SIZE] = {0};
    

    where SOME_SIZE is large enough to handle all your cases. The you need to change scanf call to

    scanf("%s", firstname);
    

    This tells scanf to read characters from the input stream up to the next whitespace character and store the results to the firstname array. Note that you don’t need to use the & operator here; under most circumstances, an expression of array type will be converted (“decay”) to an expression of pointer type, and the value of the expression will be the address of the first element in the array.

    Note that scanf is not very safe, and it’s not very robust. If you enter more characters than your buffer is sized to hold, scanf will happily store those extra characters to memory following the array, potentially clobbering something important. You can guard against this by using an explicit field width in the conversion specifier, like

    scanf(*%29s", firstname);
    

    but in general it’s a pain.

    scanf is also not very good at detecting bad input. If you enter “12er” as one of your marks, scanf will convert and assign the "12", leaving the "er" in the stream to foul up the next read.

    scanf returns the number of successful assignments, so one way to guard against bad input is to check the return value, like so:

    if (scanf("%d", &mathmarks) != 1)
    {
      printf("Bad input detected for math marks\n");
    }
    

    Unfortunately, scanf won’t remove bad characters from the stream; you’ll have to do that yourself using getchar or similar.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing a test app to emulate PIO lines, I have a very simple Python/Tk
I am writing a simple code, as below,,,, - (void)viewDidLoad { [super viewDidLoad]; restaurant_name
I'm writing a super small interpreter in vb.net because i need to execute a
I started writing a super-simple text adventure game a few months ago, and I
I'm writing simple APN toggle app. I wanted to ask how to force android
I am writing a small program in C++ that receives mic input and does
I was writing a simple AIDL based android remote service & a client to
I am writing aplha composite test app based on this example /* Create an
I'm writing a simple Game of Life program in Java and am having a
I am writing a very simple Android app constituted by an activity and a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.