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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:54:22+00:00 2026-06-17T20:54:22+00:00

First time working with C and I was asked to do a simple average

  • 0

First time working with C and I was asked to do a simple average function WITHOUT scanf (only using getchar) for my Systems class. I ended up writing an unnecessarily complicated loop just to get my code to compile and even after compiling+running it doesn’t seem to do anything after taking in the keyboard input.

#include <stdio.h>
#include <stdlib.h>

//average program

//use getchar to get numbers separately instead of scanf and integers.
//Not sure why. Most likely to build character.

int main (int argc, const char * argv[])
{
    char x,z;
    float avg;
    int tot,n,b;

    printf("Input Integer values from 1 to 100. Separate each value by a space. For example: 23 100 99 1 76\n");

     ret:

    x=getchar();
    while( x != '\n' );
    {
        if(x==isspace(x))
        { goto ret;}


     opd:

    z=getchar();
        if ((z == isspace(z)))
        {
            b = x - '0';//subtracting 0 from any char digit returns integer value
            tot +=b;
            n++;
            goto ret;
        }

        else if(z == '\n')
        {
            b = x - '0';
            tot +=b;
            n++;
            goto end;
        }

        else
        {
            x = x*10;
            x = x + z;
            goto opd;
        }
    }

    end:
    avg=tot/n;

    printf("Taking of the average of the values. The average is %1.2f\n",avg);

    return avg;

}
  • 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-17T20:54:24+00:00Added an answer on June 17, 2026 at 8:54 pm
    1. The semicolon in while(...); causes an infinite loop, it’s the same as saying: while(...) continue;
    2. You should use only one loop, and you should try to use only one call to getchar() … it’s too confusing with multiple getchar() calls and your code is trying to throw away the first line the way it’s written.
    3. Definitely get rid of the goto statement, your instructor won’t like them and they are quite unnecessary. (Do read up on break and continue.)
    4. In C parsers calling getchar() directly, it’s useful to be able push a character back on the input stream. See man 3 ungetc or just write a simple wrapper around getchar(). You should only need one character of pushback at the end of a parser loop.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First time question asked long time lurker. I'm working on a Silverlight app with
Long time reader, first time asker. Anyway, Here's the code I'm working with: class
First time working with JSON in Rails and trying to figure out something which
This is my first time working with a WPF datagrid. From what I understand
This is my first time working with file i/o in java, and it's not
I am having an problem. First time working with a windows server, do you
We're building a very content rich site in Drupal. It's my first time working
Currently working for the first time with JSON and with little experience of jQuery.
I'm working in Python for the first time and I've used Mechanize to search
i am working inapp-purchase project and first time for creating inapp-purchase in new app

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.