How to put column on a for loop

Having trouble creating a column within a for loop

Page 1 of 1

7 Replies - 3934 Views - Last Post: 22 March 2010 - 11:19 AM Rate Topic: -----

#1 Guest_ppy1111*


Reputation:

How to put column on a for loop

Posted 22 March 2010 - 10:01 AM

Hi Guys,

I'm having trouble creating columns in a for loop. I'm suppose to create two columns when the user enters 2 for cin. I've created an if statement with a nested for loop.

The output is suppose to be like this:

Number of columns: 2

A B
C D
E F
G H
I J
K L
M N
O

but the way i coded it it turns out to be like this:

Number of columns: 2

A B C D E F G H I J K L M N O

What do i need to do create two columns
here's what i have so far:

 
#include <iomanip>
#include <iostream>

using namespace std;

int main()
{
    int column, ctr, column_ctr;
    char ch;

    while (ctr=1)
    {
        cout << "Number of column: ";
        cin >> column;
        cout << endl;

            if (column==0)
                {return 0;}

            else if (column==1)
                {for (ch='A'; ch<='O'; ch++)
                    {cout << setw (9) << ch << endl;}
                cout << endl;
                }

            else if (column==2)
                {for (ch='A'; ch<='O'; ch++)
                    {cout << setw (9) << ch;}
                cout << endl << endl;
                }

     

    ctr++;
    }

    return 0;

}


This post has been edited by Martyn.Rae: 22 March 2010 - 10:04 AM
Reason for edit:: Edited end code tag


Is This A Good Question/Topic? 0

Replies To: How to put column on a for loop

#2 Tapas Bose  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 472
  • Joined: 09-December 09

Re: How to put column on a for loop

Posted 22 March 2010 - 10:08 AM

Here it is :
#include <iomanip>
#include <iostream>

using namespace std;

int main()
{
   int column, ctr, column_ctr;
   char ch;

   while (ctr = 1)
   {
      cout << endl;
      cout << "Number of column: ";
      cin >> column;
      cout << endl;

      if (column == 0)
      {
         return 0;
      }

      else if (column==1)
      {
         for (ch='A'; ch<='O'; ch++)
         {
            cout << setw (9) << ch << endl;
            cout << endl;
         }
      }

      else if (column == 2)
      {
         int counter = 0;
         for (ch='A'; ch<='O'; ch++)
         {
            cout << setw (9) << ch;
            counter++;
            if (counter == 2)
            {
               cout<<endl;
               counter=0;
            }
         }        
       }

      ctr++;
   }

   return 0;
}

This post has been edited by Tapas Bose: 22 March 2010 - 10:15 AM

Was This Post Helpful? 0
  • +
  • -

#3 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: How to put column on a for loop

Posted 22 March 2010 - 10:10 AM

In the for-loop for two columns you have no line break (endl) anywhere. You need to break the line after you print two characters. One way to do that is to print the character, increment it, print it again, and then break the line (all of this should happen in the for loop):

for (char ch = 'A'; ch <= 'O'; ch++) {
    cout << setw(9) << ch;
    if ((ch + 1) <= 'O')
        cout << setw(9) << ++ch;
    cout << endl;
}

This post has been edited by diego_pmc: 22 March 2010 - 10:25 AM

Was This Post Helpful? 0
  • +
  • -

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: How to put column on a for loop

Posted 22 March 2010 - 10:11 AM

A simpler method would be to check if the loop counter (converted to int) is even:
else if (column==2)
{
	for (ch='A'; ch<='O'; ch++)
	{
                // actually is ch + 1 for your desired output
                // since A "integered" is 65
		if (int(ch + 1) % 2 == 0)
			cout << endl;
		cout << setw (2) << ch;
	}
	cout << endl << endl;
}


Was This Post Helpful? 0
  • +
  • -

#5 LiveToCode  Icon User is offline

  • New D.I.C Head

Reputation: 11
  • View blog
  • Posts: 37
  • Joined: 17-March 10

Re: How to put column on a for loop

Posted 22 March 2010 - 10:13 AM

Another solution is
for (int k = 1; k < 10; k++) {
   cout<<k <<" ";
   if (k % 2 == 0)
      cout<<endl;
}

That will print the numbers in 2 columns. You can modify the 2 in the if statement to the number of columns you want.
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: How to put column on a for loop

Posted 22 March 2010 - 10:15 AM

I think a single for loop can get the job done dont you think?
#include <iostream>

using namespace std;

int main()
{
	int count = 0;
	for(char i = 'a'; i<='z'; i++)
	{
		count++;
		cout<<i;
		if(count%2 == 0)
			cout<<endl;
	}
	
	cin.ignore();
	cin.get();
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#7 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 81
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: How to put column on a for loop

Posted 22 March 2010 - 10:22 AM

And also...
while (ctr=1)


... I guess you meant one of these...
while (ctr == 1)
// or...
while (ctr != 1)


This post has been edited by diego_pmc: 22 March 2010 - 10:26 AM

Was This Post Helpful? 0
  • +
  • -

#8 Guest_ppy1111*


Reputation:

Re: How to put column on a for loop

Posted 22 March 2010 - 11:19 AM

Thanks Guys,

I've been trying to solve this for a couple of hours and I ran out of ideas so i turned to the internet.
Thank you for all the suggestions, I really appreciate all the help

Till next time
Was This Post Helpful? 0

Page 1 of 1