quit program when character q is entered

Hello, I am trying to exit the program if the character 'q' is entered into input. I am expecting an integer value for input. I am confused on the best way to go about this. I tried atoi but had no luck. Would a switch statement work better for me?

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60




// Still need program to exit if character q is entered

// still need program to exit if say a big number like 50000000000 is entered,
// stop computation at 35000000 
// if user enters character s

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <string>


#define PI 3.14

using namespace std;

int main(){
// for the program to keep executing until control + c is pressed, or 'q' is entered
bool done = false;
int input;


while(!done){

std::cout << "Please enter an integer or 'q' to quit" << std::endl;



// user input
cin >> input;

if(input == 'q')
	done = true;
	break;


std::cout << "You have asked to compute the square root of PI " << input <<  " times:" << std::endl;

	// for loop to increment number output
	for(int i = 0; i < input; i++){
		std::cout << (i+1) << std::endl;
	}

	double calculatePi;
	calculatePi= sqrt(PI);

	std::cout << "The square root of PI is " << setprecision(3) << calculatePi << "." << std::endl;

	}
}




This is what you missed. So you are trying to check if an integer equals a string.

1
2
3
4
5
6
7
8
9
10
int main(){
	while (true) {
		int command;   // Should be string command if we want to launch if-statement.
		cin >> command;

		if (command == "q") {   // This will not work
			break;
		}
	}
}


However, this would work perfectly fine:

1
2
3
4
5
6
7
8
9
10
int main(){
	while (true) {
		string command;    // Now it's a string!
		cin >> command;

		if (command == "q") {
			break;
		}
	}
}


In your case you are trying to make the program see if an Integer = "q", which doesn't make much sense. Therefor, you have to set the input as a string and then convert it into an integer so it makes sense for your code. Take a look:

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
27
28
29
int main(){
	string choice;
	double PI = 3.14;

	while (true) {

	std::cout << "Please enter an integer or 'q' to quit" << std::endl;
	cin >> choice;

	if (choice == "q") {
		break;
	}
	else {
		int input = atoi(choice.c_str());     // Converting string to integer

		std::cout << "You have asked to compute the square root of PI " << input << " times:" << std::endl;

		for (int i = 0; i < input; i++) {
			std::cout << (i + 1) << std::endl;
		}

		double calculatePi;
		calculatePi = sqrt(PI);

		std::cout << "The square root of PI is " << setprecision(3) << calculatePi << "." << std::endl;
		}

	}
}


The code above should work!
Last edited on
awesome, thank you very much!

Also, say a big number like 5 million is entered by the user, and I don't want to go all the way to 5 million, i want to stop at 1 million. If i press the character 's', how do i stop the computation immediately and display the last computation I did, before display sqrt of pi? would i just use the peek function to read any input? the program will then come back and ask for input again
Last edited on
A little uncertain of what you are requesting. For storing something with a spesific set of digits or letters, you could use char array. For example char bignumber[50]. Maybe it would need some specifications if you still want to keep the amount of characters the same but not exceed a certain number. Ergo, 1 million instead of 5 million, considering that they both have 7 digits. You can read more about this here, http://www.cplusplus.com/doc/tutorial/ntcs/.

When it comes to the "press the character 's'" part you could do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
vector<char> values;

int main(){
	char addDigit;

	while (cin >> addDigit) {
		if (addDigit == 's') {
			break;
		}
		else {
			values.push_back(addDigit);
		}
	}

	cout << "Your total values is: ";
	for (int i = 0; i < values.size(); ++i) {
		cout << values[i];
	}
	cout << "\n" << endl;

	return 0;
}

You could also make the whole vector one single variable instead of printing it out like I did here. From the code above you can start making out what is- and not permitted inside the vector. For example, the first char can only be between 1-4 or something. As shown below:

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
27
vector<char> values;

int main(){
	char addDigit;

	while (cin >> addDigit) {          // Keep adding digits to vector unless
		if (addDigit == 's') {         // We press 's' to end the loop.
			break;
		}
		else {
			values.push_back(addDigit);
		}
	}

	if (values[0] != 1) {      // The first value we entered must be a 1
		cout << "You must start with a 1" << endl;
	}
	else {   // Displaying the values by printing them out, but only if first value was a 1
		cout << "Your total values is: ";
		for (int i = 0; i < values.size(); ++i) {
			cout << values[i];
		}
		cout << "\n" << endl;
	}

	return 0;
}
Last edited on
So this is all i need to do now "Now if the user entered a huge number by mistake and wants stop the computation in the middle! The only way to stop your basic program in the middle of a computation is to use Ctrl C, which would brutally kill your program. Design and implement your program so that users can stop any computation in the middle by entering ā€˜sā€™. Your program will stop gracefully the current computation and display the prompt:

Please enter an integer or ā€˜qā€™ to quit."

this is what I have. How would i go about doing this? Thanks!!

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// still need program to exit if say a big number like 50000000000 is entered,
// stop computation at 35000000 
// if user enters character s

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <string>
#include <cstring>

#define PI 3.14

using namespace std;

int main(){
// for the program to keep executing until control + c is pressed, or 'q' is entered
bool done = false;
string choice;
string input;

while(!done){

std::cout << "Please enter an integer or 'q' to quit" << std::endl;



// user input
cin >> choice;

if(choice == "q"){
	break;
}else{

		int input = atoi(choice.c_str());     // Converting string to integer

std::cout << "You have asked to compute the square root of PI " << input <<  " times:" << std::endl;
		
			// for loop to increment number output
			for(int i = 0; i < input; i++){
				std::cout << (i+1) << std::endl;
			}

			double calculatePi;
			calculatePi= sqrt(PI);

			std::cout << "The square root of PI is " << setprecision(3) << calculatePi << "." << std::endl;

			}
		}
	}




Are you thinking about a key stroke? See this:

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
27
28
29
30
#include <Windows.h>
#include <concrt.h>

vector<char> values;

int main(){
	char addDigit;

	while (cin >> addDigit) {
		if (addDigit == 's') {
			break;
		}
		else {
			values.push_back(addDigit);
		}
	}

	cout << "Your total values is: ";
	for (int i = 0; i < values.size(); ++i) {
		cout << values[i] << endl;
		Concurrency::wait(500);

		if (GetKeyState('S') & 0x8000) {
			break;
		}
	}
	cout << "\n" << endl;

	return 0;
}


Check out https://stackoverflow.com/questions/41600981/how-do-i-check-if-a-key-is-pressed-on-c for more
I don't really know how to go about this. so if the user enters 's', how do i stop current computation and ask for input again?
Topic archived. No new replies allowed.