I do not know, what's wrong with the code below: It simply gives me=
=20
compilation error. The compilation log is as follows:
D:/Documents/C_Notes/try.cpp: In function `int main()':
D:/Documents/C_Notes/try.cpp:11: non-lvalue in increment
The offending line is "if (*inp++ =3D=3D '?') count++;" But the funn=
y thing is=20
if I substitute p for inp in the while loop, then it compiles and giv=
es me=20
the right answer.
I am under the impression that the post and pre increment operators c=
an be=20
used on the names of char arrays. Somebody, please clear me on this =
and=20
give me some plausible explanation.
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
char inp[] =3D "?cmewop? OWCOIJJJ?mcjmwow? cwjw?xjw?";
char* p =3D inp;
int count =3D 0;
while (*inp)
if (*inp++ =3D=3D '?') count++;
cout<<count<<'\n';
system("PAUSE");
return 0;
}
|