Intro to c++ help with vector problem

In summary: Instead, cin should be used to read in the values and then userGuesses.at should be used to output the correct value.
  • #1
medonaldson2
2
0
So I'm trying to do this problem:

Write a for loop to populate vector userGuesses with NUM_GUESSES integers. Read integers using cin. Ex: If NUM_GUESSES is 3 and user enters 9 5 2, then userGuesses is {9, 5, 2}.

Here is my code so far...

Code:
#include <iostream>
#include <vector>
using namespace std;

int main() {
   const int NUM_GUESSES = 3;
   vector<int> userGuesses(NUM_GUESSES);
   int i = 0;
   
   for (i = 0; i < NUM_GUESSES; i++) {
      cin >> userGuesses[i];
   }
   cout << userGuesses[i];

   return 0;
}

The output is when tested with values {2, 4, 6}

02 4 6

The output needs to be...

2 4 6

I don't know why the zero is there. Any ideas?

thanks
 
Technology news on Phys.org
  • #2
I managed to get the desired output with

Code:
#include <iostream>
#include <vector>
using namespace std;

int main() {
		const int NUM_GUESSES = 3;
		vector<int> userGuesses(NUM_GUESSES);
		int i;

		for (i = 0; i < NUM_GUESSES; i++) {
			cin >> userGuesses[i];
		}

		cout << '\n';

		for (i = 0; i < NUM_GUESSES; i++) {
			cout << userGuesses[i] << ' ';
		}

		cout << '\n';

		return 0;
}

I made some minor changes. Hopefully you can spot them. :)
 
  • #3
The problem with your code is at the line:
Code:
cout<<userGuesses[i];
When this is executed, the value of i is 3!. But userGuesses has components at 0, 1 and 2. There is no component v[3]. The standard says (I looked it up) that this causes no run time error, but the result could be any thing. The member function at of vector, userGuesses.at(i), has the same behavior as [], but will "throw an exception" since there is no such component of userGuesses. The exception causes immediate exit from the program. So when using a vector, it is up to the programmer to ensure any reference using [] is a valid component of the vector.

greg1313 has shown you a correct program. Notice he uses a loop to output the contents of userGuesses. You can not write cout<<userGuesses; i.e. cout does not accept vectors to output.
 

FAQ: Intro to c++ help with vector problem

What is a vector in C++?

A vector in C++ is a dynamic array that can hold a collection of elements of the same data type. It is a part of the Standard Template Library (STL) and provides various functions for manipulating and accessing the elements.

How do I declare a vector in C++?

To declare a vector in C++, you need to include the vector header file and then use the vector keyword followed by the data type of the elements inside angle brackets. For example, vector myVector; will declare a vector that can hold integer values.

How do I add elements to a vector in C++?

You can add elements to a vector in C++ using the push_back() function. This function takes in the value of the element to be added as an argument and appends it to the end of the vector.

How do I access elements in a vector in C++?

To access elements in a vector in C++, you can use the at() function, which takes in the index of the element as an argument and returns its value. You can also use the square bracket [] notation to access elements directly by their index.

How do I resize a vector in C++?

To resize a vector in C++, you can use the resize() function, which takes in the new size of the vector as an argument. If the new size is larger than the current size, the vector will be extended with default-initialized elements. If the new size is smaller, the extra elements will be removed from the end of the vector.

Similar threads

Replies
11
Views
15K
Replies
22
Views
3K
Replies
5
Views
2K
Replies
23
Views
2K
Replies
5
Views
4K
Replies
39
Views
4K
Replies
1
Views
1K
Back
Top