String char

closed account (EwCjE3v7)
How can i get user to type a sentence for ex

1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;

int main()
{
    int char = 'c';

    return 0;
}

like u can do that but i would like to do this
1
2
3
4
5
6
7
8
9
#include <iostream>
using namespace std;

int main()
{
    int char = "char";

    return 0;
}


What is the type for a string?

I know its a stupid question
Last edited on
Try std::string
http://www.cplusplus.com/reference/string/string/

The constructor with some examples on how to make strings can be found here:
http://www.cplusplus.com/reference/string/string/string/

If you have using namespace std; you do not need to type std:: in front of anything.
Last edited on
Also, be aware that strings will be terminated at the first whitespace. So string sOne = "String"
would be ok, but
string sOne = "String String"
will only print "String"
@undeadsoldier Are you thinking about when reading a string using operator>>? Because std::string can store whitespaces just fine.
Last edited on
closed account (28poGNh0)
I think you're looking for something like that

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# include <iostream>
using namespace std;

int main()
{
    char *firstString = "I am the first string";//Gives you a warning(deprecated)
    char secondString[] = "I am the second string";
    char thirdString[30] = "I am the thid string";
    const char *fourthString = "I am the fouth string but special one you cannot change me";
    string fifthString = "I am the fifth string of";

    cout << firstString << endl;
    cout << secondString << endl;
    cout << thirdString << endl;
    cout << fourthString << endl;
    cout << fifthString << endl;

    return 0;
}


Hope that helps
closed account (EwCjE3v7)
Thanks guys I got it now
Hey Techno01. Can you explain the differences between all of those? What makes one unique from the other? What does each one do?
Topic archived. No new replies allowed.