Hello,
[quoted text, click to view] > Hi I'm trying to print "Spiderman is amazing" twice but on the second
> line, it is giving me some weird symbols. Can someone please help me
> fix this problem... Thanks...
Try this one:
// code start
#include <iostream>
#include <string>
int main()
{
std::string mynameis = "Spiderman is amazing";
std::string iamnoname = mynameis;
std::cout << mynameis << std::endl;
std::cout << iamnoname << std::endl;
return 0;
}
// code end
and some comments from my side:
[quoted text, click to view] > #include <iostream.h>
<iostream.h> does not exist in C++. If it works, it is just for backward
compatibility. Use instead <iostream>. Bear in mind that most functions,
types, definitions,... are then embedded in the "std" namespace.
Try to avoid a global "using namespace std" which may result in conflicts.
[quoted text, click to view] > main()
should be "int main()"
[quoted text, click to view] > {
> char mynameis[]="Spiderman is amazing";
> char string[25];
string is not a nice name here, because string is a class defined in the C++
Standard. You may use it, but it may be confusing.
[quoted text, click to view] > char *ptr=mynameis;
> char *p=string;
> for(int i=0; mynameis[i] !='\0'; i++)
Just for the habits. There is no difference in the loop when you use i++ for
built-in types but the post-increment calls the pre-increment in many
classes. So, just use always ++i in loops and you are fine.
Regards,
Patrick