stuck with this question

This is a discussion on stuck with this question within the C Programming forums, part of the General Programming Boards category; hi guyz i hv problem with my codes of the following question: Write a C program to receive a series ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    6

    Angry stuck with this question

    hi guyz

    i hv problem with my codes of the following question:

    Write a C program to receive a series of integers from the operator. Calculates the product of those integers and print the total to the screen when negative value is enter.

    the output hsould be like below:

    Please enter an integer: 2
    Please enter an integer: 5
    Please enter an integer: 4
    Please enter an integer: -1
    Total (product):40

    i hope u can help me out with this question

    thank you,


    #include<stdio.h>

    int main()

    {
    int num;
    int sum;
    int loop;

    printf("Enter numbers:\n");




    while (loop)
    {
    scanf("%d", &num);

    sum = num * num;

    if (num < 0)

    printf("total: %d", sum);
    }



    getch();

    }
    Last edited by DHSL; 05-27-2010 at 01:48 PM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Post your "codes".
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    6
    the one that i tried i put it above

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    First of all, what is the value of loop? Since you don't initialize it, it could be anything. You also don't have a condition for changing loop, so depending what garbage is in loop when you declare it, your while loop will either not happen at all, or be infinite.

    Also, each iteration (once you get your loop working) you are overwriting the value of sum with num*num. Ie, you are not keeping a running product.

    You should probably check to see if the input is negative -before- you do anything with it.

    what purpose does getch() serve there?
    Last edited by KBriggs; 05-27-2010 at 02:10 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    6
    thats way

    i cant get the logic of the question. beside, iam not professional to answer it.
    i cant use the loops properly

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    Don't think you need to be pro to get this one

    Ok:

    First of all, when do you want to stop your loop? When the input is negative. So that should be your loop condition.

    Secondly, if you want to keep a running product, you should read in each value, and them multiply the previous product by the value. (Not square the value that is read in like you are doing)

    And third, you should only multiply your total by the new input if it is positive. (If statment, maybe?
    Last edited by KBriggs; 05-27-2010 at 02:26 PM.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    11
    you cant get the logic???? shame on you man

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should initialize your variables before you start using them. That means, set them to an initial value, such as zero.

    You should add a step in your < 0 check to escape from your loop. Set loop to 0, or use a break statement in there after you print your total.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    6
    so,, is this condition correct

    while (num > 0; num !< 0) ?

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    6
    can i get the correction of this code in terms of codes!! please

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, the ; there splits that into two different statements, and the while loop expects only one. I would advise you to read your book's loop section over again.
    Code:
    for( initialize here; condition is true; change values here )
    {
        do stuff here
    }
    Or...
    Code:
    do
    {
        do stuff here, will happen at least once guaranteed
        you need to do any condition changes here typically
    } while( condition is true );
    Or...
    Code:
    while( condition is true )
    {
        do stuff here, may not happen, depending if condition is true at the first pass
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    11
    come on he explain every thing to you just convert the words to cods

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    6
    so, how to set the condition for the loop in this case, and how to multiply the numbers?

  14. #14
    Registered User
    Join Date
    May 2010
    Posts
    11
    just try to complete my solution

    #include<stdio.h>

    int main()

    {
    int num=0;
    int sum;

    while (num >=0)
    {
    printf( "the first number" );
    scanf("%d",&num);
    Last edited by eagle0eyes; 05-27-2010 at 03:14 PM.

  15. #15
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Your solution should use int main(void).
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 05:24 AM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 12:36 PM

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