Help for the desperate ...?

This is a discussion on Help for the desperate ...? within the C Programming forums, part of the General Programming Boards category; Hello all... I have been working on this for about a week now, which is probably pretty pathetic to admit. ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    2

    Question Help for the desperate ...?

    Hello all...

    I have been working on this for about a week now, which is probably pretty pathetic to admit. This assignment is for a beginners C Ansi programming class. I have created this using the "for", "do" and "while" loops, and am missing the same thing every time.

    Here is my problem:

    When the user enters a negative number, an error message is initiated. The program is then supposed to ignore that and prompt the user for the number again. For example, if it were positive value # 3, and a negative number was entered, then the negative number would be ignored, and the program would prompt for #3 again.

    I'm sure this is something stupid I keep doing over again, since I keep coming up with the same anwer, but I would truly appreciate any insight you could give me. Even if you were to simply ask me a question to put me in the right direction. I seem to have entered my own loop, and can't get out.

    Thanks in advance!!!!!
    Very humbly .....

    Kathy

    here is the code:

    #include <stdio.h>
    void main (void)

    {
    /*------------Declare Variables--------------------------*/

    int value,x;
    int tot_val = 0;
    int pos_int = 0;
    float average;

    /*------------Describe what the program does-------------*/


    printf("\nThis program calculates the average of as many numbers as you wish to enter.");


    /*------------Give instructions to the user--------------*/


    printf("\n\nFirst, enter the number of values to process:");

    scanf("%i", &value);

    printf("\nNow enter the %i numbers to be averaged.\n\n",value);



    /*------------Enter the loop information----------------*/

    for (x=1; x <= value; x++)

    {
    printf("Enter the positive integer #%i: ", x);

    scanf ("%i", &pos_int);

    tot_val=tot_val + pos_int;

    {

    if(pos_int < 0 )

    printf("\n***Invalid entry. Value must be positive.\n");
    continue;

    }

    pos_int++;
    }

    /*-------------Figure the average--------------------- */



    average = (float)tot_val / value;


    printf("\n\nThe average of the %i numbers entered is %.2f\n.",value,average);

    }//end main

  2. #2
    Registered User Eigenvalue's Avatar
    Join Date
    Aug 2002
    Posts
    21
    Hi kmr6655,

    try this instead
    Code:
    for (x=1; x <= value; x++){
                        
       printf("Enter the positive integer #%i: ", x);
       scanf ("%i", &pos_int);
    
       while ( pos_int < 0 ){
          printf( "Please enter a positive integer!\n" );
          scanf( "%i", &pos_int );
       }
       tot_val=tot_val + pos_int;
       pos_int++;
    }
    The user will stay in the while loop until they enter in a positive number.

    Hope this helps!

    [edit]Change void main(void) to int main(void)and add return 0; at the end of main.
    main always returns an int.[/edit]
    Last edited by Eigenvalue; 11-03-2002 at 11:58 AM.
    "I'm not responsible for what other people think I am able to do." -- Richard Feynman

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    2
    Just wanted to say thank you for your help.....

    It hung for some reason, so I added the printf statement, and it works like a charm.

    Much appreciated!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Desperate Over Ecs!
    By cookie in forum C Programming
    Replies: 17
    Last Post: 07-16-2008, 02:25 PM
  2. Debugging Help - I'm Desperate
    By Tman in forum Game Programming
    Replies: 2
    Last Post: 02-21-2006, 01:39 PM
  3. Desperate Help needed
    By roco090 in forum C++ Programming
    Replies: 9
    Last Post: 02-18-2005, 09:43 PM
  4. I don't like doing this but i'm desperate
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-21-2002, 07:44 PM
  5. Desperate for help - ugly nested if
    By baseballkitten in forum C Programming
    Replies: 4
    Last Post: 11-19-2001, 03:56 PM

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