-
functions won't work
before i explain my problem i'll give you what i've got so far
Code:
#include <windows.h>
#include <iostream>
using namespace std;
int question ( string usr_name );
int main()
{
string usr_name;
cout<<"hello enter a name: ";
cin>> usr_name;
cin.ignore();
cout<<"ok so your name is "<< question ( usr_name );
Sleep (20000);
}
int question ( string usr_name )
{
return usr_name;
}
now as you can probably gather i'm trying to make a function out of the code above and i think i've got it but when i compile i get an error on the
Code:
{
return usr_name;
}
part of it, i've scanned tutorials but i can't find my problem or how to solve it, i realize this is probably some basic problem involving a misplaced semicolon or somethiing but i'd appreciate it if someone can help me out with the problem anyhow. thanks in advance
-
int question ( string usr_name )
Well you pass it a string, and try and return an int.
Make the types compatible.
-
eg, make question() return a string.
BTW, you should include <string> if you're going to be using strings.
-
Actually, <iostream> includes <string> for many of it's functions, so it's not really necessary.
-
> Actually, <iostream> includes <string> for many of it's functions, so it's not really necessary.
You mean your iostream includes string.
Other people's compilers may not have the same internal dependencies.
-
>> Actually, <iostream> includes <string> for many of it's functions, so it's not really necessary.
Many people have complained that cout << myString doesn't work when myString is a string and they end up using cout << myString.c_str(). The source of there error is not including <string>. That is because at least one popular compiler library includes only parts of <string> inside <iostream>.
Never rely on one header including another header, always explicitly include any header that you need in your file.