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!

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.

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.

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.

commented: She got it! Thanks. +1

Thank you! I think this is what he's looking for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.