Thread: Newbie needs a little help!

  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
    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++...

  8. #8
    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

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    8
    Okay, so I have fixed that, and guess what? Another issue! Here is the updated code:
    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");             
                         }
             printf ("Enter the numbers you want to average");
             while(loopcount< numtoav){  
              scanf("%d", &numbers);
              total = total + numbers;
              loopcount=loopcount+1;                
             }         
             average = total/numtoav; 
    printf("The average of your numbers is: %d", average);               
             getch ();
             }
    Basically now it doesn't work, I can enter the numbers but get no output. It just goes down a line each time I hit enter. I'm probably doing/not doing something very obvious and basic, any idea's?

    You may want to update to something newer...
    something like Pelles C for plain C... Code::Blocks with MinGW for C/C++...
    Thank you I will upgrade once I finish this! And I am using a combination of a book called "Practical C Programming, 3rd Edition (1997)" (IKR I must love old things) and A tutorial series by a youtube user called the new Boston
    YouTube - thenewboston&#39;s Channel
    My program started life when I followed this tutorial. > YouTube - C Programming Tutorial - 6 - While Loops

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jubjubj View Post
    Basically now it doesn't work,
    You need to enable warnings for your compiler.

    There's a couple of things wrong here:

    Code:
    scanf ("%d, numtoav");
    Namely, the way the whole thing is quoted, and that you didn't use &, the "address of" operator. If you had your warnings enabled, the compiler would have clued you in.

    Also: you should return 0 at the end of main.
    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

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jubjubj View Post
    Thank you I will upgrade once I finish this! And I am using a combination of a book called "Practical C Programming, 3rd Edition (1997)" (IKR I must love old things) and A tutorial series by a youtube user called the new Boston
    YouTube - thenewboston&#39;s Channel
    My program started life when I followed this tutorial. > YouTube - C Programming Tutorial - 6 - While Loops
    The others are giving you good help with your code so I'll just add one more comment about tutorials and such...

    1) DO NOT mix C and C++, these are separate languages and the result can be very confusing. What you know from C does carry forward to C++ but the reverse is not true... a LOT of C++ code will return a chain of syntax errors in a C compiler.

    2) I'm not sure that watching videos is a good way to learn anything. What you need to do is get an *up to date* C tutorial or manual --certainly something newer than 14 years ago-- and go through it page by page, exercise by exercise. Most of these documents teach in a progressive manner making sure each step prepares you for the next. Videos may get the information in front of your eyes (for a fleeting few seconds) but actually doing the work will get it between your ears where it will live, quite comfortably, for the rest of your life. The problem being that moving pictures do not trigger long term retention of information (which is probably why some people needed to see Star Wars 2,927 times )

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Quote Originally Posted by MK27 View Post
    You need to enable warnings for your compiler.
    I hinted at this but the OP didn't take the bait.

  13. #13
    Registered User
    Join Date
    May 2011
    Posts
    8
    Quote Originally Posted by CommonTater View Post
    The others are giving you good help with your code so I'll just add one more comment about tutorials and such...

    1) DO NOT mix C and C++, these are separate languages and the result can be very confusing. What you know from C does carry forward to C++ but the reverse is not true... a LOT of C++ code will return a chain of syntax errors in a C compiler.

    2) I'm not sure that watching videos is a good way to learn anything. What you need to do is get an *up to date* C tutorial or manual --certainly something newer than 14 years ago-- and go through it page by page, exercise by exercise. Most of these documents teach in a progressive manner making sure each step prepares you for the next. Videos may get the information in front of your eyes (for a fleeting few seconds) but actually doing the work will get it between your ears where it will live, quite comfortably, for the rest of your life. The problem being that moving pictures do not trigger long term retention of information (which is probably why some people needed to see Star Wars 2,927 times )
    I think your right, I rushed into this, I told myself I would stick to books, but that changed quickly. I will go back to reading an up-todate book. And maybe re-visit this later on. I'm still proud of what I achieved in 24 hours, but there is deferentially holes in the way I work that need to be fixed.
    Thanks guys!

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jubjubj View Post
    I think your right, I rushed into this, I told myself I would stick to books, but that changed quickly. I will go back to reading an up-todate book. And maybe re-visit this later on. I'm still proud of what I achieved in 24 hours, but there is deferentially holes in the way I work that need to be fixed.
    Thanks guys!
    Patience is it's own reward.
    Impatience is it's own punishment.

    Figure a month to learn the basics, 3 months before you can write anything useful, 6 months to a year to get good at it.
    Last edited by CommonTater; 05-02-2011 at 10:00 AM.

  15. #15
    Registered User
    Join Date
    May 2011
    Posts
    8
    On this note, are there any book you would recommend of that you used? I was thinking "C for Dummies 2nd edition" ?

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