Thread: Using do while to print # of words per line...

  1. #1
    Banned
    Join Date
    Apr 2011
    Posts
    56

    Using do while to print # of words per line...

    Hi, I can make do while loop to work here. It is suppost to print user defined(numPerLine) words per line but for some reason it prints all in one line. I know something is wrong with my do while. Didn't practice do while enough and now i am having trouble (I like fro loop )

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    //function prototypes
    
    int getNumber();
    int printnumPerLine();
    void printResult(int, int);
    
    int main()
    {
    int num;
    int numPerLine;
    
    num = getNumber();
    numPerLine = printnumPerLine();
    printResult(num, numPerLine);
    }
    
    //get input from keyboard and return it to caller
    int getNumber()
    {
    int num;
    	cout << "\n\nPlease enter a non-negative number: ";
    	cin >> num;
    	
    	return num;
    }
    
    int printnumPerLine()
    {
    	int numPerLine;
    
    	cout << "\nHow many numbers per line? ";
    	cin >> numPerLine;
    
    	return numPerLine;
    }
    //print our result
    void printResult(int num, int numPerLine)
    {
    	int i = 0;
    
    	do{
    		cout << setw(4) << i;
    		i++;
    	} while (i<=num);
    		
    	if(i % numPerLine == 0){
    			cout << endl;
    	}
    	cout << endl << endl;
    }
    Thanks

    sorry, I had to post it again because I unintentionally posted it to C forums instead.
    Last edited by Iron Hide; 05-11-2011 at 05:09 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,059
    Untill you pass 'cout' a newline character '\n' or 'endl', it will keep printing on the same line.
    Devoted my life to programming...

  3. #3
    Banned
    Join Date
    Apr 2011
    Posts
    56
    Where do I need to enter endl?

    I did it here:
    Code:
    if(i % numPerLine == 0){
    	cout << endl;
    Thanks

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Iron Hide View Post
    Where do I need to enter endl?

    I did it here:
    Code:
    if(i % numPerLine == 0){
    	cout << endl;
    Thanks
    You've already printed all the numbers by the time you get there, since that is after your loop. Perhaps you intended to place it inside the loop?

  5. #5
    Banned
    Join Date
    Apr 2011
    Posts
    56
    works! I tried pacing it various paces but this one Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to print a data file with words and numbers
    By Weyoun92 in forum C Programming
    Replies: 6
    Last Post: 05-06-2011, 07:26 AM
  2. how to open files and print line by line in shell
    By omega666 in forum Linux Programming
    Replies: 4
    Last Post: 04-15-2011, 04:54 PM
  3. how to print a increasing number of words?
    By gershonj in forum C Programming
    Replies: 4
    Last Post: 07-11-2008, 07:36 AM
  4. reading words line by line from a file
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 05-04-2008, 12:34 AM
  5. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM