| | |
Code w/in for loop to display only certain characters
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2007
Posts: 17
Reputation:
Solved Threads: 0
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!
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!
•
•
Join Date: Mar 2008
Posts: 1,411
Reputation:
Solved Threads: 114
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. Hope this helps.
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; }
Last edited by William Hemsworth; Nov 3rd, 2008 at 8:15 pm.
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 263
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.
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
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
now it should print out ever letter that are either e s or t.
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
![]() |
Similar Threads
- drawing a rectangle (C++)
- misplaced else and removing space characters (C++)
- I Need Assembly help w/reversing strings (Assembly)
- I think Loop problem (Java)
- character count (C++)
- Need Help in Reading characters from a text file (C++)
- arrays and functions (C++)
Other Threads in the C++ Forum
- Previous Thread: is this correct?
- Next Thread: Processing wav files
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






