Printing array of characters backwards

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

Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Printing array of characters backwards

 
0
  #1
Oct 22nd, 2008
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:

  1. main file:
  2. #include <iostream>
  3. #include "testArray.cpp"
  4.  
  5. using namespace std;
  6.  
  7. int
  8. main()
  9. {
  10.  
  11. testArray test;
  12. char ch;
  13.  
  14. cout << "Enter a sentence:" << endl;
  15.  
  16. ch = cin.get();
  17.  
  18. while(ch != '\n')
  19. {
  20. test.store_char(ch);
  21. ch = cin.get();
  22. }
  23.  
  24. cout << "Number of vowels: " << test.vowels() << endl;
  25.  
  26. cout << "Number of digits: " << test.digits() << endl;
  27.  
  28. //cout << "Number of spaces: " << test.spaces() << endl;
  29.  
  30. cout << "Expression backwards: " << test.backwards() << endl;
  31.  
  32. system("pause");
  33. }
  34.  
  35. testArray.cpp:
  36. #include <iostream>
  37. #include "Array.cpp"
  38. using namespace std;
  39.  
  40. class testArray:public Array
  41. {
  42. protected:
  43. int numVowels;
  44. int numDigits;
  45. int numOther;
  46. //int numSpaces;
  47. char wordEntered;
  48. char numEntered;
  49. //char spaceEntered;
  50. public:
  51. int vowels();
  52. int digits();
  53. int spaces();
  54. int other();
  55. char backwards();
  56. };
  57.  
  58. int
  59. testArray::vowels()
  60. {
  61. numVowels = 0;
  62.  
  63. for(int i = 0; i < return_size(); i++)
  64. {
  65. wordEntered = tolower(return_char(i));
  66. switch(wordEntered)
  67. {
  68. case 'a':
  69. case 'e':
  70. case 'i':
  71. case 'o':
  72. case 'u':
  73. numVowels++;
  74. break;
  75. default:
  76. numOther++;
  77. }
  78. }
  79. return numVowels;
  80.  
  81. }
  82.  
  83. int
  84. testArray::digits()
  85. {
  86. numDigits = 0;
  87. for(int i = 0; i < return_size(); i++)
  88. {
  89. numEntered = return_char(i);
  90. if(isdigit(numEntered))
  91. {
  92. numDigits++;
  93. numOther--;
  94. }
  95. }
  96. return numDigits;
  97. }
  98.  
  99. /*int
  100. testArray::spaces()
  101. {
  102.  numSpaces = 0;
  103.  for(int i = 0; i < return_size(); i++)
  104.  {
  105.   spaceEntered = return_char(i);
  106.   if(spaceEntered == ' ')
  107.   {
  108.   numSpaces++;
  109.   numOther--;
  110.   }
  111.  }*/
  112.  
  113. //return numSpaces;
  114. //}
  115.  
  116. int
  117. testArray::other()
  118. {
  119. return numOther;
  120. }
  121.  
  122. char
  123. testArray::backwards()
  124. {
  125. for(int i = return_size(); i > 0; i--)
  126. cout << return_char(i) << endl;
  127. }
  128.  
  129. Array.cpp:
  130. #include <iostream>
  131.  
  132. using namespace std;
  133.  
  134. class Array
  135. {
  136. protected:
  137. char word[50];
  138. int size;
  139. int i;
  140. char theWord;
  141. public:
  142. Array();
  143. void store_char(char);
  144. char return_char(int);
  145. int return_size(void);
  146. };
  147.  
  148. Array::Array()
  149. {
  150. int i;
  151. for(i = 0; i < 50; i++)
  152. word[i] = 0;
  153. }
  154.  
  155. void
  156. Array::store_char(char userEnter)
  157. {
  158. size++;
  159. word[i] = userEnter;
  160. i++;
  161. }
  162.  
  163. char
  164. Array::return_char(int num)
  165. {
  166. theWord = word[num];
  167. return theWord;
  168. }
  169.  
  170. int
  171. Array::return_size()
  172. {
  173. return size;
  174. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Printing array of characters backwards

 
0
  #2
Oct 22nd, 2008
First of all, its good practice is to put your class declarations in a header or a .h file, put your definitions in a .cpp file and then include the .h file, where needed.

You really should not be including your .cpp files in other files. The rest, coming in a bit.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Re: Printing array of characters backwards

 
0
  #3
Oct 22nd, 2008
sorry thats how my teacher told me to do it...i dont think he likes header files, i used to use them in my last class but I think he said he doesnt like them.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Printing array of characters backwards

 
0
  #4
Oct 22nd, 2008
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.

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 2:36 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 108
Reputation: christiangirl is an unknown quantity at this point 
Solved Threads: 1
christiangirl christiangirl is offline Offline
Junior Poster

Re: Printing array of characters backwards

 
0
  #5
Oct 22nd, 2008
if I make those changes, nothing works. With the way I have it(and I finished Array::spaces()) everything works except backwards
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Printing array of characters backwards

 
0
  #6
Oct 22nd, 2008
strange. it works for me. here is my output.

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 ?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Printing array of characters backwards

 
0
  #7
Oct 22nd, 2008
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
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC