| | |
Printing array of characters backwards
![]() |
•
•
Join Date: Apr 2008
Posts: 108
Reputation:
Solved Threads: 1
I need to print the contents of an array backwards, but every time I try it just prints a a smiley face, or something like that.
Here's the code, it isin three files:
Here's the code, it isin three files:
C++ Syntax (Toggle Plain Text)
main file: #include <iostream> #include "testArray.cpp" using namespace std; int main() { testArray test; char ch; cout << "Enter a sentence:" << endl; ch = cin.get(); while(ch != '\n') { test.store_char(ch); ch = cin.get(); } cout << "Number of vowels: " << test.vowels() << endl; cout << "Number of digits: " << test.digits() << endl; //cout << "Number of spaces: " << test.spaces() << endl; cout << "Expression backwards: " << test.backwards() << endl; system("pause"); } testArray.cpp: #include <iostream> #include "Array.cpp" using namespace std; class testArray:public Array { protected: int numVowels; int numDigits; int numOther; //int numSpaces; char wordEntered; char numEntered; //char spaceEntered; public: int vowels(); int digits(); int spaces(); int other(); char backwards(); }; int testArray::vowels() { numVowels = 0; for(int i = 0; i < return_size(); i++) { wordEntered = tolower(return_char(i)); switch(wordEntered) { case 'a': case 'e': case 'i': case 'o': case 'u': numVowels++; break; default: numOther++; } } return numVowels; } int testArray::digits() { numDigits = 0; for(int i = 0; i < return_size(); i++) { numEntered = return_char(i); if(isdigit(numEntered)) { numDigits++; numOther--; } } return numDigits; } /*int testArray::spaces() { numSpaces = 0; for(int i = 0; i < return_size(); i++) { spaceEntered = return_char(i); if(spaceEntered == ' ') { numSpaces++; numOther--; } }*/ //return numSpaces; //} int testArray::other() { return numOther; } char testArray::backwards() { for(int i = return_size(); i > 0; i--) cout << return_char(i) << endl; } Array.cpp: #include <iostream> using namespace std; class Array { protected: char word[50]; int size; int i; char theWord; public: Array(); void store_char(char); char return_char(int); int return_size(void); }; Array::Array() { int i; for(i = 0; i < 50; i++) word[i] = 0; } void Array::store_char(char userEnter) { size++; word[i] = userEnter; i++; } char Array::return_char(int num) { theWord = word[num]; return theWord; } int Array::return_size() { return size; }
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
Well .. I have no idea why your teacher would say that, but its really bad practice.
Now onto your code. Surprised it didn't crash. Your store_char() function does not have i initialized, and neither does your constructor intialize the size variable to 0.
You may want to do something like this. You also need a check for if a user enters a string longer than 50 bytes.
You could also replace "50" with a macro "#define MAX_SIZE 50" , and then use MAX_SIZE in your code. Its better practice and if you decided to change it to 60, then you would just have to make the change at one place.
Now onto your code. Surprised it didn't crash. Your store_char() function does not have i initialized, and neither does your constructor intialize the size variable to 0.
You may want to do something like this. You also need a check for if a user enters a string longer than 50 bytes.
You could also replace "50" with a macro "#define MAX_SIZE 50" , and then use MAX_SIZE in your code. Its better practice and if you decided to change it to 60, then you would just have to make the change at one place.
Array::Array()
{
int i;
for(i = 0; i < 50; i++)
word[i] = 0;
size = 0;
}
void Array::store_char(char userEnter)
{
while (size < 50){
word[size++] = userEnter;
}
} Last edited by stilllearning; Oct 22nd, 2008 at 1:36 am.
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
strange. it works for me. here is my output.
I am not sure how your code works, since you have neither "i" nor "size" initialized in your constructor. Also its probably not a good idea using a local variable "i" in your constructor when your class also has a data member called "i". Have you printed the contents of your array after you add the characters to it to see that you have the right elements ?
Enter a sentence: I have 200 apples Number of vowels: 5 Number of digits: 3 s e l p p a 0 0 2 e v a h Press any key to continue . . .
I am not sure how your code works, since you have neither "i" nor "size" initialized in your constructor. Also its probably not a good idea using a local variable "i" in your constructor when your class also has a data member called "i". Have you printed the contents of your array after you add the characters to it to see that you have the right elements ?
Still learning you still seem to have the problem of the fact the first character is not printed. This is because in your loop to print it out backwards you are checking to see if it is > than 0 baring in mind that arrays are 0 index'ed that means that it should be => 0.
from OP you said you were getting smiley faces well thats because you are accessing uninitialized areas of memory.
Also as far as header files stand. I would advise you do not take your teachers advise in the fact you should use them because he doesn't like them.
I would use them anyway, its good practice too.
Chris
from OP you said you were getting smiley faces well thats because you are accessing uninitialized areas of memory.
Also as far as header files stand. I would advise you do not take your teachers advise in the fact you should use them because he doesn't like them.
I would use them anyway, its good practice too.
Chris
Knowledge is power -- But experience is everything
![]() |
Other Threads in the C++ Forum
- Previous Thread: program calculator
- Next Thread: How to remove the error Spawning 'link.exe' and 'cl.exe'
Views: 1856 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment beginner binary borland browser c++ c/c++ calculator char class classes code command compile compiler constructor conversion convert count delete desktop dll dynamic encryption error file files fstream function functions game givemetehcodez graph gui homework http i/o iamthwee ifstream input int java lazy lib library linker list loop looping loops math matrix memory newbie news number object objects opengl output path pointer pointers problem program programming project random read reading recursion recursive reference simple sort sorting spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual win32 window windows winsock





