Hi
I have the following code which is my project
I completed section 1 and 2 but it doesn't work
could you help figuring why?

#include<iostream>
#include<string>


using namespace std;
void count();

void reverse();

void encode();

void decode();

int selection;

int main()
{
	

	cout<<"******************"<<endl;
	cout<<"Welcome To Project"<<endl;
	cout<<"******************"<<endl;
	cout<<endl;

	cout<<"Please choose from the following menu or 0 to exit"<<endl;
	cout<<"1 - Count Characters of a string."<<endl;
	cout<<"2 - Reverse a string."<<endl;
	cout<<"3 - Encode a string."<<endl;
	cout<<"4 - Decode a string."<<endl;

	cin>>selection;

	while(selection != 0)
	{
	switch(selection)
	{
	case 1:
			count();
			break;
	case 2:
			reverse();
			break;
	case 3:
			encode();
			break;
	case 4:
			decode();
			break;
	
	default:
			cout<<"You did not choose from the list";
			break;
	}
	}



	return 0;

}

void count()
{
	string str;

	int spaces=0;
	int words=0;
	cout<<endl;
	cout<<"Enter a string: "<<endl;

	getline(cin,str);
	

	for(int i=0;i<=str.length()-1;i++)
	{

		if(str[i]==' ')
			spaces++;

	}

	words = spaces + 1;

	cout<<endl;
	cout<<"Number of characters is: "<<str.length()<<endl;
	cout<<"Number of spaces is: "<<spaces<<endl;
	cout<<"Number of words is: "<<words<<endl;


selection=0;


}

void reverse()
{
	string str;
	string rstr;
	cout<<endl;
	cout<<"Enter a string: ";

	getline(cin,str);
	
	for(int i=str.length()-1;i>=0;i--)
	{
		rstr += str[i];

	}
	cout<<endl<<rstr;
	selection=0;
}

void encode()
{

}
void decode()
{
}

You have mixed use of >> and getline() in the same program. If you do that you need to be aware that >> will leave the terminating char in the input buffer which will be evaluted when the next call that accesses the input buffer is encountered. If the next call is to >> then the terminating char left from the previous call will be ignored and everything will be fine. However, if the next call is to getline() then trouble can arise. Since the newline (enter) char is a common terminating char for >> and it is the most common terminating char for getline() (it is the default terminating char for getline() and it is the terminating char for getline() in your program) then getline() sees newline char first, stops attempting further input and removes the leading newline char from the input buffer without reading anything into the string. Therefore, your code doesn't do what you want, it does what it is supposed to do.

To fix it, either:
a) don't mix >> or other input methods that leave terminating char in the input buffer when using getline() OR
b) be sure you clear the input buffer of any remaining char before you call getline().

Some people go so far as to never using anything but getline() in their programs, eschewing >> altogether.

commented: Say "yes" to getline() +32
commented: Good approach ! +7

How to do the fix in code please?

I tried
cin.ignore(1000, '\n');
before every cin and it seems ok
is this correct

Using ignore() to clear the input buffer before each call to getline() when getline() is used in programs with other input methods is fine. The size of the first parameter sent to ignore() is debated at times, but for most code you are likely to encounter at this stage in your career (and mine too for that matter) using a reasonably large number like 1000 will usually work.

Thank you very much.

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.