What you did was declare an array of strings. You might want to use an array of characters instead.
Also, arrays cannot be dynamically allocated on runtime. You must declare an array of a certain size that so that the space can be allocated during compile time. EDIT: You can declare them dynamically, but you are better off using vectors for this, as it performs the memory allocation for you. You can get memory leaks if you forget to release memory. You can also partially-filled arrays.
You also have no return statement at the end of your main.
Try this instead
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <iostream>
#include <string>
#include <unistd.h>
using namespace std;
const int MAX(100);
int main()
{
cout<<"Input some text:"<<endl;
cout<<"It will be sorted into seperate characters!"<<endl;
string raw_input;
cin >> raw_input;
int input_length = raw_input.length();
char chars[MAX];
for (int i = 0; i < input_length; i++)
{
chars[i] = raw_input[i];
cout << chars[i] << endl;
}
string quit;
cin>>quit;
return 0;
}
| |
Now we're using a global constant so that the memory can be allocated for the array properly during compilation, and using an array of characters instead of an array of strings.
If you want to be able to output more characters one-by-one, simply change the value of the MAX constant.
I tested this, and this was the output:
Input some text:
It will be sorted into seperate characters!
hello
h
e
l
l
o
quit // here i just entered the word "quit" as the quit string and the program exited
Program ended with exit code: 0