Intro to c++ help with vector problem

  • Context: C/C++ 
  • Thread starter Thread starter medonaldson2
  • Start date Start date
  • Tags Tags
    C++ Intro Vector
Click For Summary
SUMMARY

The discussion centers on a C++ programming issue involving the population and output of a vector named userGuesses. The original code incorrectly attempts to output the vector's contents using cout << userGuesses[i];, which results in an out-of-bounds access when i equals NUM_GUESSES. The solution provided involves using a loop to iterate through the vector and output each element individually, ensuring correct formatting. The final code successfully outputs the integers entered by the user without any extraneous characters.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with the vector container from the C++ Standard Library
  • Knowledge of input/output operations using cin and cout
  • Basic understanding of for loops and array indexing
NEXT STEPS
  • Explore C++ vector member functions, particularly at() for safe access
  • Learn about C++ error handling and exceptions
  • Investigate formatting output in C++ using manipulators
  • Study best practices for managing array bounds in C++ programming
USEFUL FOR

C++ beginners, software developers learning data structures, and anyone troubleshooting vector-related issues in their code.

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.
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
15K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K
  • · 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
5K
  • · Replies 1 ·
Replies
1
Views
1K