Thread: Need help!

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    15

    Need help!

    Assignment:

    Part 1: Write a program that requires the user to input a non-negative number and then calculates the factorial of that number and prints the result to the screen. Use a while loop to continuously ask for a number (see example output below). If a negative number other than -1 is entered, print and error message and prompt for another input from the user. If -1 is entered, exit the program.
    Part 2: Modify your program (in a different file) using switch to print a menu to the screen, asking the user whether they want to calculate the factorial of a non-negative number, or exit. If they choose to calculate the factorial and enter any negative number, print an error message and take them back to the main menu. If they choose an invalid menu option, print an error message and re-print the menu.

    I'm having trouble figuring out how to have the computer calculate a number's factorial and then display it. Also after you enter a negative number I don't know how to return back to the while loop so it asks for another number. Any suggestions? Here's what I got so far:

    Code:
    #include <stdio.h>
    
    	int main() {
    
    	int big, num;
    
    	num = 5;
    
    	while (num >= 0) {
    
    	   printf("Enter the number.\n");
    	   scanf("%d", &big);
    	   num=big;
    	   printf("%d!\n", big);
    	   }
     
    	printf("ERROR - You have just entered a negative number, please enter a non-negative number.\n");
    
    	}

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Since you should only exit the while loop when the input is -1, you shouldn't write "while (num >= 0)".

    Do you know what the factorial function is?

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    15
    I know what a factorial means, such as if I was given 5! that would mean 5*4*3*2*1 but I don't know how to implement that into my program so that the computer knows to do that.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    15
    Also, Is there a command that tells the program to go back to the beginning of the program? Because if the number entered is a negative number then the program exits the while loop and goes to the message "Error - you have entered a negative nu . . . . ." but then the program stops. I want the program to go back to the while loop. Any suggestions? (To this question and the one above)

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To go back to the beginning of the loop? Yes. Either the condition must not be met or you use continue.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by engstudent363 View Post
    I know what a factorial means, such as if I was given 5! that would mean 5*4*3*2*1 but I don't know how to implement that into my program so that the computer knows to do that.
    But you just said how to do it. Start at 5, multiply down to 1. If you want to do something repetitively, then you need to use a loop structure.

    Quote Originally Posted by engstudent363 View Post
    Also, Is there a command that tells the program to go back to the beginning of the program? Because if the number entered is a negative number then the program exits the while loop and goes to the message "Error - you have entered a negative nu . . . . ." but then the program stops. I want the program to go back to the while loop. Any suggestions? (To this question and the one above)
    Read the response again: if you only want to stop on -1, then your while loop must check for -1, not any negative number. (This means your check for invalid (other negative number) input must also be inside the while loop, so that it gets repeated as well.)

Popular pages Recent additions subscribe to a feed