Thread: cout in reverse

  1. #1
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88

    cout in reverse

    How can I make the output print out to the screen in reverse?

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int getnum();
    void Fibonacci(int n);
    
    int main () 
    {
    	int n;   
    	
    	cout << "\n\t    +======================================================+\n";
    	cout << "\t    |========                                      ========|\n";
    	cout << "\t    |=====                                            =====|\n";
    	cout << "\t    |===          Fibonacci Sequence Generator          ===|\n";
    	cout << "\t    |====                                              ====|\n";
    	cout << "\t    |=======                                        =======|\n";
    	cout << "\t    +======================================================+\n\n\n\n";
    		
    	n = getnum();
    	
    	Fibonacci(n);
    	
    	cout << "\n";
        
    	return 0;
    }
    
    
    /*********************************************FUNCTIONS*************************************************/
    int getnum()
    {
    	int n = 0;
    
    	do{
    
    		cout << "\n    +---------------------------------------------------------------------+\n";
    		cout << "    |  How many numbers of the FIBONACCI SEQUENCE do you want dislayed??  |\n";
    		cout << "    +---------------------------------------------------------------------+\n\n\t\t\t\t     :";
    		cin >> n;
    	
    			if(n <= 0)
    			{
    			cout << "\n\t\t      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
    			cout << "\t\t      !!!!!!                      !!!!!!\n";
    			cout << "\t\t      !!!!    Does Not Compute!!    !!!!\n";
    			cout << "\t\t      !!!!!!                      !!!!!!\n";
    			cout << "\t\t      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
    			}
    			cout << '\n';
    
    	}while(n <= 0);
    	
    	return n;
    }
    
    
    
    void Fibonacci(int n)
    {
    	int i;
    	int x = 0, y = 1;
    	int sum;
    	const int MaxPerLine = 6;
    	int count = 0;
    	
    		cout << "\n\t   *******************************************************\n";
    		cout << "\t     The first " << n << " integers of the Fibonacci series are:  \n";
    		cout << "\t   *******************************************************\n\n";
    
    			for (i = 0; i < n; ++i) 
    			{					
    					if(i <= 1)
    					{
    					sum = i;
    					}
    					cout << setw(5) << x;				
    			
    					
    					sum = x + y;    //sums x and y at ever increment					
    					x = y;    //changes variable value in preperation for the next calculation 	    
    					y = sum;     //changes variable value in preperation for the next calculation	    
    					++count;
    						
    					
    					if(count % 6 == 0)    //condition that inputs a new line after every 6 values
    					{
    					cout << '\n';
    					}
    			}
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Save the results into some kind of container, then print the contents of that container in the way that you want.
    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
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Makes sense, thank you. Would an array would be an example of a container?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could use an array here.
    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

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Stack is typically a good one since what comes in first comes out last (or what comes in last comes out first).
    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
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88

    another issue

    After a couple of alterations to the program, most of it works the way I want it to except the first number being printed out (isn't correct). I'm going to change my container from an array to a vector to see if that helps, meanwhile can anyone give me some ideas?

    sorry for posting the whole program

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int getnum();
    void Fibonacci(int length);
    void init( int *, int length, int x);
    void prnt( const int *, int length, int x);
    
    int main () 
    {
    	int length;   
    	
    	cout << "\n\t    +======================================================+\n";
    	cout << "\t    |========                                      ========|\n";
    	cout << "\t    |=====                                            =====|\n";
    	cout << "\t    |===          Fibonacci Sequence Generator          ===|\n";
    	cout << "\t    |====                                              ====|\n";
    	cout << "\t    |=======                                        =======|\n";
    	cout << "\t    +======================================================+\n\n\n\n";
    		
    	length = getnum();
    		
    	if(length < 2)
    		{
    		return 0;
    		}
    	
    	Fibonacci(length);
    	    
    	return 0;
    }
    
    
    /*********************************************FUNCTIONS*************************************************/
    int getnum()
    {
    	int n = 0;    // length = n in main function :) 
    		  
    	do{
    
    		cout << "\n    +-------------------------------------------------------------------+\n";
    		cout << "    |  How many numbers of the FIBONACCI SEQUENCE do you want dislayed  |\n";
    		cout << "    |                                                                   |\n";
    		cout << "    |                         Greater than 1                            |\n";
    		cout << "    +-------------------------------------------------------------------+\n\n\t\t\t\t     :";
    		cin >> n;
    	
    			if(n < 2)
    			{
    			cerr << "\n\t\t  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
    			cerr << "\t\t  !!!!!!                             !!!!!!\n";
    			cerr << "\t\t  !!!!     Length is Out of Bounds     !!!!\n";
    			cerr << "\t\t  !!!!!!                             !!!!!!\n";
    			cerr << "\t\t  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
    			break;
    			}
    
    	}while(n < 2);
    	
    	return n;
    }
    
    
    
    void Fibonacci(int length)
    {
    	int x = 0;
    	
    	const int MaxPerLine = 6;
    	
    	
    	int *Sequence = new int[length];
    	
    
    		cout << "\n\t   *******************************************************\n";
    		cout << "\t     The first " << length << " integers of the Fibonacci series are:  \n";
    		cout << "\t   *******************************************************\n\n";
    
    
    			init(Sequence, length, x);
    
    			prnt(Sequence, length, x);
    		
    
    	delete [ ] Sequence;
    			
    }
    
    
    
    
    
    void init(int *Sequence, int length, int x)
    {
    	x = 0; int y = 1;int sum;
      
    
    	for( int i = 0; i < length; ++i )
        {
    			
    		Sequence[i] = x;
    		
    		
    			if(i <= 1)
    			{
    			sum = i;
    			}				
    					
    			sum = x + y;    //sums x and y at ever itteration					
    			x = y;    //changes variable value in preperation for the next calculation 	    
    			y = sum;     //changes variable value in preperation for the next calculation	    
    	}
    
    }
    
    
    
    
    void prnt( const int *Sequence, int length, int x)
    {
    	int count = 0;
    
    		for(int i = length; i >= 0; --i)
    		{					
    			cout << setw(5) << Sequence[i];	
    			++count;
    						
    			if(count % 6 == 0)    //condition that inputs a new line after every 6 values
    			{
    			cout << '\n';
    			}
    
    		}
    
    }

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Incorrect? Or too many values printed?

    Also, why does `x' appear in so many functions?

    Also, have you tried entering a string value instead of an integer value?

    Soma

  8. #8
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    Quote Originally Posted by phantomotap View Post
    O_o

    Incorrect? Or too many values printed?

    Also, why does `x' appear in so many functions?

    Also, have you tried entering a string value instead of an integer value?

    Soma
    Thank you. Your response helped me rationalize everything out. It works fine now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cout help!
    By omGeeK in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2012, 01:45 PM
  2. Replies: 1
    Last Post: 05-30-2010, 10:22 PM
  3. cout
    By h_howee in forum Windows Programming
    Replies: 5
    Last Post: 06-26-2006, 11:56 AM
  4. std::cout or using namespace std or using std::cout
    By ComDriver in forum C++ Programming
    Replies: 13
    Last Post: 01-31-2005, 11:54 AM
  5. Whats the difference between cout and std::cout?
    By mdshort in forum C++ Programming
    Replies: 10
    Last Post: 12-30-2003, 05:34 PM