Thread: Program not looping

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    13

    Angry Program not looping

    Hello all,
    Please I need your help again as I am stuck. I wrote a program to calculate the cumulative product of set of numbers. The program compiled but was not looping. Below is the code

    Code:
    /* program to calculate the cumulative products of a set of numbers*/
    #include <stdio.h>
    #include <math.h>
    main()
    {
       float num, product=1.0;
       int j, n;
       printf ("Enter the total items to calculate\n");
       scanf ("%f", &n);
       while (j<n)
          {
          printf ("\nEnter the number\n");
          scanf ("%f", num);
          product= product*num;
          ++j;
          };
       printf ("\nThe cumulative product is %f", product);
    }
    Please help me out.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You never initialize j, thus it has some arbitrary value. You have now learned why you should always initialize all of your variables before using them.


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

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Quote Originally Posted by quzah
    You never initialize j, thus it has some arbitrary value. You have now learned why you should always initialize all of your variables before using them.


    Quzah.
    Done it now and still the same problem persists
    Code:
    /* program to calculate the cumulative products of a set of numbers*/
    #include <stdio.h>
    #include <math.h>
    main()
    {
       float num, product=1.0;
       int j=1, n;
       printf ("Enter the total items to calculate\n");
       scanf ("%f", &n);
       while (j<n)
          {
          printf ("\nEnter the number\n");
          scanf ("%f", num);
          product= product*num;
          ++j;
          };
       printf ("\nThe cumulative product is %f", product);
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int j=1, n;
    printf ("Enter the total items to calculate\n");
    scanf ("%f", &n);
    while (j<n)
    {
        printf ("\nEnter the number\n");
        scanf ("%f", num);
    Notice anything in those bits above... regarding format specifiers and the use of the & operator?
    "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

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Changed, now looping but not calculating. I am doing something wrong. Please help me out.
    Code:
    /* program to calculate the cumulative products of a set of numbers*/
    #include <stdio.h>
    #include <math.h>
    main()
    {
       float num, product=1.0;
       int j=1, n;
       printf ("Enter the total items to calculate\n");
       scanf ("%d", &n);
       while (j<=n)
          {
          printf ("\nEnter the number\n");
          scanf ("%f", num);
          product*=num;
          ++j;
          };
       printf ("\nThe cumulative product is %f", product);
    }

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Still haven't fixed this one:
    Code:
    scanf ("%f", num);
    "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

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Quote Originally Posted by hk_mp5kpdw
    Still haven't fixed this one:
    Code:
    scanf ("%f", num);
    God bless you, it works very fine.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    ooooooo main should return

    Code:
    ++j;
          };
       printf ("\nThe cumulative product is %f", product);
       return 0;
    }
    ssharish2005

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    try this...

    try this...

    Code:
    /* program to calculate the cumulative products of a set of numbers*/
    #include <stdio.h>
    #include <math.h>
    void main()
    {
       float num, product=1.0;
       int j=0, n;
       printf ("Enter the total items to calculate\n");
       scanf ("%d", &n);
       while (j<n)
          {
          printf ("\nEnter the number\n");
          scanf ("%f", &num);
          product*=num;
          ++j;
          }
       printf ("\nThe cumulative product is %f", product);
    }

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    main isn't a void function. It is defined by the Standard to return an int.
    You don't need math.h to basic math such as is in your example.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM