0

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;
}

This is what I get in the Termninal

7
  • Cannot reproduce. Your loop seems to work. However the code breaks on some input (e.g. containing ä). Commented Apr 4, 2017 at 9:40
  • 1
    It might depend on how you type the input. cin >> userInput; only reads one word and stops at the first whitespace, leaving the rest in the input buffer. Commented Apr 4, 2017 at 9:42
  • @wkl I will get an image and add it to my post so you can see what I am getting. Commented Apr 4, 2017 at 9:44
  • @BoPersson I see, I was originally using getline but I thought that was the issue as to why it was skipping through. I would prefer top use getline. Commented Apr 4, 2017 at 9:44
  • You should insert userInput="" after cin >> ans; Commented Apr 4, 2017 at 9:45

2 Answers 2

4

The problem is that cin >> userInput; stops on the first whitespace, and my money is on a typical input string of yours containing plenty of them.

Change to

std::getline(std::cin, userInput);

This will gobble up a whole line of input, and deal with the newline character automatically for you.

For an easy life, do something similar with ans. Redefine it as a std::string. std::string even has == overloaded for a char type!

(Personally I'd also refrain from commenting standard #include files. Any C++ programmer should know what they "bring in", and this can lead to disinformation as your program expands.)

Sign up to request clarification or add additional context in comments.

12 Comments

So even with getline being used, the execution is exactly the same, it just skips right past the opportunity for me to enter another string.
Did you change the type of ans? I would if I were you. I hate reading in a single char.
I also do not understand why I would need to change the type of ans I have defined it as a char as the user will only be entering one character? @Bathsheba
Humour me and try it.
@Bethsheba I changed it, but no difference
|
0

So I have finally overcome my issue with help from @Bathsheba and @mutantkeyboard

I was using getline but only for the first instance of user input, like @Bathsheba suggests. This was not working for me as I then left the input for the ans as cin. Changing both of these to getline not just one of them and using a blank project without a pre-compiled header has solved the issue for me. The projects now loops correctly as expected.

Thank you for your help everyone!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.