little problem

Hi. I'm new to this forum aswell as C++ programming. I started learning a few days ago and now I'm trying to make a program that gets a string the user inputs and then writes that string character by character. So far I've come up with the code below but it doesn't work for some reason.
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
#include <conio.h>
#include <iostream>

using namespace std;

int main ()
{
    string str;
    int i [2] = { 10, 0 };
        cout << "Please write something.\n";
        cin >> str;
        i[2] = str.length();
        char c [i[2]];
        cout << "You worte: ";
        while (i[2] > 0)
        {
            cout << c[i[2]];
            Sleep(50);
            if (i[2] == 0)
            {
                cout << "\n";
            }
            i[2]--;
        }
    cout << "Press any key to exit.";
    getch();
    return 0;
}

Is there anything (obviously) wrong?
You have to include the string header to use string

#include <string>

You also need to include the Windows header to use Sleep()

#include <Windows.h>



Last edited on
closed account (3qX21hU5)
A suggestion is to instead of using a array at all you can just step through the string.

For example.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string stuff;
	cin >> stuff;
        
        // Steps through the string and prints each char on a separate line.
	for (string::size_type i = 0; i != stuff.size(); ++i)
            cout << stuff[i] << endl;

	return 0;
}


This code steps through the string and prints each character in the string on a separate line. You can modify it to fit your needs better if you need to.

Remember that a string is basically just a container that hold a bunch of characters. If we think about a string this way we realize that we can use it just like a array or vector in some cases.

Hope this helps a bit, and if there is anything you don't understand or need me to go into more detail on just let me know and I will do my best to try and help explain it.
Last edited on
I think he wanted to save the characters, Zereo.

If that's the case, you can't do that because arrays declared like that must have their dimension known at compile time. To get around this you could use pointers. As I'm not so good as those, I couldn't really explain how to.

EDIT: I found a page that could help - http://www.cplusplus.com/forum/general/62801/

Hope that helps!
Last edited on
Ok. Thanks for the replies. Here's the final design that still won't work.
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
34
#include <windows.h>
#include <conio.h>
#include <iostream>
#include <string.h>

using namespace std;

int main ()
{
    string str;
    int i [2] = { 10, 0 };
    while (true)
    {
        cout << "Please write something. \n";
        cin >> str;
        i[2] = str.length();
        char c [i[2]];
        cout << "You worte: ";
        while (i[2] > 0)
        {
            cout << c[i[2]];
            Sleep(50);
            if (i[2] == 0)
            {
                cout << "\n";
            }
            i[2]--;
        }
        i[1]--;
    }
    cout << "Press any key to exit.";
    getch();
    return 0;
}


EDIT

Nevermind the code in this reply, I'll try what Zereo suggested when I get back home.
Last edited on
closed account (3qX21hU5)
If he wants to save the characters he can use a vector<char> to save them in. Though I'm not sure he wanted to save them since he wasn't in his original post. But just in case you do want to save each character from the string here is how you can do that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string name("brandon");
vector<char> letters;

for (string::size_type i = 0; i != name.size(); ++i)
{
    // Prints the letters
    cout << name[i] << endl;

    // Stores each letter in the vector. Each element will contain a letter.
    letters.push_back(name[i]);
}

// Prints each element in the vector
for (vector<char>::size_type i = 0; i != letters.size(); ++i)
    cout << letters[i];


There is also other ways to do this but this is probably the easiest way to do so.

Here is some links to help you also.

Vector Tutorial - http://www.dreamincode.net/forums/topic/33631-c-vector-tutorial/

String Tutorial - http://www.dreamincode.net/forums/topic/42209-c-strings/

Let me know if you need anything else. And as always hope this information helps :)
Topic archived. No new replies allowed.