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

  • SEARCH
  • Home
  • 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 1098531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:35:33+00:00 2026-05-17T00:35:33+00:00

My assignment is to fix the code. I have my edited code below and

  • 0

My assignment is to fix the code. I have my edited code below and the original code below that. I figure I still have a few errors in here. My error checking doesnt seem to work, and I am not sure if my getchar() function is written or working properly.

Please assume I know nothing becasue that is fairly accurate.

The code compiles, but the answer is always 2. I am about 4 hours into this piece of code with 3 more to work after this.

My code

#include <stdio.h> 

double get_number(double num);

main () {
  double n1,n2,n3;
  double average;

  printf("\nCompute the average of 3 integers\n");
  printf("--------------------------------\n");
  n1 = get_number(1);
  n2 = get_number(2);
  n3 = get_number(3);
  average = (n1 + n2 + n3)/3;
  printf("The average is %0.2f\n",average);
}

double get_number(double num) { 
  double value = 0;
  char c;
  int i;
  printf("Please input number %d: ", num);

  while (c = getchar != '\n') { 
    if ( (c>9) || (c<0) ) { 
      printf("Incorrect character entered as a number - %c\n",c);
      return(0);
    }
    else {
    value = num;
    }
  }
  return(value);
}

Original code

#include <stdio.h> 

main () {
  double n1,n2,n3;
  double average;

  printf("\nCompute the average of 3 integers\n");
  printf("--------------------------------\n");
  n1 = get_number(1);
  n2 = get_number(2);
  n3 = get_number(3);
  average = (n1 + n2 + n3)/3;
  printf("The average is %0.2f\n",average);
}

double get_number(int num) { 
  double value = 0;
  char c;
  printf("Please input number %d: ", num);
  while (c = getchar() != '\n') { 
    if ( (c<=9) && (c>=0) ) { 
      printf("Incorrect character entered as a number - %c\n",c);
      exit(-1);
    }
    else {
      value = 10*value + c - '0';
    }
  }
  return(value);
}
  • 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-05-17T00:35:33+00:00Added an answer on May 17, 2026 at 12:35 am

    A few issues:

    1. You should be using ‘9’ and ‘0’, since you want the ASCII values for digit ‘9’ (0x39) and ‘0’ (0x30), not 0x9 and 0x0.

    if ( (c>'9') || (c<'0') ) { 
    

    2. != has higher precedence than =, so you need parens. Learn operator precedence, and if you’re in doubt, use parens:

    3. getchar is a function not a variable.

    while ((c = getchar()) != '\n') { 
    

    4.  You use the wrong conversion. num is a double, so you would need %f. Or, you could make num a int.

    printf("Please input number %f: ", num);
    

    5. You never actually use c in any way (except error checking). You always return 0 or num (see your else clause), which makes no sense. The else body of the original is correct.

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

Sidebar

Related Questions

I have this assignment due that requires the usage of FLTK. The code is
This is my assignment: Here are my questions: How can I fix this error:
I have added this fix https://gist.github.com/2382288 for protect all fields against mass assignment in
My assignment requires that I have an ER diagram that shows the dependencies between
I have an asp.net page with c# code-behind. I have an event that is
I get the error (Lvalue required as left operand of assignment) for this code:
For my homework assignment, I have a network of Nodes that are passing messages
I have code that runs on different platforms that seems to get different results.
I have an app that creates Python code and runs it. During this process
I have some embedded code that writes commands to a memory address to control

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.