asking for input after computation
Feb 15, 2018 at 12:44am UTC
So i ask for input, user enters 50, then program displays 1-55 and outputs the sqrt of pi. After I display the sqrt of pi, how do i bring up the "Please enter an integer or 'q' to quit" again and ask for input? Right now i have to enter s, but i don't want to have to do that. 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <string>
#include <cstring>
#include <thread>
#define PI 3.14
using namespace std;
bool thread_done = false ;
void number_output(int n){
bool thread_break = false ;
double calculatePi;
for (int i = 0; i < n; i++){
std::cout << (i+1) << std::endl;
if (thread_done == true ){
thread_break = true ;
break ;
}
}
// std::cout << "The square root of PI is " << setprecision(3) << calculatePi << "." << std::endl;
std::cout << "The square root of PI is 1.77" << std::endl;
if (thread_break == false )
cout<<"Program Done. Enter 's' to continue... " ;
thread_done = true ;
}
int main(){
// for the program to keep executing until control + c is pressed, or 'q' is entered
bool done = false ;
string choice;
string input;
double calculatePi;
while (!done){
std::cout << "Please enter an integer or 'q' to quit" << std::endl;
// user input
cin >> choice;
if (choice == "q" ){
done = true ;
}else {
int input = atoi(choice.c_str()); // Converting string to integer
thread_done = false ;
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;
// }
for (int j = 0; j <= input; j++){
calculatePi= sqrt(PI);
}
// std::cout << "The square root of PI is " << setprecision(3) << calculatePi << "." << std::endl;
if (input == 0){
std::cout << "squre root of PI is 0" << std::endl;
}
std::thread t1 (number_output, input);
string validate = "" ;
while (true ){
std::getline(std::cin, validate);
if (validate == "s" ){
cout<<"Program Terminated Gracefully!" <<endl;
thread_done = true ;
t1.detach();
break ;
}
}
}
}
}
Feb 15, 2018 at 8:50am UTC
If you don't want to enter "s" just remove the entire loop on line 80.
Topic archived. No new replies allowed.