string tempParam = "";
for(int i=0; i<numParams+1; i++){
cout<<"Please enter the name for parameter "<<i+1<<":"<<endl;
getline(cin, tempParam);
paramList.push_back(tempParam);
}
...which I want to repeat numParams (previously cin'd int) times and add tempParam each time to vector<string> paramList. The problem isi, it's skipping the first parameter for some reason:
Please enter the name for parameter 1:
Please enter the name for parameter 2:
|
Did you take in any input before this code? If so you may need to clear out the input buffer (it probably has a return character left in there). Also I believe you should remove the +1 from your for loop or it will loop one too many times.
May work, may not - I'm still learning myself. First of all instead of adding +1 to all your variables, why not just have i start at 1? Also, there is no need to endline when getting input as far as I know, that should be automatic. Also you don't have to initialize tempParam as "" in this case, let the user initialize it on input.
1 2 3 4 5 6 7
string tempParam;
for(int i=1; i<numParams; i++){
cout<<"Please enter the name for parameter "<<i<<":";
getline(cin, tempParam);
paramList.push_back(tempParam);
}
You don't need to clear the cin. The clear is used if the stream has an error (for example you asked for an int and the user entered char).
You need to ignore all the unecessary new lines from the stream, that cause your getline() to be skipped.
You can do that by entering std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ); before your loop.
You must include the <limits> header also #include <limits>
------ Build started -----
Compiling...
main.cpp
warning C4003: not enough actual parameters for macro 'max'
error C2589: '(' : illegal token on right side of '::'
error C2143: syntax error : missing ')' before '::'
error C2059: syntax error : ')'