GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Mar-2009, 11:25
uncle pennybags uncle pennybags is offline
New Member
 
Join Date: Mar 2009
Posts: 2
uncle pennybags is on a distinguished road

A penny to the first person that can show me my error


Hi, I'm kind of a novice C++ programmer right now. I'm not completely new to programming, but its been quite a few years without any real coding. I've worked with, and in, other scripts, just adding a line here or there, but nothing serious. I have some plans for the future however, that I thought it was best if I seriously learn how to program, and start over from scratch. So, I've been just working my way through a few C++ beginner books for the last few days to get a refresher and then hopefully move onto some more advanced books.

Anyway, I've looked and re-looked at the code for this very simple program and I simply can't find anywhere that it differs from the code in the book, or why it doesn't work. I must have a typo in there somewhere. I just can't for the life of me, see it. The program compiles fine. But after it collects the user input, it just sits there and doesn't ever do anything else.

CPP / C++ / C Code:
// factorial.cpp - Script 5.4

#include <iostream>

//function prototypes
void promptAndWait(); 
unsigned long returnFactorial(unsigned short num);

int main() { //begin main
    unsigned short numberIn;
    
    std::cout << "Enter a small, positive integer: [##] "; 
    std::cin >> numberIn; //prompt user and assign input to numberIn
    
    //print the factorial of the number, by calling the function, returnFactorial
    std::cout << "The factorial of " << numberIn << " is " << returnFactorial(numberIn) << ".\n\n"; 
    
    promptAndWait();
    return 0;
} //END OF MAIN

//define the functions
void promptAndWait() {
    std::cin.ignore(100, '\n');
    std::cout << "Press Enter or Return to Continue.\n";
    std::cin.get();
} //END of promptAndWait

unsigned long returnFactorial(unsigned short num) {
    unsigned long sum = 1; 
    for (int i = 1; 1 <= num; ++i) { //multiply i * sum, then increment i until i is greater than num
    sum *= i;    
    }// END for Loop
         
    return sum;
} //END returnFactorial function
Last edited by admin : 12-Mar-2009 at 21:25. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 12-Mar-2009, 12:09
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: A penny to the first person that can show me my error


Quote:
Originally Posted by uncle pennybags
Hi
Hi.


Try changing this:
CPP / C++ / C Code:
    for (int i = 1; 1 <= num; ++i) {

to this
CPP / C++ / C Code:
    for (int i = 1; i <= num; ++i) {

Regards,

Dave

Footnotes:
1. You can make the program tell you what it is doing with something like:
CPP / C++ / C Code:
    for (int i = 1; 1 <= num; ++i) {
        sum *= i;    
        std::cout << "i = " << i << ", sum = " << sum << std::endl;
    }

or some such thing. You can see that it really is doing "something." Just not what you had in mind!

2. As a matter of style, why the heck didn't you call it something like "product" instead of "sum"? Or is it from the book? Did the book call it "sum"? I wonder why. Maybe it's not important to you, Maybe not now, but...

3. A penny? What? And lose my amateur status? No thanks. Use it to buy yourself and your Significant Other a treat. Take a trip. See a show.
Last edited by davekw7x : 12-Mar-2009 at 12:39.
  #3  
Old 12-Mar-2009, 13:03
uncle pennybags uncle pennybags is offline
New Member
 
Join Date: Mar 2009
Posts: 2
uncle pennybags is on a distinguished road

Re: A penny to the first person that can show me my error


Ahh, thank you, there was my typo. damn 1's and i's look too similar sometimes.

And yes, using sum is just what the book did. I'm trying to be a good little student and follow along exactly, for now.

And thanks as well for the penny back. I think I'll invest it in the stock market. I should be able to get a few hundred shares of GM or something these days with it.
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 10:13
Linked Lists advice request promsan C Programming Language 74 23-May-2007 08:29
Major newbie problem cynack MS Visual C++ / MFC Forum 1 08-Apr-2007 11:25
Winsock error when compiling FLTK 2.0 Projects mauriciorossi FLTK Forum 3 16-Aug-2005 10:18
Help with syntax errors PeteGallo C Programming Language 7 08-Aug-2005 20:30

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:39.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.