Need help

Write a program that asks the user to type a sentence into the command prompt. If the user typed the word "exit" or "Exit" (without quotes and all lower case), then the program should exit. Otherwise, the program should print what the user typed back to the screen and ask the user to type something else.

My question is how do I make the program exit when the word Exit is typed. The program as of now will exit only when there is no capitalization at the begging but I need it to exit whether the user types "exit" or "Exit".
Thank-you in advance!!


#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

int main() {
string data;

do
{

cout << "Type a sentence and press enter."
"If the word 'exit' is typed, the program will close." << endl;

getline(cin, data);

// validate if data is not equals to "exit"
if (data.compare("exit") != 0)
{
// then type back
cout << data << endl;
}
else
{
// else interrupt while
break;
}
// will run while break or return be called
}
while (true);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

int main()
{
char exit;

do{
	
	
	
	//code here
	
	
	
	
	
	
	
	cout << endl;
	cout << "type exit to exit: ";
	cin >> exit;
}while(exit == 'exit' || exit == 'Exit');
return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>

using namespace std;

int main() {
string data;

do
{

cout << "Type a sentence and press enter."
"If the word 'exit' is typed, the program will close." << endl;

getline(cin, data);

// validate if data is not equals to "exit"
if (data != "exit" && data != "Exit" )
{
// then type back
cout << data << endl;
}
}while (data != "exit" && data != "Exit");
return 0;
}
Last edited on
Thank y'all both!! That worked!!
Topic archived. No new replies allowed.