What's wrong with my code?

My program compiles successfully, my compiler is g++ 4.8.1 on Linux Mint 16. The compiler throws off no errors, but the program doesn't do what it should. The program uses a for loop to empty a string to an array in characters, separating the string into characters and then emptying each one into an array. It should then print the characters separately and wait for input to exit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <unistd.h>
using namespace std;
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();
	string chars[input_length];
	int i=0; 
	for (i<input_length; i++;)
	{
		chars[i]=raw_input[i];
		cout<<chars[i]<<endl;
	}
	string quit;
	cin>>quit;
	
}
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
Last edited on
Thanks for the answer, but I was just going through cplusplus.com's doc on arrays and read this:

The elements field within square brackets [], representing the number of elements in the array, must be a constant expression, since arrays are blocks of static memory whose size must be determined at compile time, before the program runs.

It seems that you confirm that, but why use characters not strings? Sorry, I've only been trying C++ for a couple of months now!
Using an array of characters was the old way of using strings (in C). It's also sometimes called a "C-string". When you use a C-String, each element of the array is the corresponding letter of the string. If "Hello" was input into a char array, the array would look like this: {'H', 'e', 'l', 'l', 'o'}. However, if you input "Hello" into a string array the array would look like this {"Hello"} (note it has only 1 element). Therefore, if you want to have access to each separate character of a string, you need to use char array.

Another thing I'd like to point out:
You can totally have an array whose size can be determined at run-time. This is called a "dynamic array". For this we have to allocate memory using the new keyword. After we are done using the array, we free the memory using the delete keyword.

Here is an example:
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
27
28
29
30
31
32
33
#include <iostream>

using namespace std;

int main()
{
    int size; //The size which we'll get from the user
    
    cout << "Enter the size of the array: ";
    cin >> size; //Get the size from the user
    
    int *number; //Pointer to the allocated memory.
    number = new int[size]; //Dynamically allocate memory of 'size' no. of blocks
    
    
    //Get numbers and store them in the array
    for(int i = 0; i < size; i++)
    {
        cout << "Enter number " << i+1 << ": ";
        cin >> number[i];
    }
    
    //Output the numbers
    for(int j = 0; j < size; j++)
    {
        cout << j+1 << " number you've entered: " << number[j] << endl;
    }
    
    //Free the allocated memory
    delete[] number;
    
    return 0;
}
@StormBoy, thanks!
std::cout<<"C++ likes you!";
Lol you are welcome.

(BTW, its "Stormboy" not "StormBoy" ;)).
@StormBoy (yes, I will call you StormBoy just to bug you) I also realize both of you insist on adding a return 0; at the end, what differs between adding one or not? I use both ways and notice no difference.
Thanks StormBoy (lol)
There is no difference. It is highly debated which is better practice. In my opinion, not using EXIT_SUCCESS means both ways are bad practice ;)
@LB Thanks, all of you!
Topic archived. No new replies allowed.