Having some difficulty figuring out the right syntax I should be using when trying to get the Do/While loop to work. I would like to write a calculator in C++ that allows the user to enter a string and then it does its thing and prints the result, asks the user if they want to go again or not.
I have a simple main function so far just to get the Do/While loop right, but when I enter y or Y, the program just asks if I want to continue again, it doesnt give me the opporunity to run the "calculator" part again. What am I doing wrong?
#include "stdafx.h"
#include <iostream> //cout, cin
#include <string> //string
#include <algorithm> //remove_if(), end(), begin(), erase()
#include <stack> //stack<type>
#include <ctype.h> //isdigit()
#include <vector> //vectors
#include <stdlib.h>
using namespace std;
int main()
{
string userInput = ""; //declaring and initialising a string called user input
char ans;
do {
cout << "Welcome to the calculator, please enter your calculation and then press enter when you are done." << endl;
cin >> userInput;
userInput.erase(remove_if(userInput.begin(), userInput.end(), isspace), userInput.end()); //removes and then erases any spaces in the string
userInput.erase(remove_if(userInput.begin(), userInput.end(), isalpha), userInput.end()); // removes and then erases any alphabetic charecters
//this will leave only numbers and operators
cout << userInput << endl;
cout << "Would you like to continue?" << endl;
cout << "Please enter 'y' or 'n'" << endl;
cin >> ans;
} while ((ans == 'y')||(ans == 'Y'));
return 0;
}