A Simple Question,Answer would be Highly Appreciated.

This is a discussion on A Simple Question,Answer would be Highly Appreciated. within the C Programming forums, part of the General Programming Boards category; Hi,I'm a beginner with C. And a Bit confused with the Following Snippet,can you guys help? Code Snippet The Following ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    18

    A Simple Question,Answer would be Highly Appreciated.

    Hi,I'm a beginner with C.
    And a Bit confused with the Following Snippet,can you guys help?

    Code Snippet
    The Following Function Excepts 25 Numbers from the Keyboard & Prints the sum of the +ve Number's
    #include <stdio.h>
    main()
    {int i,sum,num;
    for(i=0;i<25;i=i+1)
    {
    puts("Enter a Number");
    scanf("%d,& num");
    fflush(stdin);

    if(num <=0) /* non positive numbers*/
    continue;
    sum=sum+num
    }
    printf("The sum of positive numbers entered is %d\n",sum);
    }
    Questions
    1.can you Explain "sum=sum+num"
    2.Can you give me a clear idea of things? and explain to me the function in detail,please!

    Thanks for the Help,

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, can you point out which parts of the code you actually DO understand - it's absolutely no point in me going through and spending my time explaining what you already know.

    Also, using fflush(stdin) is undefined - it does what this program expects in some C libraries [usually the C library is bundled with the compiler]. Another C library would be perfectly within it's rights causing your application to crash - because fflush is only defined for output files. stdin is an input file, so it may not have the components in place that fflush needs to do it's job, and if there's no specific checking for that, it may cause the application to crash. And like driving through a red light at a junction, you'd not have any way to say "it wasn't my fault".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    .can you Explain "sum=sum+num"
    yes user takes garbage, adds positive value and gets alot more garbage
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    18
    Quote Originally Posted by matsp View Post
    So, can you point out which parts of the code you actually DO understand - it's absolutely no point in me going through and spending my time explaining what you already know.

    Also, using fflush(stdin) is undefined - it does what this program expects in some C libraries [usually the C library is bundled with the compiler]. Another C library would be perfectly within it's rights causing your application to crash - because fflush is only defined for output files. stdin is an input file, so it may not have the components in place that fflush needs to do it's job, and if there's no specific checking for that, it may cause the application to crash. And like driving through a red light at a junction, you'd not have any way to say "it wasn't my fault".

    --
    Mats
    Actually I didn't write the code,I actually copied It from my Text book on to the Forum so someone could actually explain to me in a little more Detail.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    18
    Quote Originally Posted by vart View Post
    yes user takes garbage, adds positive value and gets alot more garbage
    cant quite get what your saying!
    Is "sum" a function??

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by parastar View Post
    cant quite get what your saying!
    Is "sum" a function??
    Of course not. Surely you recognize a variable declaration when you see one? "int i, sum, num"?

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by parastar View Post
    cant quite get what your saying!
    Is "sum" a function??
    Let me guess.
    You have a deadline for supplying the project on the course you have missed totally.
    So you asked your friend who at least has attended half of the course, and he gave you his project, but forgot to mention that it does not work.

    So now you are trying to figure out what this piece of ... unbeleivable work does to pretend it is done by you?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    213
    Quote Originally Posted by parastar View Post
    Actually I didn't write the code,I actually copied It from my Text book on to the Forum so someone could actually explain to me in a little more Detail.
    Did you try reading the book? Or speaking with your teacher? Two best first tries.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Replace fflush(stdin), with getchar(), to pull the newline left behind by scanf(), off the keyboard buffer.

    main() should be int main(), and have a return 0; at the end of it.

    another gechar() just before the last return, will "hold" the console window open so you can see the output of the program, before the window disappears.

    sum is summing up the numbers, if they are positive (hopefully). If you indent your code, you'll see your errors, a lot easier. You need to remove the space between the & and the word num, in your scanf() line of code.

    Also, you need to move the closing double quotes in scanf("%d", &num);, is what you need.
    Last edited by Adak; 01-29-2009 at 08:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  2. what SHOULD be a painfully simple API call...
    By Citizen Bleys in forum Windows Programming
    Replies: 3
    Last Post: 09-17-2003, 03:20 PM
  3. Simple question. Help will be appreciated.
    By wickedclownz in forum C++ Programming
    Replies: 2
    Last Post: 06-19-2003, 02:18 AM
  4. Need help with simple data types
    By partnole in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2001, 08:36 AM
  5. Linker errors with simple static library
    By Dang in forum Windows Programming
    Replies: 5
    Last Post: 09-08-2001, 09:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21