cin << troubles

Hi there, can anyone help with this, im making a very simple program for practice and am coming up against a problem that I've seen other people have had also, but I cant seem to get their solutions to work. My problem is with getline.(cin,str) the program works ok but the second time I use cin << etc I have to press return twice to input the string. I trued putting cin.ignore(1000,'\n'); after every cin to make sure theres nothign left in the input buffer but that makes the problem worse and then every time i use cin i have to press return twice. Could anyone shed any light on whats happening here.

here's the snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void AddProject() {
	
	//get title and description and input them into a temp object
	cout << "input a name for the project\n";
    string str;
    getline(cin,str);
	tempProj.setName(str);
	
	cout << "and now the description\n";
	getline(cin,str);
	tempProj.setDescription(str);
	
	//copy new object into the vector
	projV.push_back(tempProj);
	
}
string str;
getline(cin,str);

if (str[0] == 'a'){    	
    	AddProject();
}


Thanks everyone (my first post!)
There is no cin>> in your snippet..

Anyway, compile this code and see how many times you have to press enter:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

int main(){
    std::string a, b;
    std::cin >> a;
    std::cin.ignore(1000, '\n');
    std::getline( std::cin, b );
    std::cout << a << ", " << b;
    std::cin.get();
    return 0;
}
my mistake, re <<, your code works totally fine, press enter once like you would imagine but if i try to apply that to mine (as best as i can anyway:) i now have new problems, i have to press return indefinatley after some input sessions until i input another character, heres the whole amazing program :P, hope it makes sense, I really appreciate this by the way.

PD_ProjectAccess.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <string>
#include <iostream>

using namespace std;

//class prototype---------------------------------------

class Project {
		string Project_name;
		string Project_description;
	public:
		Project();
		void setName(string);
		void setDescription(string);
		string name();
		string description();
};


//class function definition ---------------------------

void Project::setName (string name){
	Project_name = name;
}

void Project::setDescription (string description){
	Project_description = description;
}

string Project::name(){
	return(Project_name);
}

string Project::description(){
	return(Project_description);
}
//constructor -----------------------------------------

Project::Project (){
	Project_name = "no name";
	Project_description = "no description";
}


PD_ClassProject.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <string>
#include <iostream>
#include <vector>

using namespace std;

//create array to store project objects and pointer to it
vector <Project> projV;
Project tempProj;


//function to add new entry--------------------------------------------
void AddProject() {
	
	//get title and description and input them into a temp object
	cout << "input a name for the project\n";
	string str;
	std::getline( std::cin, str );
	std::cin.ignore(1000, '\n');
	tempProj.setName(str);
	
	cout << "and now the description\n";
	std::getline( std::cin, str );
	std::cin.ignore(1000, '\n');	
	tempProj.setDescription(str);
	
	//copy new object into the vector
	projV.push_back(tempProj);
	
}

//function to list entries---------------------------------------------
void listProjects() {

	if (projV.size() == 0) {
	
		cout << "no projects exist\n";
		
	} else {
	
		cout << "-----------------------------------------------\n";
		cout << "---------------listing projects----------------\n";
		
		for (int i=0; i < projV.size(); i++){
		
			cout << "-----------------------------------------------\n";
			cout << "project ID: " << i << endl;
			cout << "name: " << projV[i].name() << endl;
			cout << "description: " << projV[i].description() << endl;
			
		}
		
		cout << "-----------------------------------------------\n";
		cout << "------------------end of list------------------\n";
		cout << "-----------------------------------------------\n";
		
	}
	

}


PD_Control.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
#include "PD_ClassProject.h"
#include "PD_ProjectAccess.h"

int main()
{
    using namespace std;
    
    char inputChar[1];
    
    cout << "-----------------------------------------------\n";
    cout << "------- Woo, project database! have fun -------\n";
    cout << "-----------------------------------------------\n";
    cout << "---------What would you like to do?------------\n";
   	cout << "---'a' add entry, 'l' list entries, 'q' quit---\n";
    
    while (1) { //infinite loop
    
    	string str;
    	std::cin >> str;
    	std::cin.ignore(1000, '\n');
    	cout << "str = " << str[0] << endl;
    	
    	if (str.size() > 1){
    		cout  << str << " : command too long" << endl;
    	
    	} else if (str.size() < 1){
    		cout << "command too short" << endl;
    	
    	} else if (str[0] == 'a'){    	
    		AddProject();
    		
    	} else if (str[0] == 'l'){    	
			listProjects();
			
    	} else if (str[0] == 'q'){	
    		break;
    		
   	 	} else {
   	 	
   	 		cout << str << " : invalid command" << endl;
   	 		
    	}
    }
    
    return 0;
}


thanks again
Last edited on
You only need to ignore after cin>>. Also, just so that you know, plain ignore() might be sufficient (though 1000, '\n' would discard any garbage in a line)

Also, you're misusing headers. See http://www.cplusplus.com/forum/articles/10627/
Still no luck, i apologise if im being slow here, I removed all the ignore()'s other than the one after the cin >> in the PD_Control.cpp but i still have a similar but different problem. Now it works fine until i am prompted to enter a description and like before i press return indefinatley until i enter a character and press retuen. Also I'm reading the page on heder files now, thanks for the pointer:)

P.S. i dont know if i should include the new source in this kind of scenario, i dont want to make things too verbose if its annoying.

Thanks, j
Last edited on
That's not a problem at all. Place line 18 of "PD_Control.cpp" immediately after line 14 (so that there is some output in the loop) and see what you get.
Thats brillian, its working now which is great but i really dont understand why:) I moved lines 15 and 16 so they are inside the loop and now it works fine. How would a pair of cout statements change how the cin buffer is acting?

Heres the new code which works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//PC_classProject.h
using namespace std;

//class prototype---------------------------------------

class Project {
		string Project_name;
		string Project_description;
	public:
		Project();
		void setName(string);
		void setDescription(string);
		string name();
		string description();
};


//class function definition ---------------------------

void Project::setName (string name){
	Project_name = name;
}

void Project::setDescription (string description){
	Project_description = description;
}

string Project::name(){
	return(Project_name);
}

string Project::description(){
	return(Project_description);
}
//constructor -----------------------------------------

Project::Project (){
	Project_name = "no name";
	Project_description = "no description";
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//PD_ProjectAccess.h
#include <vector>

using namespace std;

//create array to store project objects and pointer to it
vector <Project> projV;
Project tempProj;


//function to add new entry--------------------------------------------
void AddProject() {
	
	//get title and description and input them into a temp object
	cout << "input a name for the project\n";
    string str;
    getline( cin, str );
	tempProj.setName(str);
	
	cout << "and now the description\n";
	getline( cin, str );
	tempProj.setDescription(str);
	
	//copy new object into the vector
	projV.push_back(tempProj);
	
}

//function to list entries---------------------------------------------
void listProjects() {

	if (projV.size() == 0) {
	
		cout << "no projects exist\n";
		
	} else {
	
		cout << "-----------------------------------------------\n";
		cout << "---------------listing projects----------------\n";
		
		for (int i=0; i < projV.size(); i++){
		
			cout << "-----------------------------------------------\n";
			cout << "project ID: " << i << endl;
			cout << "name: " << projV[i].name() << endl;
			cout << "description: " << projV[i].description() << endl;
			
		}
		
		cout << "-----------------------------------------------\n";
		cout << "------------------end of list------------------\n";
		cout << "-----------------------------------------------\n";
		
	}
	

}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//PD_Control.cpp
#include <iostream>
#include <string>
#include "PD_ClassProject.h"
#include "PD_ProjectAccess.h"

int main()
{
    using namespace std;
    
    char inputChar[1];
    
    cout << "-----------------------------------------------\n";
    cout << "------- Woo, project database! have fun -------\n";
    cout << "-----------------------------------------------\n";
    
    while (1) { //infinite loop
    
   		cout << "---------What would you like to do?------------\n";
   		cout << "---'a' add entry, 'l' list entries, 'q' quit---\n";
    
    	string str;
    	cin >> str;
    	cin.ignore(1000, '\n');
    	cout << "str = " << str[0] << endl;
    	
    	if (str.size() > 1){
    		cout  << str << " : command too long" << endl;
    	
    	} else if (str.size() < 1){
    		cout << "command too short" << endl;
    	
    	} else if (str[0] == 'a'){    	
    		AddProject();
    		
    	} else if (str[0] == 'l'){    	
			listProjects();
			
    	} else if (str[0] == 'q'){	
    		break;
    		
   	 	} else {
   	 	
   	 		cout << str << " : invalid command" << endl;
   	 		
    	}
    }
    
    return 0;
}


Thanks hamsterman
It doesn't change anything. if you have cin >> something; and press enter without entering anything, you'll get to enter something again until you do. Thus if you just keep pressing enter, it will seem that you're in an infinite loop.
but thats te weird thing i do enter something, but i have to do it twice, the problem i was having was that when prompted to enter a description i would enter some text and press return, and then press return indefinatley until i input a second line, so here is output for the old version where the cout statements were before the loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
------- Woo, project database! have fun -------
-----------------------------------------------
---------What would you like to do?------------
---'a' add entry, 'l' list entries, 'q' quit---
a
str = a
input a name for the project
test name
and now the description
test description etc   


junk
str = j
junk : command too long


so after i input the line 'test description etc' i pressed return and it was only when i typed a 'junk' and hit enter a second time that the function progressed but now with the word junk in the cin buffer.

But when i change the code as per the last post so the two cout statements at 19 &20 are inside the loop then the program runs fine, heres the new output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
------- Woo, project database! have fun -------
-----------------------------------------------
---------What would you like to do?------------
---'a' add entry, 'l' list entries, 'q' quit---
a
str = a
input a name for the project
test name 
and now the description
test description etc
---------What would you like to do?------------
---'a' add entry, 'l' list entries, 'q' quit---



So all i changed were the placement of the two cout statements, this suggests to me that the cout statements are having an effect... i think anyway:)


Apologies if im just not understanding your answers, im probably just being slow, its just hard to get my head around :)

j
Last edited on
some more info, i played with the contents of the cout statement and the least you need to have it 'fix' the problem is cout << "a ";, it seems that the combination of a letter and a white space does something magic to fix the problem. Not sure why but i thought it may help make sense of whats happening...

Thanks j
I'm telling you. there is no problem. The function is executed correctly and the program waits for input on line 23.

Compile this code:
1
2
3
4
5
6
7
8
#include <iostream>
#include <string>

int main(){
   std::string str;
   std::cin >> str;
   return 0;
}
Try pressing enter and see what happens. The same thing happens in your code too.
massive apologies for being an idiot, i just got it:) Man, I'll only make that mistake once

thanks a bunch, j
Topic archived. No new replies allowed.