Sign in to follow this  

Need help C++

This topic is 4016 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Please help me keep this program open.
#include <iostream>
#include <cstring>
using namespace std;



int main()
{
start:
		char str[80];
	char *start, *end;
	int len;
	int t;
	int i;
	
		cout << "Enter phrase or word: ";
	cin >> str;
	cout << "original: " << str << "\n";

	len = strlen(str);

	start = str;
	end = &str[len-1];

	while(start < end) {
		t = *start;
		*start = *end;
		*end = t;

		start++;
		end--;
	}
	
loop:
	cout << "reversed: " << str << "\n";
			goto loop;		
	
	
	return 0;
		
}
The problem is that the window stays open when a value is entered but how do I only have it loop once so that the word comes up followed by please insert phrase or word (The start)?

Share this post


Link to post
Share on other sites

loop:

cout << "reversed: " << str << "\n";

goto loop;


This segment of code above creates an infinite loop. Get rid of the goto loop statement and have it goto start instead. You want to check the input for quit condition so it's not infinitely loop your program. For example, if the user enter just q it terminates. I recommend getting away from gotos unless they're absolutely necessary ie. demanded by someone you're doing coding for (though I can't image anyone outside programming class making such a demand). Also you don't need to include cstring since you're not using anything from cstring. It would probably be better if you were using a string instead of a char, unless you're attempt to understand how to deal with char pointers.

Share this post


Link to post
Share on other sites

This topic is 4016 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this