-2

I am very new to c++ and am trying to program a simple calculator. I would like to add an input option were if you type 'ans' it will replace the first number with the answer to the previous calculation. Unfortunately I already defined that input as a float which causes it not to read any string input. If I defined that input as a string, the calculator would crash because you cant multiply two strings. This is the piece of code I am stuck on.

#include <iostream>

int main()
{
    float a;
    std::cin >> a;
    if (a == 'ans') {
        std::cout << "this is a string input";
    }
    else {
        std::cout << a * 2;
    }

}

I believe that when ever I enter 'ans' it goes straight to the else part of the if and tries to multiply a * 'ans' because it returns 0.

If anybody has any idea... Thanks

  • 3
    `a` is a `float`, how can it ever be equal to `'ans'`? – Tas Feb 27 '20 at 00:09
  • 4
    [std::stof](https://en.cppreference.com/w/cpp/string/basic_string/stof) – Mooing Duck Feb 27 '20 at 00:12
  • 1
    `'ans'` you likely used the wrong quotation marks. I don't think you wanted a multicharacter literal: [https://stackoverflow.com/questions/3960954/c-multicharacter-literal](https://stackoverflow.com/questions/3960954/c-multicharacter-literal) – drescherjm Feb 27 '20 at 00:12
  • 3
    @Tas Since `'ans'` is an `int` it's _technically_ possible to match, but it's certainly not what the OP was attempting to do. – N. Shead Feb 27 '20 at 00:13
  • Read it as a string, figure out if it's `ans` or a number, and then convert it to a number if needed. – eesiraed Feb 27 '20 at 00:13
  • There are a number of things going on here and I can’t post a real answer now, but in Sunday: a) C++ is statically-typed; before you ever run the program, `a` is and always will be a `float`. b) You should get at least two warnings on the `if` part because it does not do what you think; try enabling more warnings. c) When it fails to parse `ans` as a float, it fails, and uses the pre-existing value of `ans`; it could be anything or nothing, not just 0. d) What you need to do is read it in as a string, and use the library to parse that into a float yourself. – Daniel H Feb 27 '20 at 00:14
  • `if (c++ == 'python') ...` – Kaz Feb 27 '20 at 00:32

2 Answers2

2

My suggestion would be to take a std::string as input, check whether it is "ans" or any other string input, and then, if it is not, convert it to a float using std::stof.

Also, keep in mind that 'ans' is not a string, but a multicharacter literal, as others have noted. This is not what you want here. In C++, use single quotes for character literals and double quotes for string literals.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • would std::stos convert it back to a string? –  Feb 27 '20 at 00:52
  • @Whityj `std:stos` doesn't seem to exist (I had to check because I had not heard of it). Use `std::to_string()` to convert a numeric (variable) into a `std::string`. – Ted Lyngmo Feb 27 '20 at 01:01
1

There are many ways to overcome this problem. One of them is to use std::stof() which takes a std::string and size_t *, then returns the number contained in the std::string and in the same time the value of the var pointed to by the size_t * changes to the number of the processed chars of the std::string if its leading chars are numbers, as follows

#include <iostream>
#include<string>
int main()
{
    std::string a{};

    std::cout<<"Enter a thing: \n";
    std::cin >> a;
    size_t processedCharsNo{};

    float num{};
    if(!isalpha(a[0]))num = std::stof(a, &processedCharsNo);
    if (processedCharsNo == 0) {
        std::cout << "this is a string input";
    }
    else {
        std::cout << num * 2;
    }

}
asmmo
  • 6,922
  • 1
  • 11
  • 25
  • 1
    Nitpick: You do not need to initialize a variable that's never going to be read before it's written to (`processedCharsNo`), and this `float num {std::stof(a, &processedCharsNo)};` is generally better received as `float num = std::stof(a, &processedCharsNo);` – Ted Lyngmo Feb 27 '20 at 00:24
  • Just a note: When doing this kind of extraction, it's generally a good idea to check that it actually worked: `if(std::cin >> a) { /* ok proceed */ } else { /* nah, extraction failed completely */ }`. – Ted Lyngmo Feb 27 '20 at 01:07