roboticonx8
roboticonx8

Reputation: 39

string/integer vector - cout

I tried to make a program that will ask the user to input pancakes eaten by 10 people and then list them. People's names and pancakes eaten were stored in different vectors. I am getting an error when printing the values from the vectors.

#include <iostream>
#include <bits/stl_vector.h>
#include <bits/stl_bvector.h>

using namespace std;

int main() {
    vector<int> pancakes;
    vector<string> name;
    int temp_num;
    for (int x = 0; x < 10; x++) {
        cout << "Enter pancakes eaten by person " << x+1 << endl;
        cin >> temp_num;
        pancakes.push_back(temp_num);
        name.push_back("Person " + x);
    }
    for (int x = 0; x < 10; x++){
        cout << name[x] << " ate " << pancakes[x] << " candies." << endl;
    }
    return 0;
}

The error I'm getting is "Subscripted value is not an array.".

Upvotes: 0

Views: 77

Answers (1)

Cory Kramer
Cory Kramer

Reputation: 117856

You cannot add a std::string and an int, so this is not allowed

name.push_back("Person " + x);

You can, however, use std::to_string and then concatenate.

name.push_back("Person " + std::to_string(x));

Also I'm not sure why you have <bits> includes, you should only have

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

Upvotes: 3

Related Questions