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