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

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.

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?

>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

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;
}

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

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;
}

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

This article has been dead for over six months. Start a new discussion instead.