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

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Vectors
AI Thread Summary
The discussion centers around creating a for loop in C++ to populate a vector with user-input integers. The original poster is seeking help after starting the loop but getting stuck. They are advised to use "cin >> userGuesses[i];" to read integers directly into the vector. The conversation then shifts to the absence of a "cout" statement for output, with participants noting that printing is not required unless specified in the problem statement. They suggest adding a second for loop to print the vector's contents for verification.The discussion also transitions to a similar problem in Java, where the user encounters a compilation error due to redeclaring the loop variable "i." The solution provided is to remove the "int" declaration from the for loop, as "i" has already been declared outside the loop. The participants emphasize that while constants can be used for defining the number of values in a vector, it is not mandatory, as vectors can dynamically adjust based on input.
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 cout statement? (Thinking)
 
ineedhelpnow said:
i don't know if i said this before but ILU ILS! :o question tho...why no cout statement? (Thinking)

We only use cout 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 cout.
 
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

Back
Top