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

  • Thread starter Thread starter medonaldson2
  • Start date Start date
  • Tags Tags
    C++ Intro Vector
AI Thread 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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

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