declaring string

Thread Starter

TAKYMOUNIR

Joined Jun 23, 2008
352
this program i see in text book and i try to compile it but it does not work ,it does not except string ,i get error say that string not declared so what is the wrong i use gcc compiler
Rich (BB code):
#include <stdio.h>
#include<string.h>
int main( void )
{
string s[4] = {"hello", "help", "save me", "groan"};
char t[] = "help";
string p = s[3];
printf( "%4s?\n", s[0] );
printf( "%s! ", p );
printf( "%10s ", s[2] );
printf( "%i", t == s[1] );
printf( "%c!\n", s[0][0] );
}
 

tshuck

Joined Oct 18, 2012
3,534
this program i see in text book and i try to compile it but it does not work ,it does not except string ,i get error say that string not declared so what is the wrong i use gcc compiler
Rich (BB code):
#include <stdio.h>
#include<string.h>
int main( void )
{
string s[4] = {"hello", "help", "save me", "groan"};
char t[] = "help";
string p = s[3];
printf( "%4s?\n", s[0] );
printf( "%s! ", p );
printf( "%10s ", s[2] );
printf( "%i", t == s[1] );
printf( "%c!\n", s[0][0] );
}
You need to specify what namespace 'string' lies in... either write 'std::string' for each instance of string, or 'using namepsace std;' toward the top outside of main()..... also, depending on your compiler, you may get an error in printing a string with printf(I'm not familiar with gcc...), you may need to convert the string to a char*, you can use s.c_str() to convert to a const char*...
 

takao21203

Joined Apr 28, 2012
3,702
string p = s[3];

I don't think this can work.
You need to use some kind of strcpy.

It is really called strcpy but of course, does string copy.
 

spinnaker

Joined Oct 29, 2009
7,830
You need to specify what namespace 'string' lies in... either write 'std::string' for each instance of string, or 'using namepsace std;' toward the top outside of main()..... also, depending on your compiler, you may get an error in printing a string with printf(I'm not familiar with gcc...), you may need to convert the string to a char*, you can use s.c_str() to convert to a const char*...

Also the wrong include file at the top

#include <string>

not


#include <string.h>
 

WBahn

Joined Mar 31, 2012
30,051
this program i see in text book and i try to compile it but it does not work ,it does not except string ,i get error say that string not declared so what is the wrong i use gcc compiler
Rich (BB code):
#include <stdio.h>
#include<string.h>
int main( void )
{
string s[4] = {"hello", "help", "save me", "groan"};
char t[] = "help";
string p = s[3];
printf( "%4s?\n", s[0] );
printf( "%s! ", p );
printf( "%10s ", s[2] );
printf( "%i", t == s[1] );
printf( "%c!\n", s[0][0] );
}
I don't see anything that states whether this is C or C++. What is the extension on your source code file, .c or .cpp? Some compilers (don't know about gcc) use this to determine whether to assume it is C or C++ and configure the compiler options accordingly.

If it is being compiled as a C program, the there is not variable type called "string".
 
Top