Thread: recursivle function help

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    recursivle function help

    Code:
    #include<iostream>
    using namespace std;
    
    void reverseInput();
    
    void main(){
    	reverseInput();
    	
    }
    void reverseInput(){
    	cout<<"Enter 0 to end.";
    	cout<<"Enter a number: ";
    	int num;
    	cin>> num;
    	int first;
    	if(num !=0){
    		first = num;
    		reverseInput();//will keep calling until it reaches 0.
    		cout<<"The numbers in reverse order are ";
    		cout<<" "<<first<<", "<<endl;//now it prints from the end.
    	}
    
    
    }
    how do i make this line
    "The numbers in reverse order are ";
    to display only once when i end the input by entering 0.

    right now
    when i end the input by entering 0, that line will print when each number is printed.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by nicz888 View Post
    [code]
    how do i make this line
    "The numbers in reverse order are ";
    to display only once when i end the input by entering 0.
    Have main() print the message instead of the reverse function doing it.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And you don't really need the variable "first" - just use num.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    37
    Quote Originally Posted by brewbuck View Post
    Have main() print the message instead of the reverse function doing it.
    when i do that it will look like this
    sample output and input

    Enter 0 to end
    Enter a number: 2
    Enter a number: 5
    Enter a number: 6
    Enter a number: 8
    Enter a number: 0
    0
    8
    6
    5
    2
    The numbers in reverse order are

    which is not right i want it to look like this
    Enter 0 to end
    Enter a number: 2
    Enter a number: 5
    Enter a number: 6
    Enter a number: 8
    Enter a number: 0
    The numbers in reverse order are 0 8 6 5 2

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, you need to put the output in an else-branch of the "if(num != 0)"...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM