In this code- #include <iostream>
#include<cstdlib>
#include<cstring>
#include<conio.h>
int main()
{
std::string a="";
char c;
for(int i=0;i<1000;i++)
{
c=getch();
if(c=='\r')
break;
std::cout<<"*";
a[i]=c;
}
int d=0;
d=a.length();
std::cout<<d;
std::cout<<a;
return 0;
}
The value of d is always 0, and the 'std::cout<<a' code gives no output. Why? Please help!
Thanks, it works. But what confusing me is that, in my wrong method, if I enter a string, say "abcd" and we compare it with another string b(std::string b="abcd") by->a==b, i am actually getting a return value-0, which means that the strings are same. But as I said, when I am printing a i get nothing. How is that happening?
What?????..... but in Turbo C++ a return value of 0 meant that the arrays are same. I didn't know that in the GCC compiler it's just the opposite! Thanks for enlightning me @Smac89 :-D
The return value of operator == has type bool. It is true when they are equal and it is false when they are not equal. If you convert bool to int (such as by printing it without boolalpha), false becomes 0 and true becomes 1. This is the same in any compiler.
Yup, just found out that strcmp() returns 0 when the strings are same. That's why I thought that similiarity returns a value '0'. Its different from the '=='. Thanks guys!!!