943,882 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 973
  • C++ RSS
Nov 3rd, 2008
0

Code w/in for loop to display only certain characters

Expand Post »
Hey there, everyone.

Here is the question I had to write code for:

Include the following code:
char test[15] = {‘T’, ‘h’, ‘i’,’s’, ‘_’, ’i’, ’s’, ‘_’, ’a’, ‘_’, ’t’, ’e’, ’s’, ’t’};
for (int row = 0; row < 15; row++)
{ cout << test[row]; }

Add the code within the for loop to display only the characters t, e, and s using if/else or case statements.


Here is what I turned in:

#include <iostream>
using namespace std;

void main()
{
char test[15] = {'T', 'h', 'i','s', '_', 'i', 's', '_', 'a', '_', 't', 'e', 's', 't'};
int row;

cout << "Characters 10-12 are:"<<endl;
for (char row = 0; row <15; row++)
{
if(row > 9)
if (row < 13)
{ cout << test[row]; }
else;
}
cout << "\n" <<endl;
}


I thought this was good. But it was handed back to me to redo. My teacher said he wants to see me do this using the exact character letter. I guess meaning by doing 't' 'e' 's'. I am confused as to how to re-write the code this way. Any help is appreciated. Thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cherryteresa is offline Offline
17 posts
since Dec 2007
Nov 3rd, 2008
0

Re: Code w/in for loop to display only certain characters

First of all, don't use void main, theres just no need, and main always returns int.
If I understood correctly, I think your teacher wants you to make a simple loop to display the characters 10-12, without cycling through the rest.
#include <iostream>
using namespace std;

int main() {
   char test[15] = {'T', 'h', 'i','s', '_', 'i', 's', '_', 'a', '_', 't', 'e', 's', 't'};
   int row;

   cout << "Characters 10-12 are:"<<endl;
   for (char row = 9; row < 13; row++) {
      cout << test[row];
   }
   cout << "\n" <<endl;
}
Hope this helps.
Last edited by William Hemsworth; Nov 3rd, 2008 at 8:15 pm.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Nov 3rd, 2008
0

Re: Code w/in for loop to display only certain characters

To me the problem boils down to how many of each of the 3 char are there in the array. I find:
1 e
2 t
3 s
So I'd loop through the array looking at each char and see if it equals one of those three values displaying each as found so the output looks something like:

sstest

if the array is searched from lowest index to highest. Note, the index of the array should be an int, not a char.

In addition, beware, the array is declared with capacity of 15, but there are only 14 char in the array if I've counted correctly. That means if you try accessing the last element in the array it will be junk, and who know what value that will be.
Last edited by Lerner; Nov 3rd, 2008 at 9:37 pm.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 3rd, 2008
1

Re: Code w/in for loop to display only certain characters

No I don't think that's what her teacher meant. By doing what you did there, you are "hardcoding" this. That is not a good practice. Suppose he wanted to change the char array from "This is a test" to some other phrase, say "i'm too cool for a test". Now, using the same code that you have there, you will not print out 't's or 's's or 'e' at all. You are printing out what is stored in test[9]~test[13]. What he wants you, is to cycle through the array from 0 to however long and with for loop, you check if the letter is either 't' 's' or 'e' ever iteration.'

So you'd want something like
for(int i = 0; i < 15; i++)
{
   if ((array[i]=='t')||(array[i]=='e')||(array[i]=='s'))
   {
       cout << array[i];
    }
}

now it should print out ever letter that are either e s or t.
Last edited by freudian_slip; Nov 3rd, 2008 at 9:38 pm. Reason: oops
Reputation Points: 11
Solved Threads: 4
Light Poster
freudian_slip is offline Offline
36 posts
since Oct 2008
Nov 3rd, 2008
0

Re: Code w/in for loop to display only certain characters

Thank you! I think this is what he's looking for.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cherryteresa is offline Offline
17 posts
since Dec 2007
Nov 3rd, 2008
0

Re: Code w/in for loop to display only certain characters

Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cherryteresa is offline Offline
17 posts
since Dec 2007
Nov 3rd, 2008
0

Re: Code w/in for loop to display only certain characters

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cherryteresa is offline Offline
17 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: is this correct?
Next Thread in C++ Forum Timeline: Processing wav files





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC