cout in reverse

This is a discussion on cout in reverse within the C++ Programming forums, part of the General Programming Boards category; How can I make the output print out to the screen in reverse? Code: #include <iostream> #include <iomanip> using namespace ...

  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
    20,980
    Save the results into some kind of container, then print the contents of that container in the way that you want.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,980
    You could use an array here.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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
    3,831
    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, 12: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, 10:54 AM
  5. Whats the difference between cout and std::cout?
    By mdshort in forum C++ Programming
    Replies: 10
    Last Post: 12-30-2003, 04:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21