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

BenRichi_
  • 223
  • 1
  • 4
  • 21
  • Cannot reproduce. Your loop seems to work. However the code breaks on some input (e.g. containing `ä`). – wkl Apr 04 '17 at 09: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. – Bo Persson Apr 04 '17 at 09:42
  • @wkl I will get an image and add it to my post so you can see what I am getting. – BenRichi_ Apr 04 '17 at 09: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. – BenRichi_ Apr 04 '17 at 09:44
  • You should insert `userInput=""` after `cin >> ans;` – John Is No Apr 04 '17 at 09:45
  • @Ben - `getline` has a different problem when mixed with `>>` :-( See [Why does std::getline() skip input after a formatted extraction?](http://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – Bo Persson Apr 04 '17 at 10:30
  • This isn't the problem, but don't use `std::endl` unless you need the extra stuff that it does. `'\n'` ends a line. – Pete Becker Apr 04 '17 at 12:55

2 Answers2

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.)

Bathsheba
  • 220,365
  • 33
  • 331
  • 451
  • 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. – BenRichi_ Apr 04 '17 at 09:52
  • Did you change the type of `ans`? I would if I were you. I hate reading in a single `char`. – Bathsheba Apr 04 '17 at 09:52
  • 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 – BenRichi_ Apr 04 '17 at 09:54
  • Humour me and try it. – Bathsheba Apr 04 '17 at 09:54
  • @Bethsheba I changed it, but no difference – BenRichi_ Apr 04 '17 at 09:56
  • @BenRichi_ you're missing a scope resolution operator on `isspace` and `isalpha` . Try putting `::isspace` and `::isalpha` instead. It works on my computer. – mutantkeyboard Apr 04 '17 at 10:00
  • @mutantkeyboard I made the change you recommended but the issue is still the same, I do not get why this works on some computers but not mine... – BenRichi_ Apr 04 '17 at 10:02
  • @BenRichi_ here is the code -> https://pastebin.com/Ui5UTLDC ... please give it a try. Also make sure to clean the project, and rebuild again. And please try to avoid including `stdafx.h` – mutantkeyboard Apr 04 '17 at 10:04
  • @BenRichi_ what's inside your "stdafx.h" ? – John Is No Apr 04 '17 at 10:05
  • @mutantkeyboard If I do not include stdafx.h then Visual Studio hates everything and just throws up errors for everything – BenRichi_ Apr 04 '17 at 10:14
  • @BenRichi_ just create a new project, and when it asks you turn off the option for precompiled headers. More info -> http://stackoverflow.com/questions/21222922/when-do-i-want-to-turn-off-precompiled-header-in-visual-studio – mutantkeyboard Apr 04 '17 at 10:23
  • I think in this link you can find useful suggestions for what you want to solve: http://www.cplusplus.com/forum/articles/6046/ – Nicolò Ghielmetti Apr 04 '17 at 10:30
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!

BenRichi_
  • 223
  • 1
  • 4
  • 21