GIDForums

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Mar-2008, 01:08
signorsinatra signorsinatra is offline
New Member
 
Join Date: Mar 2008
Posts: 5
signorsinatra is on a distinguished road

Nested for loop issues


Hi, I'm new to posting on the forum, but have been reading it for some time. I am sure that my problem is just my mind escaping me, but I have been trying everything I can think of to get this to work. I need to create a program that will ask a user to input two different characters and an integer and then put their output as so:

For example: If a user enters % & 3, the output would be:

%&%
&%&
%&%

If a user enters ^* 4, the output would be:

^*^*
*^*^
^*^*
*^*^

I know that I need to use a nested for loop. I think I have the first for statement ok which will make the output set to n number of columns. I think I also have the nested for statement ok to print n rows. I just can't seem to get the cout statements after the nested for loop correct.

This is what I have so far: It outputs like this:

input: $ % 3

$%$%$%
$%$%$%
$%$%$%

I think I have the looping structure almost correct, but I just can't for the life of me think of a way to format the output to get my desired result. I have included my code, but I was wondering if you had any suggestions to where I should go to learn, or what steps I can take to better my output?

I can only use iostream, I can't use iomanip.
CPP / C++ / C Code:
#include <iostream>
using namespace std;

int main()
{
    int n;
    char a, b;
    cout << "Please enter two characters and an integer: " << flush;
    cin >> a >> b >> n;

	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
                <<a<<b;
	
	     cout<<endl;
		
        }
	
         system("pause");
         return 0;
     }
  #2  
Old 23-Mar-2008, 06:20
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 353
Peter_APIIT is on a distinguished road

Re: nested for loop issues


No system("pause");
__________________
Linux is the best OS in the world.
  #3  
Old 23-Mar-2008, 09:07
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,102
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough

Re: nested for loop issues


Quote:
Originally Posted by signorsinatra
Hi, I'm new to posting on the forum, but have been reading it for some time. I am sure that my problem is just my mind escaping me, ...

Welcome to GIDForums™. If you look at your patterns you want to output the test conditions pretty much write themselves.

IF I am on an ODD line AND at an ODD char position I want to COUT << A
IF I am on an ODD line AND at an EVEN char position I want to COUT << B
IF I am on an EVEN line AND at an ODD char position I want to COUT << B
IF I am on an EVEN line AND at an EVEN char position I want to COUT << A

Every time I get ready to increment my current print line I want to COUT << ENDL

My Output:
Code:
$ ./TwoChars Please enter two characters and an integer : % & 3 %&% &%& %&%
Code:
$ ./TwoChars Please enter two characters and an integer : ^* 4 ^*^* *^*^ ^*^* *^*^

HTH,
Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
Last edited by cable_guy_67 : 23-Mar-2008 at 10:28.
  #4  
Old 23-Mar-2008, 15:56
signorsinatra signorsinatra is offline
New Member
 
Join Date: Mar 2008
Posts: 5
signorsinatra is on a distinguished road

Re: nested for loop issues


Do you know where I can find reserved words to test for line and character position.

I know I will want something like

if (lineposition%2==0 && charposition%2<0)

But I don't know the correct way to call for line position or character position.
  #5  
Old 23-Mar-2008, 16:30
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,102
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough

Re: nested for loop issues


Quote:
Originally Posted by signorsinatra
Do you know where I can find reserved words to test for line and character position.


Here is a perfect example of why not to use non-descriptive variable names. You are very close with your example. Looking at your loops you used i for the first one and j for the second. Since the first loop is the current line and the second loop is your character position on the current line change those variable names then test like in your example.

The outer portion of your loop could become:
Code:
for (int lineposition = 1; lineposition <= n; lineposition ++)

It would be helpful to do the same with the other variables you interact with. Or just use the i and j names in your modulo tests. I personally prefer the longer (to type anyway) but more descriptive names myself.

Quote:
Originally Posted by signorsinatra
I know I will want something like

if (lineposition%2==0 && charposition%2<0)

So this case would be for any even lines (2,4,6...) and odd character positon (1,3,5...)
It looks almost exatly like the 4 in my code exept mine reads,
Code:
else if ( (i%2 == 0)&&(j%2 == 1) ) cout << b;
I like yours better.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #6  
Old 25-Mar-2008, 11:08
C++_Bandit's Avatar
C++_Bandit C++_Bandit is offline
Junior Member
 
Join Date: Mar 2008
Location: Virginia
Posts: 40
C++_Bandit will become famous soon enough

Re: nested for loop issues


Quote:
Originally Posted by Peter_APIIT
No system("pause");

It depends on what compiler you are using. It is required when you are using Dev C++, but not for others.
  #7  
Old 25-Mar-2008, 20:46
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,230
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: nested for loop issues


Quote:
Originally Posted by C++_Bandit
It depends on what compiler you are using. It is required when you are using Dev C++, but not for others.
system("pause") does NOT depend on the compiler. It is NOT required for DevC++. It should not be used. Period.

Edit the DevC++ program template to remove it.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #8  
Old 26-Mar-2008, 11:10
C++_Bandit's Avatar
C++_Bandit C++_Bandit is offline
Junior Member
 
Join Date: Mar 2008
Location: Virginia
Posts: 40
C++_Bandit will become famous soon enough

Re: Nested for loop issues


Walt let's continue this here.
  #9  
Old 28-Mar-2008, 16:18
vers vers is offline
New Member
 
Join Date: Mar 2008
Posts: 5
vers is on a distinguished road

Re: nested for loop issues


was wondering....

how would you write this in code:

IF I am on an EVEN line AND at an ODD char position I want to COUT << B

==> if ((i%2 == 0) && (j%2 == 0) ??
  #10  
Old 29-Mar-2008, 08:47
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,102
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough

Re: nested for loop issues


Quote:
Originally Posted by vers
was wondering....

how would you write this in code:

IF I am on an EVEN line AND at an ODD char position I want to COUT << B

==> if ((i%2 == 0) && (j%2 == 0) ??

Your if condition as written will be true when the line(i) is even (no remainder) and your character position(j) is even (no remainder). Just replace your single output statement in place of the question marks. Since you want the ODD character position you just check for j to be equal to 1 since it is the only thing you can have left over with %2.

If you look back in this thread you will find what you are looking for.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
Last edited by cable_guy_67 : 29-Mar-2008 at 09:37.
 

Recent GIDBlogNew Corolla Altis, 10th Generation - Part I by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows Updates and Secure Website Issues LocalTech Computer Software Forum - Windows 0 31-Jul-2007 04:30
apache 2.2.1 doesn't support nested query ctrohaya Apache Web Server Forum 0 15-Nov-2006 19:46
nested for loops sandeepeecs C Programming Language 3 20-Aug-2006 08:49
Help with nested for loops...please. sorry newbie keperry CPP / C++ Forum 14 16-Oct-2004 11:25
Downloadable document compatibilty issues? rhino1616 Web Design Forum 3 07-May-2003 03:05

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:42.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.