How to Populate a Vector Using a For Loop in C++?

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Vectors
Click For Summary
SUMMARY

The discussion focuses on how to populate a vector in C++ using a for loop. Participants clarify that to read integers into the vector `userGuesses`, the line `cin >> userGuesses[i];` should be used within the loop. Additionally, it is noted that there is no requirement to print the values immediately after input, although doing so can help verify correctness. A similar issue arises in a Java implementation where the variable `i` is mistakenly declared twice, leading to a compilation error.

PREREQUISITES
  • C++ programming fundamentals
  • Understanding of vectors in C++
  • Basic input/output operations using `cin`
  • Java programming fundamentals (for the Java-related discussion)
NEXT STEPS
  • Implement error handling for user input in C++
  • Explore dynamic vector resizing in C++
  • Learn about Java's `ArrayList` for dynamic arrays
  • Study best practices for variable scope in C++ and Java
USEFUL FOR

Students learning C++ and Java, software developers working with user input in arrays and vectors, and educators teaching programming concepts.

ineedhelpnow
Messages
649
Reaction score
0
ok I am working on some activities and i can't get past this one.
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}.

Sample program:

Code:
#include <iostream>
#include <vector>
using namespace std;

int main() {
   const int NUM_GUESSES = 3;             
   vector<int> userGuesses(NUM_GUESSES); 
   int i = 0;                         

   <STUDENT CODE>

   return 0;
}

this is all i got so far
Code:
for(i=0;i<NUM_GUESSES;++i)

now I am stuck (Blush). do i need to define any new variables? (Thinking) like "x" or something. a variable for the integers entered? :confused:
 
Technology news on Phys.org
ineedhelpnow said:
ok I am working on some activities and i can't get past this one.
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}.

Sample program:

Code:
#include <iostream>
#include <vector>
using namespace std;

int main() {
   const int NUM_GUESSES = 3;             
   vector<int> userGuesses(NUM_GUESSES); 
   int i = 0;                         

   <STUDENT CODE>

   return 0;
}

this is all i got so far
Code:
for(i=0;i<NUM_GUESSES;++i)

now I am stuck (Blush). do i need to define any new variables? (Thinking) like "x" or something. a variable for the integers entered? :confused:

No need.
Try [m]cin >> userGuesses;[/m]
(Nerd)
 
i don't know if i said this before but ILU ILS! :o question tho...why no count statement? (Thinking)
 
ineedhelpnow said:
i don't know if i said this before but ILU ILS! :o question tho...why no count statement? (Thinking)

We only use count if we want to print something.
 
yeah. shouldn't the statement be printed?
i thought it always had to be printed (Blush)
i guess this is the first example i had where it wasnt printed...sorry. :o
 
ineedhelpnow said:
yeah. shouldn't the statement be printed?
i thought it always had to be printed (Blush)
i guess this is the first example i had where it wasnt printed...sorry. :o

Well... the problem statement doesn't ask for anything to be printed...

But yeah, it does make sense to print the result to verify if the program worked properly.
So then I suggest to put in another for-loop in which the values in the vector are printed with count.
 
when doing vectors will there always be a constant in the program referencing the number of values?
 
ineedhelpnow said:
when doing vectors will there always be a constant in the program referencing the number of values?

Nope. There won't be.
We can read as many values as there are on the input, and put them in the vector one by one.
Afterwards, we can "ask" the vector how many values are in there.
 
Same Question but using Java. Write a for loop to populate array userGuesses with NUM_GUESSES integers. Read integers using Scanner. Ex: If NUM_GUESSES is 3 and user enters 9 5 2, then userGuesses is {9, 5, 2}.

I have :

for(int i =0;i<NUM_GUESSES;i++)
userGuesses = scnr.nextInt();

output:

StoreGuesses.java:10: i is already defined in main(java.lang.String[])
for(int i =0;i

Any input would help, thank you :)
 
  • #10
dinklebuurg said:
Same Question but using Java. Write a for loop to populate array userGuesses with NUM_GUESSES integers. Read integers using Scanner. Ex: If NUM_GUESSES is 3 and user enters 9 5 2, then userGuesses is {9, 5, 2}.

I have :

for(int i =0;i<NUM_GUESSES;i++)
userGuesses = scnr.nextInt();

output:

StoreGuesses.java:10: i is already defined in main(java.lang.String[])
for(int i =0;i

Any input would help, thank you :)
I'm sorry, here's the full code if it might help. My input is above.
 
  • #11
It looks like you need to define [m]NUM_GUESSES[/m] first, and your for loop needs braces, doesn't it (I assume since the languages I use all require them).

edit: Perhaps you don't need braces since the body of the loop is one statement. I tend to always use them for clarity. :)
 
  • #12
dinklebuurg,
From the error message, I'm close to 100% certain your main looks something like:
Code:
int i;
for (int i=0;i<NUM_GUESSES;i++) {
  // something
}
The Java compiler doesn't like your to declare variable i twice. In the for loop when you say int i, you are declaring i as well as initializing it to 0. The above code declares i before the for loop and so i is declared twice. So just remove the word int in your for loop.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 15 ·
Replies
15
Views
4K
Replies
1
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 30 ·
2
Replies
30
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K