From: cozofdeath on
I trying to learn c++ and have been creating test programs to get some
practice. This one I can't figure out and I would appreciate any help.
The subject of this message is the error I'm getting and I get it for
all array[i]'s in the leetspeak function. I haven't gone further with
this because of this problem, but any suggestions are very welcome.
Thanks for any help. btw I'm using Dev-c++

#include <cstdlib>
#include <iostream>

using namespace std;
void leetspeak(char &array);

int main(int argc, char *argv[])
{
char line[100];
char &array=line[100];

cout << "Enter a sentence: ";
cin.getline(line, 100);

leetspeak(line[100]);

cout << "\nElite speak translation...\n";
cout << "line << "\n";

system("PAUSE");
}

void leetspeak(char &array)
{
int i;

for(i=0;array[i]!='\0';i++)
{
switch(array[i]);
{
case 'A':
array[i]=4;
break;
case 'E':
array[i]=3;
break;
case 'I':
array[i]=1;
break;
case 'O':
array[i]=0;
break;
case 'S':
array[i]=5;
break;
}
}
}

From: James Dennett on
cozofdeath wrote:
> I trying to learn c++ and have been creating test programs to get some
> practice. This one I can't figure out and I would appreciate any help.
> The subject of this message is the error I'm getting and I get it for
> all array[i]'s in the leetspeak function. I haven't gone further with
> this because of this problem, but any suggestions are very welcome.
> Thanks for any help. btw I'm using Dev-c++
>
> #include <cstdlib>
> #include <iostream>
>
> using namespace std;
> void leetspeak(char &array);
>
> int main(int argc, char *argv[])
> {
> char line[100];

Here you declare that "line" is an array of 100 elements,
each of type char.

> char &array=line[100];

This is invalid; it declares a misleadingly named reference
called "array", which is not an array -- it would be a
reference to (i.e., an alias for) the 101-st element of
the array "line", except that line has only 100 elements,
so line[99] is the last.

It's not at all clear what you were trying to achieve with
the declaration of "array".

You'd be better to use std::string objects.

std::string line; // no artificial limit on line length.

>
> cout << "Enter a sentence: ";
> cin.getline(line, 100);

Would become
std::getline(std::cin, line);

> leetspeak(line[100]);
>
> cout << "\nElite speak translation...\n";
> cout << "line << "\n";
>
> system("PAUSE");
> }
>
> void leetspeak(char &array)

So "leetspeak" takes as its argument a single character, by
reference. Probably better to change it to

void leetspeak(std::string & text)

so that it takes a whole string of characters

> {
> int i;
>
> for(i=0;array[i]!='\0';i++)

array isn't an array or a pointer, and it doesn't support
indexing, so array[i] is a syntax error.

> {
> switch(array[i]);

The semi-colon there does nothing except disturb the eye of
the reader, incidentally.

> {
> case 'A':
> array[i]=4;
> break;

I think you mean '4', not 4.

-- James
From: Jim Langston on

"cozofdeath" <mpmpl(a)hotmail.com> wrote in message
news:1165190261.511145.319700(a)16g2000cwy.googlegroups.com...
>I trying to learn c++ and have been creating test programs to get some
> practice. This one I can't figure out and I would appreciate any help.
> The subject of this message is the error I'm getting and I get it for
> all array[i]'s in the leetspeak function. I haven't gone further with
> this because of this problem, but any suggestions are very welcome.
> Thanks for any help. btw I'm using Dev-c++
>
> #include <cstdlib>
> #include <iostream>
>
> using namespace std;
> void leetspeak(char &array);
>
> int main(int argc, char *argv[])
> {
> char line[100];
> char &array=line[100];

As someone else pointed out, this is fairly meaningless. You are creating a
reference to a single character, and then pointing it to a character that
your program doessn't have access too (array overflow)

> cout << "Enter a sentence: ";
> cin.getline(line, 100);
>
> leetspeak(line[100]);
>
> cout << "\nElite speak translation...\n";
> cout << "line << "\n";
>
> system("PAUSE");
> }
>
> void leetspeak(char &array)

void leetspeak( char*& array )
is probably what you want here. A reference to a character pointer.
char[]& array *may* be legal syntax, or it may not, I'm just not sure and
too lazy to test right now

> {
> int i;
>
> for(i=0;array[i]!='\0';i++)
> {
> switch(array[i]);
> {
> case 'A':
> array[i]=4;
> break;
> case 'E':
> array[i]=3;
> break;
> case 'I':
> array[i]=1;
> break;
> case 'O':
> array[i]=0;
> break;
> case 'S':
> array[i]=5;
> break;
> }
> }
> }

As pointed out by others, std::string is better.


From: James Dennett on
Jim Langston wrote:
> "cozofdeath" <mpmpl(a)hotmail.com> wrote in message
> news:1165190261.511145.319700(a)16g2000cwy.googlegroups.com...
>> I trying to learn c++ and have been creating test programs to get some
>> practice. This one I can't figure out and I would appreciate any help.
>> The subject of this message is the error I'm getting and I get it for
>> all array[i]'s in the leetspeak function. I haven't gone further with
>> this because of this problem, but any suggestions are very welcome.
>> Thanks for any help. btw I'm using Dev-c++
>>
>> #include <cstdlib>
>> #include <iostream>
>>
>> using namespace std;
>> void leetspeak(char &array);
>>
>> int main(int argc, char *argv[])
>> {
>> char line[100];
>> char &array=line[100];
>
> As someone else pointed out, this is fairly meaningless. You are creating a
> reference to a single character, and then pointing it to a character that
> your program doessn't have access too (array overflow)
>
>> cout << "Enter a sentence: ";
>> cin.getline(line, 100);
>>
>> leetspeak(line[100]);
>>
>> cout << "\nElite speak translation...\n";
>> cout << "line << "\n";
>>
>> system("PAUSE");
>> }
>>
>> void leetspeak(char &array)
>
> void leetspeak( char*& array )
> is probably what you want here. A reference to a character pointer.

Why would that be a good choice? Do you think the code wishes
to alter the char*?

> char[]& array *may* be legal syntax, or it may not, I'm just not sure and
> too lazy to test right now

It's not, though
void leetspeak( char (&array)[100] )
would be a valid way to declare that the parameter array
is a reference to an array of exactly 100 characters. Such
nameless ("magical") constants are usually a sign of a problem
though.

-- James
From: cozofdeath on
Thank you both for your suggestions and answers. Although I don't truly
understand why the get line would become std::getline(std::cin, line);
you've helped me out greatly.Thanks again.