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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

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