One More Vector Issue?!?

Hey again! I got my last vector issue working. Now, I am successfully taking user input into vectors. However, my issue now is that after taking the input, I am attempting to output the size of the vector, such as in the sample below:


1
2
3
4
5
6
7
8
9
10
11
12
13

int main () {

//Vectors
vector<char> Name;

//Then the user inputs values into the 'Name' vector...

//Finally...
cout << "Thank you!" << endl << endl;

cout << "Name Size:   " << (int) Name.size();


Whenever this is run, however, the output is "Name Size: -1". If I try using the 'char' type before the 'Name.size()' command, the output is "Name Size: \377" ?

I am having difficulty figuring this out. could anyone help?
You're probably having some undefined behavior going on in your input retrieval code, so you should post that.
Are you sure that Name vector is being properly populated? Set a breakpoint after the user inputs values into the vector and then check the size of the container when the breakpoint is triggered.

Also looking over your code from your previous post, change how you input data into the vector to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

for (p = 0; end_input == true; p++) {
                char c;
                cin>>c;
		Database.push_back(c);              //or Search_Term
		switch (c) {
			case '|':
				Database.pop_back();   //or Search_Term
				end_input = false;
				break;
			default:
				cout << Database[p] << endl;     //or Search_Term
				break;		
		}
	}


Last edited on
Hey. The code that Diedrexier posted is roughly my retrieval code, however, I did not have the :

char c;
cin >> c;

Besides that the code is the same. However, my output displays every character that the user enters, as an individual line, outputting each element of the vector. For example:


//User inputs...

Hello

//This entire word is typed and then entered. I am expecting that each character is placed in an individual index. The output is then:

H
e
l
l
o

However, the 'Name.size()' function returns -1 at this point. I will try what diedrexier suggests, but in the meantime, does anyone else suggest anything?
Besides that the code is the same.

That's not possible, as diedrexler's code would not work without the two lines you mentioned (which just happen to be responsible for actually reading the input).

Again: post your full code.
Last edited on
Okay. Sorry. Here is my code:

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
//Preprocessor Directives
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

using namespace std;

//Vectors
vector<char> Database;
vector<char> Search_Term;

//Main Function Heading
int main (int argc, char * const argv[]) {

Database.reserve(1);
Search_Term.reserve(1);

end_input = true;

//This 'for' loop places user input into the 'Database' vector, ending, when the user inputs a specific key.
	for (p = 0; end_input == true; p++) {
		cin >> Database[p];
		switch (Database[p]) {
			case '|':
				Database.pop_back();
				end_input = false;
				break;
			default:
				cout << Database[p] << endl;
				break;		
		}
	}

cout << endl << endl << "Next, please enter each character of the term you would like to search for throughout the 'Database Terms.' /n It may be part of a name." << endl;
	cout << "Also, the same rules apply. When you enter the '|' character, input will end.";

end_input = true;

		//This 'for' loop places user input into the 'Search_Term' vector, ending, when the user inputs a specific key.
	for (p = 0; end_input == true; p++) {
		cin >> Search_Term[p];
		switch (Search_Term[p]) {
			case '|':
				Search_Term.pop_back();
				end_input = false;
				break;
			default:
				cout << Search_Term[p] << endl;
				break;		
		}	
	}

	cout << endl << endl << "Database Size:    " << (int) Database.size() << endl << endl << "Search Term Size:    " << (int) Search_Term.size();


	return 0;	
}



P.S.
I tried your idea Diedrexier, however, the console merely flashed white, and infinite line breaks were entered by the system. I do not understand this but.... for now, I have posted the code Athar.

Also, some variables in the above code are not shown in declarations. Just please keep in mind that they are declared in the actual code that is being compiled. They do not affect my question, however, so I did not include them.
Last edited on
Your original code, I mean. That doesn't even compile.
The code above should work now. I think I may have fixed it.
Last edited on
When dealing with undefined behavior, that might be pure luck.
You probably were writing out of array or vector bounds somewhere.
Since you're not very forthcoming with information, I can just recommend taking a look at how vectors are used again.
Is this the full section of code where your vectors are manipulated?
Yes it is. This is all I actually have in my original code. This is all I have. However, I can't understand why the size won't turn out correct. I have read and re-read the information in the vectors parts of this website, yet I can't seem to figure it out?
Even after your edit, the code still does not compile.
Be that as it may, your vectors contain no elements, but you try to access some elements using
cin >> Database[p];
That will always be an out-of-bound access.
To actually add an element to a vector, you need to use push_back.

With Database.pop_back(); you actually try to remove an element from a vector that is already empty.
That's possibly why size() returns -1. But anything could happen - that's just undefined behavior.
Last edited on
Okay. Thank you! I'll try that...
WooHoo!!! Thank you both Athar and Diedrexler!

I got the code to compile and function correctly, using a combination of both of your suggestions. I tried a very similar approach earlier, but it did not work. However, I re-organized the code, and I tried some slightly different commands, and now it works!

Thanks again. :)
Your code as it is wont compile. Making sure that all the variables are properly declared(see: end_input and p ) in addition to following the code changes i described will get the vectors populated in the right way.

Thanks for your help. :)
Topic archived. No new replies allowed.