Backward String

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1233 Views - Last Post: 05 March 2013 - 07:37 PM Rate Topic: -----

#16 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: Backward String

Posted 05 March 2013 - 10:20 AM

Spoiler

Was This Post Helpful? 1
  • +
  • -

#17 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Re: Backward String

Posted 05 March 2013 - 03:22 PM

I have reviewed everyone's posts and had gotten the program to run appropriately. :)/>/>

Another thing my professor wants us to do is to always put while loops around our programs asking the user if they want to run it and if the user would want to run it again.

I have no problem writing a while loop for this, we have been doing that for almost 2 semesters now. Though with this program, when I write the while loop I usually write, the whole program outputs without the user doing any input at one time.

The only way I got it to not do that is if I changed
// Get string.
	cout << "Enter a word, name, or phrase: ";
	getline(cin, word);


to

// Get string.
	cout << "Enter a word, name, or phrase: ";
	cin >> word;


but when I do this, word only accepts up to one word instead of an entire phrase.

Any thoughts? Thank you!


P.S. Here is my main() where I am trying to place the while loop:
// Function prototype.
void printReverse (string *);

int main()
{
	char answer;
	string word;
	string *x;
	x = &word;

	// Initialize program.
	cout << "Would you like to reverse a word, name, or phrase?\nEnter Y for Yes, or N for No: ";
	cin >> answer;
		
	while (answer == 'y' || answer == 'Y')
	{

	cout << "\n";
	// Get string.
	cout << "Enter a word, name, or phrase: ";
	getline(cin, word);

	// Function call.
	printReverse(x);

	// Rerun program?
	cout << "Would you like to reverse a word, name, or phrase again?\nEnter Yes or No: ";
	cin >> answer;
	}

	return 0;
}

This post has been edited by 4N63L-Z3R0: 05 March 2013 - 03:25 PM

Was This Post Helpful? 0
  • +
  • -

#18 4N63L-Z3R0  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 94
  • Joined: 19-October 12

Re: Backward String

Posted 05 March 2013 - 03:28 PM

P.S. Here is my int main() with the while loop:
int main()
{
	char answer;
	string word;
	string *x;
	x = &word;

	// Initialize program.
	cout << "Would you like to reverse a word, name, or phrase?\nEnter Y for Yes, or N for No: ";
	cin >> answer;
		
	while (answer == 'y' || answer == 'Y')
	{

	cout << "\n";
	// Get string.
	cout << "Enter a word, name, or phrase: ";
	getline(cin, word);

	// Function call.
	printReverse(x);

	// Rerun program?
	cout << "Would you like to reverse a word, name, or phrase again?\nEnter Y for Yes, or N for No: ";
	cin >> answer;
	}

	return 0;
}

This post has been edited by 4N63L-Z3R0: 05 March 2013 - 03:39 PM

Was This Post Helpful? 0
  • +
  • -

#19 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: Backward String

Posted 05 March 2013 - 05:40 PM

When the user enters their response on line 10, they type in 'Y' and <Enter>. Your cin extraction only reads in the 'Y', but leaves the carriage return in the input buffer. getline() stops reading when it sees the carriage return. So you need to clear the buffer before you call getline().

This post has been edited by Skydiver: 05 March 2013 - 07:44 PM

Was This Post Helpful? 2
  • +
  • -

#20 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Backward String

Posted 05 March 2013 - 07:37 PM

While we're at it ... :)
You may like to see these two other looping control examples ...

1stly ... with the loop-end test incorporated into the input prompt ...

// recursiveReverseString.cpp // 2013-03-05 //

#include <iostream>
#include <string>



// example of recursive function calls ...
void reverse( std::string& s, int bot, int top )
{
	if( bot >= top ) return; // ok ... time to quit since no more char's to swap
	
	// swap (this) 'top' with (this) 'bot' char ...
	char tmp = s[bot];
	s[bot] = s[top];
	s[top] = tmp;
	
	reverse( s, bot+1, top-1 ); // ok ... call recursively now ...
}



int main()
{
	using std::cout;
	using std::flush;
	using std::endl;
	using std::cin;
	using std::string;
	
	for( ; ; ) // an example of a 'forever loop' in C++ (or C) ...
	{
		cout << "Enter string to reverse (or empty line to exit): " << flush;
		string line;
		getline( cin, line ); // 'eats newline' at end
		
		if( !line.size() ) return 0;
		
		reverse( line, 0, line.size()-1 );
		cout << "Reversed: " << line << endl;
	}
}



2ndly ... with a 'more' function called at the end of the loop (which loop is to be traversed at least once) ...

// recursiveReverseStringMore.cpp // 2013-03-05 //

#include <iostream>
#include <string>



// example of recursive function calls ...
void reverse( std::string& s, int bot, int top )
{
	if( bot >= top ) return; // ok ... time to quit since no more char's to swap
	
	// swap (this) 'top' with (this) 'bot' char ...
	char tmp = s[bot];
	s[bot] = s[top];
	s[top] = tmp;
	
	reverse( s, bot+1, top-1 ); // ok ... call recursively now ...
}

bool more() // defaults to 'yes' more ...
{
	std::cout << "More (y/n) ? " << std::flush;
	std::string line;
	getline( std::cin, line );
	if( line == "n" || line == "N" ) return false;
	return true;
}



int main()
{
	using std::cout;
	using std::flush;
	using std::endl;
	using std::cin;
	using std::string;
	
	do
	{
		cout << "Enter string to reverse: " << flush;
		string line;
		getline( cin, line ); // 'eats newline' at end
		
		reverse( line, 0, line.size()-1 );
		cout << "Reversed: " << line << endl;
	}
	while( more() );
}

This post has been edited by David W: 05 March 2013 - 10:08 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2