![]() |
|
#1
|
|||
|
|||
Nested for loop issuesHi, 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:
|
|||
|
#2
|
|||
|
|||
Re: nested for loop issuesNo system("pause");
__________________
Linux is the best OS in the world. |
|
#3
|
||||
|
||||
Re: nested for loop issuesQuote:
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:
Code:
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
|
|||
|
|||
Re: nested for loop issuesDo 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
|
||||
|
||||
Re: nested for loop issuesQuote:
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:
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:
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:
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
|
||||
|
||||
Re: nested for loop issuesQuote:
It depends on what compiler you are using. It is required when you are using Dev C++, but not for others. |
|
#7
|
||||
|
||||
Re: nested for loop issuesQuote:
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! |
|
#9
|
|||
|
|||
Re: nested for loop issueswas 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
|
||||
|
||||
Re: nested for loop issuesQuote:
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 GIDBlog
New Corolla Altis, 10th Generation - Part I by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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