problems with getline

I have this code:

1
2
3
4
5
6
7
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:
|


Include headers:

1
2
3
4
5
6
7
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <conio.h>
#include <vector> 


Help?
Last edited on
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);
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<string>
#include<vector> 
using namespace std;
int main(){
	vector<string> paramList;
	int numParams=10; // you are counting it 0,1,2,3,4,5,6,7,8,9
	string tempParam = "";		
	for(int i=0; i<numParams; i++){	//@changed		
		cout<<"Please enter the name for parameter "<<i+1<<":"<<endl;
		getline(cin, tempParam);			
		paramList.push_back(tempParam);
	
	}

	// Display

	for(int j=0; j< paramList.size();j++)
		cout << "position " <<j << " Value " << paramList.at(j).c_str() << "\n";
	
	

return 0;
}
He needs it to start at zero, mahlerfive.
Thanks a lot for the tips, guys. One more question - I tried cin.clear but what parameters does it take?
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>

Hope this helps
Errors. O noez.


------ 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 : ')'


Gah! Help please!
Last edited on
Thanks much.

Nvm though, I fixed it myself =D

 
cin.ignore(1024, '\n');


Yay!

Mod: Please lock this topic or delete it, not needed anymore.
Topic archived. No new replies allowed.