When I run this program using the F or R choice the first output repeats twice, and I can't figure out why. The rest of the program works great.


Prog8.cpp

#include "stdafx.h"
#include "ModifyT.h"
#include <iostream>
#include <string>
using namespace std;


int main()
{
	char ch;
	char exitLetter;
	char line[101];
	ModifyT modifytext;

	cout << "Please enter R to reverse the string, and F for a normal string: ";
    cin >> ch;
	

	while (ch == 'f' || ch == 'F' ) {
		cout << "Type a Phrase: ";
                  cin.getline(line, 101);
                  exitLetter = line[0];

		if (exitLetter == 'y' || exitLetter == 'Y') {
                    return(0);
		}

        if (exitLetter == 'x' || exitLetter == 'X') {
                   modifytext.PrintF(line);
		}

	}

	while (ch == 'r' || ch == 'R') {
		cout << "Type a Phrase Under 100 Characters: ";
		cin.getline(line, 101);
		exitLetter = line[0];

		if (exitLetter == 'y' || exitLetter == 'Y') {
                    return(0);
		}

                if (exitLetter == 'x' || exitLetter == 'X') {
                   modifytext.PrintR(line);
                }
	}

}

ModifyT.cpp

#include "stdafx.h"
#include "ModifyT.h"
#include <iostream>
#include <string>
using namespace std;

void ModifyT::PrintF(string text) {
    cout << text << endl;
}

void ModifyT::PrintR(char *textPtr) {
	char *p = textPtr;

	while ( *p != '\0' ){
	 ++p;
	}

	while ( p != textPtr ) {
	 cout.put ( *--p );
	}

        cout << "\n";
}

ModifyT.h

#ifndef _MODIFYT_H
#define	_MODIFYT_H

#include <string>
using namespace std;

class ModifyT
{
public:
	void PrintR(char *);
	void PrintF(string);
};

#endif	/* _MODIFYT_H */

Any help would be appreciated.

Change the whiles to if statements, and for all purposes, exitLine is ch so make a while loop around the two statements to ask whether or not the user wants to go again.

char yesno = 'y';


while(yesno == 'y' || yesno == 'Y') {
cout << "Please enter R to reverse the string, and F for a normal string: ";
    cin >> ch;
	

	if (ch == 'f' || ch == 'F' ) {
		cout << "Type a Phrase: ";
                  cin.getline(line, 101);

                   modifytext.PrintF(line);
		}

	}

	if (ch == 'r' || ch == 'R') {
		cout << "Type a Phrase Under 100 Characters: ";
		cin.getline(line, 101);

                   modifytext.PrintR(line);
                }
	}
cout<<"press y to continue or any other key to quit."<<endl;
cin>>yesno;
}

the reason you use ifs is that ch doesn't change from whatever you input earlier so the while loop repeats.

I need the the program to keep asking for a phrase thats why I used the while loop. It reprints the phrase if an X is the first character and quits the program if a Y is the first character.

my snippet should do that, it starts with yesno = to y and then asks for a forward or reverse string(your code) after displaying it, it then asks whether or not you want to go again, and then for a forward/reverse string ect, until you enter another character besides 'y'.

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.