Thread: Newbie needs a little help!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    8

    Newbie needs a little help!

    Hey guys, so I started to learn a little C from numerous different sources and Iv'e hit a little "bump". When following a simple tutorial of how to make a average calculator with 5 inputs I decided I wanted to make it a little more advanced, and have a unlimited amount of inputs. I know what I have done wrong(As pointed out in "comments" in the code), but not sure how to correct it. Could you guys have a little look and in very basic terms tell me how to fix it, thanks.

    Code:
     #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {
             int loopcount;
             int numbers;
             int total;
             int average;
             int numtoav;
             /* ^^ Number's to average */
             int mainvar;
             total=0;
             loopcount=0;
             
             {
                    printf("How many numbers do you want to average?");
                    scanf ("%d, &numtoav");     
                         
                         }
             
             while(loopcount< &numtoav){
             /* Problem's persisst here ^^ */   
              printf ("Enter the numbers you want to average");
              scanf("%d", &numbers);
              total = total + numbers;
              loopcount=loopcount+1;  
                              
             }         
             average = total/&numtoav; 
             /*Aswell as here ^^ */
    printf("The average of your numbers is: %d", average);
                           
             getch ();
             }
    Also any tips on bad coding etiquette would be also appreciated.

  2. #2
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79
    scanf takes a reference to the integer numtoav. But otherwise you should use it as an ordinary integer.
    Code:
    while(loopcount < numtoav) {
    ...and...
    average = total / numtoav;

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    8

    Thanks

    Thank you, it runs now, but it still doesn't work fully, CMD looks something like this (Area between *'s is user input)
    How many numbers do you want to average?*5*
    Enter the numbers you want to average*2 6 7 9*
    Enter the numbers you want to averageEnter the numbers you want to averageEnter
    the numbers you want to averageEnter the numbers you want to averageEnter the
    numbers you want to average

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    I don't understand this:
    Enter the numbers you want to average*2 6 7 9*
    Are you attempting to enter 2 6 7 9 (four numbers with spaces) on one line of user input before a return?

    The program wants each number entered on a separate line with a return.

    Anyway, the code works for me after I get rid of all the compile warnings/errors. How about posting the current, compilable (with no warnings) code?
    Last edited by mike65535; 05-02-2011 at 07:23 AM.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Also any tips on bad coding etiquette would be also appreciated.
    Get your indentation and whitespace under control. Consistency makes it SO much easier to read and spot errors.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    8
    Quote Originally Posted by mike65535 View Post
    I don't understand this:

    Are you attempting to enter 2 6 7 9 (four numbers with spaces) on one line of user input before a return?

    The program wants each number entered on a separate line with a return.

    Anyway, the code works for me after I get rid of all the compile warnings/errors. How about posting the current, compilable (with no warnings) code?
    Well I'm glad it works for you, its pretty odd I must admit. But like I said, I'm a newbie and to be honest I'm mostly figuring this out as I go, I use DEV C++ to write and compile and don't know what you mean about posting the compilable. From what I see there are no compiling errors or anything like that. And yes, I was entering the inputs on one line, if I try enter each input on different lines the program just keeps going back to "Enter the numbers you want to average"

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jubjubj View Post
    yes, I was entering the inputs on one line, if I try enter each input on different lines the program just keeps going back to "Enter the numbers you want to average"
    Of course:

    Code:
              while(loopcount< &numtoav){
                       printf ("Enter the numbers you want to average");
                       scanf("%d", &numbers);
                       total = total + numbers;
                       loopcount=loopcount+1;  
             }
    When you press enter, everything you typed gets added to the stdin buffer. That scanf() only reads one number at a time, from that buffer. So if you enter all the numbers on one line, the while() loop reads one from the stdin buffer, then it prints "Enter..." again, then it reads another number (from the numbers still in the stdin buffer), until the loop has completed, or the buffer is empty, in which case it has to wait for you to enter more.

    You might get a better idea of this if you say you want 10 numbers, then enter them 2 at a time. "Enter..." will print out twice each time. If you feed it three at a time, "Enter..." will print out three times.

    Code:
    Enter: 1 2
    Enter: Enter: 2 3 4
    Enter: Enter: Enter: 5 6 7 8
    Enter: Enter: Enter: Enter: 9
    Probably you should print "Enter..." before the loop, not in it.
    Last edited by MK27; 05-02-2011 at 08:02 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    jubjubj... are you working with any kind of tutorials or textbooks?

    Also DEV C++ has been abandoned for several years now... You may want to update to something newer...
    something like Pelles C for plain C... Code::Blocks with MinGW for C/C++...

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Try the latest edition of "The C Programming Language" by Brian W. Kernighan and Dennis M. Ritchie.

    C for Dummies 2nd edition
    NO!

    Teach Yourself C in 21 Days -- Table of Contents
    Better than "C for Dummies", but still, no.

    Also, as a matter of interest, I highly doubt that that site has Pearson's (the latest publishers) blessing.

    Soma

  10. #10
    Registered User
    Join Date
    May 2011
    Posts
    8
    NO!
    FML already started reading it. :S

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jubjubj View Post
    FML already started reading it. :S
    I have never looked at "C for Dummies", but I did have "Programming for Dummies" out from the library for a long time. I thought it was a pretty decent reference, actually -- in fact, I've never seen a more succinct and useful description of sort algorithms (bucket, bubble, merge, quick, heap, etc.) anywhere. There was virtually no long winded tripe and great use of pseudo code and flow diagrams. Way better than 80% of online tutorials/blogs.

    A lot of people seem to enjoy learning with the K&R book phantomotap recommends tho. People will show up here with the exercises, etc, and they (K&R) are the dudes who wrote the language in the first place, so they probably have some insight.
    Last edited by MK27; 05-03-2011 at 12:34 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User
    Join Date
    May 2011
    Posts
    8
    Quote Originally Posted by MK27 View Post
    I have never looked at "C for Dummies", but I did have "Programming for Dummies" out from the library for a long time. I thought it was a pretty decent reference, actually -- in fact, I've never seen a more succinct and useful description of sort algorithms (bucket, bubble, merge, quick, heap, etc.) anywhere. There was virtually no long winded tripe and great use of pseudo code and flow diagrams. Way better than 80% of online tutorials/blogs.

    A lot of people seem to enjoy learning with the K&R book phantomotap recommends tho. People will show up here with the exercises, etc, and they (K&R) are the dudes who wrote the language in the first place, so they probably have some insight.
    Thanks for the reassurance! I might just read both, because I'm really enjoying C for dummies, its humorous attitude keep's me engaged, as I'm not a huge fan of books. But I'll be sure to read the K&R one too since it is
    "regarded by many to be the authoritative reference on C"
    - Wikipedia.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jubjubj View Post
    Thanks for the reassurance! I might just read both, because I'm really enjoying C for dummies, its humorous attitude keep's me engaged, as I'm not a huge fan of books. But I'll be sure to read the K&R one too since it is - Wikipedia.
    But don't just read... run up the examples in your compiler and play with them as you go. Once you understand the code, move on to the next page. Reading alone does not really work with programming languages, learning is doing so if you follow the book you will learn in a reasonable order and hopefully be ready to write a few smaller things on your own when you finish.
    Last edited by CommonTater; 05-03-2011 at 02:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  2. Plz help this newbie
    By Desperate in forum C++ Programming
    Replies: 6
    Last Post: 07-10-2006, 10:26 PM
  3. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 12:27 PM
  4. Rpg Newbie
    By Paninaro in forum Game Programming
    Replies: 4
    Last Post: 06-22-2002, 07:30 PM
  5. sdl-newbie
    By Unregistered in forum Game Programming
    Replies: 8
    Last Post: 05-22-2002, 11:08 AM

Tags for this Thread