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:

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;
}

Recommended Answers

All 6 Replies

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.

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.

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;         
 }
}

if I make those changes, nothing works. With the way I have it(and I finished Array::spaces()) everything works except backwards

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 ?

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

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.