this is a basic question but I'm new to C++ so apologies in advance :)
I couldn't seem to print out the strings I stored in a vector. I have used std:: cout as well as printf but printf seems to give the error "The program has stopped working". Where am I going wrong?
Here's the code with std::cout :-
#include <iostream>
#include <cstdio>
#include <vector>
#include <fstream>
using namespace std;
int main(){
int np;
string temp;
scanf("%d", &np);
vector <int> money;
vector <string> names;
for(int i = 0; i< np; i++){
scanf("%s", &temp);
names.push_back(temp);
cout << names[i] << endl;
}
return 0;
}
This didn't return any string at all.
The other program I tried with printf is exactly the same, except the cout line is replaced with:
printf("%s", &names[i]);