Sign in to follow this  

Need help C++

Recommended Posts

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

int main()
		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;

	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
prh99    520


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

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