Member Avatar for Ayu1004

I've spent entire night and day searching on the internet how to solve my problem, and i couldn't find anything... so please help me, if you know :(

this is piece of my code:

int main ()
{
	string line;
	
	cout<<"Welcome!"<<endl;
	getline(cin, line);

	while (line!="0")
	{
		hello (line);
		getline(cin, line);
	}
	return 0;
}

as you can see i didn't use getline for stream input and i don't plan on using only one string. but the problem is that every time i write something i have to press enter twice, for the program to react. nothing is wrong with the function hello. so how do i fix this, so i don't have to press enter twice? and yes i tried putting the '\n'. doesn't work

Recommended Answers

All 13 Replies

I dont quite understand your problem, I ran this test program and everything seemed to run ok.

#include <iostream>
#include <string>
using namespace std;

void hello(string &str) {
   // Do something with str
}

int main () {
   string line;
   
   cout << "Welcome!" << endl;
   getline(cin, line);

   while (line != "0") {
      hello (line);
      getline(cin, line);
   }
   return 0;
}

Unless your version of hello is doing something different to mine, it should work.

Member Avatar for Ayu1004

it's not the problem about hello because when i used cin>>line; earlier, it worked perfectly fine. the problem is that i have to press enter twice when using getline. you don't get that? i asked one of my friends and he said he gets the same problem...

>the problem is that i have to press enter twice when using getline.
Press twice for what to happen?
I enter a string, and press enter and the function hello deals with it, and then the same happends until I enter "0" as a string, isn't that what is supposed to happen?

Member Avatar for Ayu1004

>the problem is that i have to press enter twice when using getline.
Press twice for what to happen?
I enter a string, and press enter and the function hello deals with it, and then the same happends until I enter "0" as a string, isn't that what is supposed to happen?

i have to press twice to get any reaction from the program... again it's not the problem about hello because when i used cin>>line; earlier it was 100% ok. for some reason when i use getline i have to press enter twice to get a new code line to execute... i really don't understand... it's strange that you don't get that...

Would you mind posting the entire code so I can understand it better then, I missing a large chunk which is obviously causing the problem. :P

Member Avatar for Ayu1004

Would you mind posting the entire code so I can understand it better then, I missing a large chunk which is obviously causing the problem. :P

.... fine.... it's only the getline that's screwing up everything though ...

#include <iostream>
#include <string>
using namespace std;

void hello (string line)
{
	bool greetings=false;
	bool greeting_back=false;
	int greeting_counter=0;
	int greeting_back_counter=0;
	string greeting[2] = {"hello", "hi"};
	int i;
	for (i=0; i<2;i++)
	{	
		if (line==greeting[i])
		{
			{
	if (greetings==true)
		{		
			greeting_counter++;
			if (greeting_counter>1)
				cout<<"are you going to keep saying hello all day? :)"<<endl;
			else cout<<":)"<<endl;
		}
			else 
			{
				cout<<"hello"<<endl;
				greetings=true;
			}
	}
			greeting_back=true;
		}
	}

	if (greeting_back==false)
	{
		if (greeting_back_counter>1) {}
		else 
		{
			cout<<"no hello for me? :("<<endl;
			greeting_back_counter++;
		}
	}
}

int main ()
{
	string line;
	
	cout<<"Welcome!"<<endl;
	getline(cin, line);

	while (line!="0")
	{
		hello (line);
		getline(cin, line);
	}


	return 0;
}
Member Avatar for Ayu1004

err.. it looks like i messed something else here too...

Well, I dont know what to say o.O it works perfectly for me :)

Member Avatar for Ayu1004

oh yeah..i made some changes,the greeting_back and the greeting_back_counter should be in the main part of program because it always resets itself

Member Avatar for Ayu1004

Well, I dont know what to say o.O it works perfectly for me :)

haha you didn't check everything: this is the new code, i corrected the mistakes, but i still have to press twice because of the getline...:

#include <iostream>
#include <string>
using namespace std;

void hello (string line, int &greeting_counter, int &greeting_back_counter, bool &greetings,bool &greeting_back)
{
	string greeting[2] = {"hello", "hi"};
	int i;
	for (i=0; i<2;i++)
	{	
		if (line==greeting[i])
		{
			{
	if (greetings==true)
		{		
			greeting_counter++;
			if (greeting_counter>1)
				cout<<"are you going to keep saying hello all day? :)"<<endl;
			else cout<<":)"<<endl;
		}
			else 
			{
				cout<<"hello"<<endl;
				greetings=true;
			}
	}
			greeting_back=true;
		}
	}

	if (greeting_back==false)
	{
		if (greeting_back_counter>1) {}
		else 
		{
			cout<<"no hello for me? :("<<endl;
			greeting_back_counter++;
		}
	}
}

int main ()
{
	string line;
	int greeting_counter=0;
	int greeting_back_counter=0;
	bool greetings=false;
	bool greeting_back=false;
	
	cout<<"Welcome!"<<endl;
	getline(cin, line);

	while (line!="0")
	{
		hello (line,greeting_counter,greeting_back_counter,greetings,greeting_back);
		getline(cin, line);
	}


	return 0;
}

> but i still have to press twice because of the getline...:
My answer remains, it works perfectly for me x]

I'll go out on a limb and guess that you're using Visual C++ 6.0, where your problem is a known bug.

commented: Good guess :) +3
commented: From C++ expert to Psychic? Whoo! =P +4
Member Avatar for Ayu1004

I'll go out on a limb and guess that you're using Visual C++ 6.0, where your problem is a known bug.

I am using it... o.o omg... ty ty ty!!!!!! ty for helping me!!! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.