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 1069053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:21:50+00:00 2026-05-16T20:21:50+00:00

I worked on this yesterday about 5 hours and got the code to work

  • 0

I worked on this yesterday about 5 hours and got the code to work using help from this site, but I think the way I did it was a cheater way, I used a scanf command. Anyways I want to fix this the correct way. Thanks guys! Oh the code compiles but the average that gets spit out is wrong. I would like to conceptually understand what I am doing wrong as well as finish the assignment.

#include <stdio.h> 
#include <stdlib.h>
double get_number(int 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(int num) { 
  double value = 0;
  int c;
  printf("Please input number %i: ", num);
  while ((c = getchar()) != '\n') { 
    if ( (c<'0') || (c>'9') ) { 
      printf("Incorrect character entered as a number - %i\n",c);
      exit(-1);
    }
    else {
      c =value;
    }
  }
  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-16T20:21:51+00:00Added an answer on May 16, 2026 at 8:21 pm

    Your get_number function always returns 0, because you never assign anything to the value variable. I’m guessing you want:

    value = value*10 + (c - '0');
    

    Instead of

    c = value;
    

    To clarify:

    You are reading a number digit by digit (with getchar you read a single character). So let’s say you read the digits 1 2 3:

    When you read 1, you do value = value*10 + ('1' - '0') meaning value = 0 + 1 = 1.

    When you read 2, you do the same and get value = 1*10 + ('2' - '0') meaning value = 12.

    Same for 3.

    What you need to understand is that multiplying something by 10 adds a 0 at the end of that something. Adding a digit to something that ends with a zero replaces that 0 by that digit.

    You must also understand that you are dealing with characters, which are actually integers. The character '0' is represented by an ASCII code of let’s say x. '1' is represented by x + 1 etc. If you were to print the value of '0'+'3' it would not be 3 as you’d expect. This is why we subtract '0' (which is x) from the characters, to get the actual digits.

    Hope that clears some things up.

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

Sidebar

Related Questions

I have worked quite a lot with javascript but yesterday, I started using node.js.
I started using Thrift about a week ago. I got the latest source from
This question is about using getter methods of a singleton object in worker threads.
Yesterday I posted this question regarding using lambdas inside of a Join() method to
I'm developing a service using .NET on Windows platforms. It had worked until yesterday...
hoping to get some help with this query, I've worked at it for a
Okay I tried asking this question yesterday but i'm not sure if I gave
Yesterday I installed feedparser (on OSX 10.5) and it worked fine, but now it
I built a web app where I'm using the Google API. Yesterday it worked
I have problem with my own Subversion repository. It worked yesterday. But I received

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.