Extra new line character without code

This is a discussion on Extra new line character without code within the C++ Programming forums, part of the General Programming Boards category; OK, so I have completed a homework assignemnt, and the code is correct; however, it is not running as smooth ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    Extra new line character without code

    OK, so I have completed a homework assignemnt, and the code is correct; however, it is not running as smooth as I wish for it to. I am posting this purely out of curiosity. The program prints out a diamond (consisting of asterisks) where the size is based on a # received from the user. The problem is that after the diamond is printed it prints out a lot of extra lines. Here it is!





    Code:
    
    #include<iostream>
    using namespace std;
    
    int main(){
    
    	int width,		//used to receive width from user
    		star = 0,	//used to increment asterisks and is defaulted to 0 as the initial do-while loop must print only 1 star
    		space;		//used to increment the spaces before asterisks
    	do{
    		cout << endl << endl << "Welcome to the Asterisk Diamond Generator"  << endl << endl;
    		cout << "Please enter an odd number which will be the width of the diamond: ";	//receive width variable
    		cin>>width;
    		if(width &#37; 2 == 0)	//prevent program from continuing with an even #
    			cout <<"\nWell, that's almost and odd number, but try again...\n\n\n";
    	
    	}while(width % 2 == 0);
    
    	space = (width/2);		//set the initial spacing for the star
    	cout<< "\n\n\n\n\n";		//clear room for a good drawing
    
    	do{									//draw the top half
    		for(int i = 0; i <= space; i++)
    			cout<< " ";
    		for(int j = 0; j <= star; j++)
    			cout<< "*";
    
    		cout<< endl << endl;
    		star += 2;
    		space -= 1;
    	}while(star <= width);
    
    
    	space = 1;			//set the initial spacing and star count for the bottom half
    	star = (width - 3);
    
    
    	do{		//draw the bottom half
    		for(int i = 0; i <= space; i++)
    			cout<< " ";
    		for(int j = 0; j <= star; j++)
    			cout<< "*";
    
    		cout<< endl << endl;
    		star -= 2;
    		space += 1;
    	}while(space <= (width+1));
    
    	cout<< "Scroll up to see the diamond.\n\n";
    	cout<< "Thank you for using the Asterisk Diamond Generator." << endl << endl << endl;
    
    	return 0;
    }
    Any advice on this problem would be grrrrrreat!
    Last edited by CornedBee; 01-31-2008 at 12:10 PM. Reason: Rescued the layout from the evil tabs.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    How many is lots? Are you aware of this part of your program?
    Code:
    cout<< "Thank you for using the Asterisk Diamond Generator." << endl << endl << endl;
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Thank you for taking a look, but that is not where the extra spaces are. Here is the code again with notation about where the extra spaces seem to occur. To see this in action, you will need to compile the program because the spaces do seem to occur without any code creating them as far as I can tell.


    Code:
    
    #include<iostream>
    using namespace std;
    
    int main(){
    
    	int width,		//used to receive width from user
          		star = 0,	//used to increment asterisks and is defaulted to 0 as the initial do-while loop must print only 1 star
    		space;		//used to increment the spaces before asterisks
    	do{
    		cout << endl << endl << "Welcome to the Asterisk Diamond Generator"  << endl << endl;
    		cout << "Please enter an odd number which will be the width of the diamond: ";	//receive width variable
    		cin>>width;
    		if(width % 2 == 0)	//prevent program from continuing with an even #
    			cout <<"\nWell, that's almost and odd number, but try again...\n\n\n";
    	
    	}while(width % 2 == 0);
    
    	space = (width/2);		//set the initial spacing for the star
    	cout<< "\n\n\n\n\n";		//clear room for a good drawing
    
    	do{									//draw the top half
    		for(int i = 0; i <= space; i++)
    			cout<< " ";
    		for(int j = 0; j <= star; j++)
    			cout<< "*";
    
    		cout<< endl << endl;
    		star += 2;
    		space -= 1;
    	}while(star <= width);
    
    
    	space = 1;			//set the initial spacing and star count for the bottom half
    	star = (width - 3);
    
    
    	do{		//draw the bottom half
    		for(int i = 0; i <= space; i++)
    			cout<< " ";
    		for(int j = 0; j <= star; j++)
    			cout<< "*";
    
    		cout<< endl << endl;
    		star -= 2;
    		space += 1;
    	}while(space <= (width+1));
    
    /*
    
                       
    
     EXTRA SPACES OCCUR HERE
    
    
    
    */
    	cout<< "Scroll up to see the diamond.\n\n";
    	cout<< "Thank you for using the Asterisk Diamond Generator." << endl << endl << endl;
    
    	return 0;
    }

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Modify the loop for the bottom half to look like this:
    Code:
            do{             //draw the bottom half
                    for(int i = 0; i <= space; i++)
                            cout<< " ";
                    for(int j = 0; j <= star; j++)
                            cout<< "*";
    
                    cout<< endl << endl;
                    star -= 2;
                    space += 1;
                    cout << "Space: " << space << ", Star: " << star << "\n";
            }while(space <= (width+1));
    I think you'll find it enlightening.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Haha, awesome. I feel so dumb now...

    Thanks for helping me out with that!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  2. Suggest another way or how can my code be improved
    By peterchen in forum C Programming
    Replies: 6
    Last Post: 01-23-2006, 04:37 PM
  3. Simple question (for you not for me!)
    By antonis in forum C Programming
    Replies: 35
    Last Post: 11-10-2005, 12:41 PM
  4. Output text containing '\r' as new line character.
    By anonytmouse in forum C Programming
    Replies: 4
    Last Post: 11-17-2003, 08:47 PM

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