C/C++ Intro to c++ help with vector problem

  • Thread starter Thread starter medonaldson2
  • Start date Start date
  • Tags Tags
    C++ Intro Vector
Click For Summary
The discussion focuses on a coding problem involving populating a vector with user input integers in C++. The initial code attempts to read integers into a vector but produces an incorrect output due to an attempt to access an out-of-bounds index. The output includes an unexpected zero because the index variable `i` exceeds the valid range after the loop. A solution is provided that correctly outputs the vector's contents by using a separate loop to print each element, ensuring that the output format meets the requirements. Key points include the importance of managing vector indices properly and the limitation of the `cout` function, which cannot directly output vectors.
medonaldson2
Messages
2
Reaction score
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
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. :)
 
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.
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

  • · Replies 11 ·
Replies
11
Views
15K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
12
Views
3K
  • · Replies 39 ·
2
Replies
39
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K