Thread: Need help on one of my assignments

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

    Need help on one of my assignments

    I was given this assignment but I'm not really sure where to start. Could someone give me some ideas?

    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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    Generally, the ideas are all there (e.g., "use a while loop").

    What have you tried?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    start with the program that ask the user to enter the number and just prints it back to console

    when it works - add the required loop that will ask and print the number

    when it works - write funtion that calculates factorial of the integer number returning int
    and print the result of calculations

    when it is done ... I hope you fill figure the futher modifications by yourself
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by engstudent363 View Post
    I was given this assignment but I'm not really sure where to start.
    Start by creating a project. Perhaps a Hello World project, which some compilers can create for you. Then just make as many small changes as you can to get it closer to what the assignment asks for.
    When you get stuck, search the web. If that fails, then finally come back here and post your code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    15
    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:

    btw this is a c program, not c++

    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");
    
    	}

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You really need to learn indentation:
    http://cpwiki.sf.net/User:Elysia/Indentation
    Take a look because it's important.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignments or challenges
    By Dogmasur in forum C Programming
    Replies: 12
    Last Post: 08-20-2008, 02:24 AM
  2. Replies: 3
    Last Post: 04-06-2007, 05:10 PM
  3. Help on school assignments
    By J.Z. in forum C Programming
    Replies: 5
    Last Post: 10-17-2003, 02:40 PM
  4. C++ programming assignments and tests
    By NeoNite in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2003, 05:41 AM
  5. Embedded assignments
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 08-28-2001, 10:58 AM