Unable to convert 'int' to const 'string'

I'm working on a program right now, where I'm trying to create a dynamic vector array, then assign individual strings to parts of the array.

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
	void init_create_cards()
	{

		vector <char> question(count);
		vector <char> answer(count); 

		//This part basically says that while 'i' is less than count, the program will prompt the user for 
		//input, and add one to 'i'
		
		for (int i = 1; i < count; i++)
		{
			cout << "Please type the question for card " << i << "\n\n";
			question.vector::push_back(i);
			cin >> question[i];

			system("cls");

			cout << "Please type the answer for card " << i << "\n\n";
			answer.vector::push_back(i);
			cin >> answer[i];

			system("cls");
		}
		
	}


When I try to use strings with the array:

1
2
          vector <string> question(count);
		vector <string> answer(count);


The compiler complains that it can't convert an integer to a constant string. When I use a char, it works better, compiled without error, but in the program when I try to input a value, it gets mad when I put in anything more than one character. I don't know how to extend the memory for a char when it's in a vector array, so can someone please help?

This is not homework, I'm doing this for fun by myself. I'd really appreciate any help of feedback.
A char and an int are both integral data types. Hence, when you say
question.push_back( i );
the "i" is first converted to a char, as if you had said
question.push_back( (char)i );

What you really want is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
vector <string> questions;
vector <string> answers;

for (int counter=1; true; counter++)
{
    string s;

    cout << "Please enter the question for card " << counter
         << ".\n(Or just press ENTER to stop)" << endl;
    getline( cin, s );
    if (s.empty()) break;  // we're done when the user just presses ENTER
    questions.push_back( s );

    cout << "Please enter the answer for card " << counter << endl;
    getline( cin, s );
    answers.push_back( s );

    cout << endl;  // put an extra line of space between each Q/A pair.
}


Clearing the screen is overrated. When you are really ready to do so, if you are using Unix/Linux/OS X, check out my answer in this thread:
http://www.cplusplus.com/forum/beginner/387/

On Windows:
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
35
36
37
#include <windows.h>

void clear() {
  HANDLE                     hStdOut;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  DWORD                      count;
  DWORD                      cellCount;
  COORD                      homeCoords = { 0, 0 };

  hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  if (hStdOut == INVALID_HANDLE_VALUE) return;

  /* Get the number of cells in the current buffer */
  if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
  cellCount = csbi.dwSize.X *csbi.dwSize.Y;

  /* Fill the entire buffer with spaces */
  if (!FillConsoleOutputCharacter(
    hStdOut,
    (TCHAR) ' ',
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Fill the entire buffer with the current colors and attributes */
  if (!FillConsoleOutputAttribute(
    hStdOut,
    csbi.wAttributes,
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Move the cursor home */
  SetConsoleCursorPosition( hStdOut, homeCoords );
  }


Or, to be simple, just cout a bunch of newlines:
1
2
for (int i = 0; i < 300; i++) cout << '\n';
cout << flush;


Hope this helps.
Last edited on
Thats mean duoas! I ve just put togheter a few lines too! :)

mine, using pointers:

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <vector>
#include <string>

using namespace std;


int main()
{

	int count =3;


	//pointers: points to some memory
	//string* myString points to the beginning of a string in memory

	//store all pointers to the string in vectors
	vector<string*> question;
	vector<string*> answer;

	string* tmpString=0;

	for( int i=0; i<count; i++)
	{
		//ask for the question
		cout << "Please type the question for card " << i << "\n\n";
		tmpString = new string; //create a new string in mem
		//put your question in it
		cin>>*tmpString; 
		//store the pointer in the vector
		question.push_back(tmpString); //it is only the POINTER TO the string NOT the string itself!


		//the same for the answer
		cout << "Please type the answer for card " << i << "\n\n";
		tmpString = new string;
		cin>>*tmpString;
		answer.push_back( tmpString);

	}


	//--->>> output!!
	//loop trough all values that are in the vector
	vector<string*>::iterator quest;
	vector<string*>::iterator answ;

	for(quest = question.begin(), answ=answer.begin();
		quest!= question.end() && answ!=answer.end(); 
		quest++, answ++)
	{
		//print the question
		cout<<*(*quest);	//quest=iterator in the vector
							//*quest = converts the iterator back to a string-pointer (string*)
							//*(*quest) = the string (string, not string*) that this pointer points to
		
		cout<<"  -   ";

		//print the answer	
		cout<<*((string*)*answ);

		cout<<"\n";
	}
		

	return 0;
}



Just wondering about vectors, they are all one after each other like an array. So if you use string that can change its size, isnt it possible that the string overwrites the next string in the vector?
(i guess it doesnt, there is propably some fallback rescue-function in vectors, im just not absolutely sure)



thanks wooden nose

Last edited on
Thanks a lot guys, very helpful, as usual.

Duoas, if you're using *nix, why not just

system("clear");

Instead of cls, like in windows?
Last edited on
> Just wondering about vectors, they are all one after each other like an array.

Yes, a vector is specifically stored as an array internally. So you could pass it to functions that take arrays:
1
2
3
4
void myarrfn( const int a[], unsigned size );
...
vector<int> v;
myarrfn( &v[0], v.size() );


> So if you use string that can change its size, isnt it possible that the string overwrites the next string in the vector?

No, a string is a container class. So the array is an array of objects. Modifying one object doesn't affect other objects.

> Duoas, if you're using *nix, why not just

For the same reason you shouldn't say system("cls") in Windows. It is resource heavy and a security leak.

For cross-platform homework solutions, I don't mind seeing
if (system( "cls" )) system( "clear" );
But still, you should only clear the screen if you _need_ to. That's very rare. Remember, clearing the screen in a typical console application is fairly anti-user-friendly.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.