Vector pointer.

I'm trying to get the user to input 10 word's one at a time and store them in
a string vector as they are entered. The biggest problem so far has been using a variable to point to the next location in the vector.
After several attempt's this is the closest i've got (1 error).

The error i'm getting is:

G:\DCSTemp\Vector Exercises - Copy\main.cpp|13|error: 'i' was not declared in this scope|||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <vector>
#include <cctype>

int main()
{
    std::vector<std::string> StrVec (10);
    std::string input;
    std::cout << "Enter 10 word's one at a time.\n";
    for (int i = 0; (i = (StrVec.size() -1)); ++i);
    {
        std::cout << "Enter word " << i + 1 << " ";
        std::cin >> input;
        std::vector<std::string> StrVec[i] = input;
    }
}
Last edited on
Look at your for loop condition.

(i = (StrVec.size() -1))

This is saying "keep looping if (assign the size of the vector minus one to i) is true"

I think you should instead be checking if i is less than the size of the vector.

Also, thank you so much for using ++i instead of i++ :D

On line 15, remove the std::vector<std::string> part - you don't need to redeclare the vector.
Updated the code, But still getting the original error message.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <vector>
#include <cctype>

int main()
{
    std::vector<std::string> StrVec (10);
    std::string input;
    std::cout << "Enter 10 word's one at a time.\n";
    for (int i = 0; (i = StrVec.size()); ++i);
    {
        std::cout << "Enter word " << i + 1 << " ";
        std::cin >> input;
        StrVec[i] = input;
    }
}
Remove the semicolon after the for statement at line 11.
You did not correctly update your code. Look again at your loop condition:

(i = StrVec.size())

This is saying "keep looping as long as (assign the size of the vector to i) is true."

Does that make sense to you?

As I said originally, you need the loop condition to be true when i is less than the size of the vector.
Thank's L B, Sorted.

Is there any way i can improve my program?
Only started learning a couple of month's ago and need all the help i can get.

#include <iostream>
#include <string>
#include <vector>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
    unsigned int counter = 0;
    std::vector<std::string> StrVec (10);
    std::string input;
    std::cout << "Enter 10 word's one at a time.\n";
    for (unsigned int i = 0; i < StrVec.size(); ++i)
    {
        std::cout << "Enter word " << i + 1 << " ";
        std::cin >> input;
        StrVec[i] = input;
    }
    for (unsigned int i = 0; i < StrVec.size(); ++i)
    {
        counter++;
        std::cout << " " << StrVec[i];
        if (counter == 8)
            {
                counter = 0;
                std::cout << '\n';
            }
    }
}
Topic archived. No new replies allowed.