Beginner: How store sentences in a variable?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • EhYay
    New Member
    • Oct 2022
    • 6

    Beginner: How store sentences in a variable?

    How do you store whole sentences in a variable?

    I want to print out sentences using variables. I tested with a 'string' data type and stored individual sentences as an element then used a 'for' function to print them with a delay between each sentence. The result is an error, upon building it, saying
    "invalid conversion from 'const char*' to 'char'
    at this line:
    t1[3] = {"Hello!", "This is a test for my program.", "\nI hope you're havin fun reading this!"};
    Full code:
    Code:
    #include <iostream>
    #include <string>
    #include <windows.h>
    
    // A function to make text that'll play automatically.
    void playText() {
    	using namespace std;
    	string t1[3];
    	t1[3] = {"Hello!", "This is a test for my program.", "\nI hope you're havin fun reading this!"};
    
    	for(int i = 0; i < 3; i++) {
    	cout << t1[i];
    	Sleep(1000);
    	}
    }
    
    int main() {
    	using namespace std;
    	cout << "Starting program...";
    	playText();
    	cout << "Done!";
    	return 0;
    }
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    Code:
    t1[3] = {"Hello!", "This is a test for my program.", "\nI hope you're havin fun reading this!"};
    Code:
    string t1[3] = {"Hello!", "This is a test for my program.", "\nI hope you're having fun reading this!"};
    or
    Code:
    string t1[3];
    t1[0] = "Hello!";
    t1[1] = "This is a test for my program.";
    t1[2] = "\nI hope you're having fun reading this!";

    Comment

    Working...