I've noticed that although my code "works" if you follow the rules, adding more numbers causes this value to be passed to the next cin.
for example in this program i take a sentence using get line and count vowels and such. It works but some things happen that i don't know how to stop.
It counts the "enter" at the end as a character.
at the end of the program where i ask 1 for yes or 2 for no,
i do cin >> again;, the number is counted and the loop continues,
but the enter is counted as well and causes the program to "skip"(not really) back to the yes or no at the bottom.
Basically I've noticed this before but haven't come across the solution.
...I'm not sure how to make cin safe, is this what people mean by "memory leak"
First, you don't have to use this but if you were to use
usingnamespace std;
before int main(){ ... } you wouldn't have to use
std::
Second about the problem where at the end the user input isn't stoping the loop. Try initiating the variable without a value (at the top). I assume your having an endless loop. Also I don't see where you are receiving a new input for the variable (again). You know:
cin>>again;
also I've had problems similar to this in the past and I set it
1 2 3 4
do {
this...
}
while(again != 2 )
About the other question, what I think is going on is your string can also be viewed as a char array[ ]; and when you use a char array you will have a \0 to signal the end of the sequence. If Im understanding your wording right. I think the answer to what is causing it to count, what you call the "enter" is really it counting the last character of the sequence ( \0 ). Now if its causing an issue in the loop where you are using the input.length(), you could change it to y<=input.length()-1;
Thanks man. I changed the code, I was messing with it but I did have cin>>again; at the bottom of it all.
I'm not getting an infinite loop though, that was not the problem.
The program runs perfectly but. let me run through it.
1. I start it off and it prints out the initial cout message saying write stuff.
2. so getline puts whatever you write into the input string.
3. it goes through the for loop and adds to the ints i placed their, and does so correctly.
4. once out of the for loop, it prints out the integers and I check and #of vowels and stuff is right.
5. finally it prints out "again 1.yes ...2.no" and uses cin >> again.
6. pressing 2 will get you out of the loop while(again==1)
7. pressing 1 will cause the loop to go again.
8. here is the problem
9. the cin>>again action uses the 1 correctly, but uses the "enter" by placing it in the getline part of the loop from point 2 on this post. so it doesn't wait at the getline again, it continues past and stops at the "again yes or no" portion of code.
Why does it skip, why does getline not wait for the input again.
at the "again yes or no" portion, if i write
1(followed by a space) words here
then the program would use the 1 correctly, but the use the following words in the getline portion, like its already there in the memory.
Also if you know the loop will iterate at least once you could use a do while loop so the user will set the value for you. It prevents the possibility of your predetermined value being the possible problem.
I used the cin.ignore, and it worked a little, might even work indefenitely but I didn't tinker with it too long just yet.
I simplified the code in the first post while still replicating the problem.
Yes the code has flaws but right now I am only trying to answer one question.
Why is it that the last line cin>>x; causes so much trouble
if i put an integer it skips the next getline in the loop.
if i put a char, words, then it goes on an infinite loop.
so what happens with cin >> x, seems to be spilling over and affecting the getline() portion.
I'm tryihg to figure this out but haven't found something that is a solid solution.
For one you might need to use cin.ignore in more than one place and second I see you have a bool condition in the while statement but no bool variable.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int x;
string y;
do
{
cout << "write words" << endl;
getline(cin,y);
cout <<"Your input is:"<< y << endl;
cout << "Type 2 if you want the loop to end" << endl; //This sentence is for you
cin >> x;
}
while(x != 2);
}
Not sure if this helps but I felt like you earned it.
That is considerate, but funny thing is your code still has the problem I was encountering.
I'm not having trouble with the loop, making it stop or go.
The problem is the second time in the loop, the program doesn't wait for the getline input.
Your program did this too. Does it do that when you run it?
if you type words , or put a number when it asks you to press 2 to quit,
respectively, you'll get an infinite loop, or it'll skip the getline, print out "your input is" (blank),
and you'll be at the press 2 to quit option again.
So how can we continue without encountering the infinite loop or skipping?