invalid types of char int for array subscript
Nov 16, 2013 at 2:26am UTC
I'm supposed to write a program that if you type a phrase, it outputs the letters used in alphabetical order.
Example: I say hi
it would output
1 a
1 h
2 i
1 s
1 y
When I run the code, it gives me 3 errors of the same message.
invalid types of char int for array subscript.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <iostream>
#include <cstring>
using namespace std;
void input(char phrase);
void alphabetical(int letters[], char phrase);
void output(int letters[]);
int main()
{
char phrase;
int letters[26];
input(phrase);
alphabetical(letters, phrase);
output(letters);
}
void input(char phrase)
{
cout << "Enter anything that you want to write." << endl;
cin.get(phrase);
}
void alphabetical(int letters[], char phrase)
{
int i = 0, count = 1;
for (i=0; i <26; i++)
letters[i] = 0;
while (phrase[i] != '\0' )
{
if (phrase[i] == ' ' )
count ++;
letters[tolower(phrase[i]-97]++;
i++;
}
}
void output(int letters[])
{
int i =0, count = 1;
cout << count << " words" << endl;
for (i=0; i<26; i++)
{
if (letters[i]>0)
cout << letters[i] << " " << static_cast <char >(i+97) << endl;
}
}
Nov 16, 2013 at 10:31am UTC
phrase isn't an array so phrase[i] is illegal
Last edited on Nov 16, 2013 at 10:32am UTC
Topic archived. No new replies allowed.