Code w/in for loop to display only certain characters

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 17
Reputation: cherryteresa is an unknown quantity at this point 
Solved Threads: 0
cherryteresa cherryteresa is offline Offline
Newbie Poster

Code w/in for loop to display only certain characters

 
0
  #1
Nov 3rd, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,411
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 114
Sponsor
William Hemsworth William Hemsworth is offline Offline
Nearly a Posting Virtuoso

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

 
0
  #2
Nov 3rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 263
Lerner Lerner is offline Offline
Posting Virtuoso

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

 
0
  #3
Nov 3rd, 2008
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.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 36
Reputation: freudian_slip is an unknown quantity at this point 
Solved Threads: 4
freudian_slip's Avatar
freudian_slip freudian_slip is offline Offline
Light Poster

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

 
1
  #4
Nov 3rd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 17
Reputation: cherryteresa is an unknown quantity at this point 
Solved Threads: 0
cherryteresa cherryteresa is offline Offline
Newbie Poster

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

 
0
  #5
Nov 3rd, 2008
Thank you! I think this is what he's looking for.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 17
Reputation: cherryteresa is an unknown quantity at this point 
Solved Threads: 0
cherryteresa cherryteresa is offline Offline
Newbie Poster

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

 
0
  #6
Nov 3rd, 2008
Thanks.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 17
Reputation: cherryteresa is an unknown quantity at this point 
Solved Threads: 0
cherryteresa cherryteresa is offline Offline
Newbie Poster

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

 
0
  #7
Nov 3rd, 2008
Thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC