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.
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();
}
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.
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)
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.
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?
//PD_ProjectAccess.h
#include <vector>
usingnamespace 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";
}
}
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 :)
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...