Changing from pointer to array notation

This is a HW question. I have tried and tried... please tell me what is wrong in the second code. The 1st code is whats given. We are asked to rewrite it using array notation... not pointers. In the second code, after I enter my string, nothing happens. The program just runs and runs. I need it to output the length of saySomething. I have also tried using "strlen" without success Any ideas would be very welcome.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int here(const char *);

int main()
{
char saySomething[65];

cout << "Write something...";
cin >> saySomething;
cout << here(saySomething) << endl;
}
int here(const char *s);
{
int y;
for(y = o; *s != '\0'; ++s);
++y;
return y;
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int here(const char []);

int main()
{
char saySomething[65];

cout << "Write something...";
cin >> saySomething;
cout << here(saySomething) << endl;
}
int here(const char a[]);
{
int y;
for(y = o; a != '\0'; ++a);
++y;
return y;
}
for(y = o; a != '\0'; ++a);
Needs to be:
1
2
int y(0);
for(int i(0); a[i] != '\0'; ++i) // must remove the ; 

You need i to walk thru the array. The ; at the end of the for loop statement meant there was no body to the loop. Not what you want.

EDIT: Don't need i, just use y.
Last edited on
What is the here function supposed to do? (Looks like it's supposed to count characters, but the random ++y; on line 18 is throwing me off)
And are you sure the first code is what's given to you?
There are some errors with it:
14
15
16
17
18
19
20
21
int here(const char *s); // Extra semicolon
{
    int y;
    for(y = o; *s != '\0'; ++s); // o instead of 0, *s instead of *(s+y),
                                 // ++s instead of ++y, extra semicolon at the end
    ++y;
    return y;
}


If you want to translate that literally into array notation, it would be
14
15
16
17
18
19
20
21
int here(const char s[]); // Extra semicolon
{
    int y;
    for(y = o; s[0] != '\0'; ++s); // o instead of 0, s[0] instead of s[y],
                                   // ++s instead of ++y, extra semicolon at the end
    ++y;
    return y;
}

Either way, neither of the two will actually compile. (But I've put in comments on what's wrong with the code)

If you're trying to count characters, the way to do it is
1
2
3
4
int y;
for (y = 0; s[y] != '\0'; ++y) // The ++y here takes care of the counting
{} // Nothing to do here
return y;


In contrast, the following:
1
2
3
4
int y;
for (y = 0; s[y] != '\0'; ++y)
    ++y;
return y;

will skip over every other character and increment y twice for each character that it doesn't skip over.
If s has an odd number of characters, then it'll skip over the terminating null character and venture off into no man's land and (possibly) do crazy stuff.
Last edited on
Thank You!

Yes, the code is supposed to count the characters.

for the 1st code I had a typo with the ; after the for statement and again the the ++s vs ++a.

The best part of the answer was the need for a[i] in the second code... thats what I've been missing. Thanks again for your help. Both codes now run as intended!!!!
Topic archived. No new replies allowed.