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.
 

Related to Intro to c++ help with vector problem

1. 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.

2. 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.

3. 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.

4. 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.

5. 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

  • Programming and Computer Science
Replies
11
Views
15K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
2
Replies
66
Views
4K
Back
Top