7 Replies - 470 Views - Last Post: 11 May 2011 - 04:26 PM Rate Topic: -----

#1 Iron Hide  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 9
  • Joined: 01-May 11

Can make my do while loop to work ...

Posted 11 May 2011 - 03:47 PM

I can make this code work in for loop but can't in do while loop. Can someone please take a look at this:

#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;
}


This program output the resultaccording to the user's defined number of lines.

Is This A Good Question/Topic? 0
  • +

Replies To: Can make my do while loop to work ...

#2 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Can make my do while loop to work ...

Posted 11 May 2011 - 03:49 PM

DO you get any errors? does it run at all? what is it managing to do as opposed to what you want it to do?
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Can make my do while loop to work ...

Posted 11 May 2011 - 03:59 PM

What is the output supposed to look like?
Was This Post Helpful? 0
  • +
  • -

#4 Iron Hide  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 9
  • Joined: 01-May 11

Re: Can make my do while loop to work ...

Posted 11 May 2011 - 04:13 PM

It works but instead of printing user's defined (numPerLine)# of words per line, it prints all in one line.

I used if statement to print nemPerLine but its not working

if(i % numPerLine == 0){
 cout << endl;

Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Can make my do while loop to work ...

Posted 11 May 2011 - 04:18 PM

put the if-statement into the loop?
Was This Post Helpful? 0
  • +
  • -

#6 Iron Hide  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 9
  • Joined: 01-May 11

Re: Can make my do while loop to work ...

Posted 11 May 2011 - 04:21 PM

where do you want me to put it. because I tried moving it to various places but didn't work

Thanks
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Can make my do while loop to work ...

Posted 11 May 2011 - 04:25 PM

//print our result
void printResult(int num, int numPerLine) {
    int i = 0;

    do {
        cout << setw(4) << i;
        i++;

        if(i % numPerLine == 0) {
            cout << endl;
        }
    } while (i <= num);

    cout << endl << endl;
}


output:
C:\CProjects\Forum Help>231634-can-make-my-do-while-loop-to-work.exe


Please enter a non-negative number: 22

How many numbers per line? 5
   0   1   2   3   4
   5   6   7   8   9
  10  11  12  13  14
  15  16  17  18  19
  20  21  22


C:\CProjects\Forum Help>

Was This Post Helpful? 1
  • +
  • -

#8 Iron Hide  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 9
  • Joined: 01-May 11

Re: Can make my do while loop to work ...

Posted 11 May 2011 - 04:26 PM

Awesome! thanks NickDMax

Works perfect

Thanks again
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1