some problem

...apparently I found some problem here when I tried converting char array to string

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 <sstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    char quit;
    char input;
    int changed;
    vector <char> test;
    do
    {
        cout << "enter a number" << endl;
        cin >> input;
        test.push_back(input);
        cout << "want to quit ? press e" << endl;
        cin >> quit;
    } while (quit != 'e');
    char integer[test.size()];
    for (int i = 0; i < test.size(); ++i) integer[i] = test.at(i);
    for (int i = 0; i < test.size(); ++i) cout << integer[i] << " ";
    string result (integer);
    cout << result;
}


it did good converting those char but after the end of the char, the string added some strange symbol at the end
You forgot to add a null character '\0' to mark the end of the string.
Last edited on
thanks
Topic archived. No new replies allowed.