Question

For the detecting of negative number;

#include <string>
#include <iostream>
#include <cmath>
using namespace std;

string command;
getline(cin,command);

if (command[0] == '-')
        encoder = -1;

and for the detecting of point;

  else if (command[x] == '.')
        {
            if (detector = 0)
            {
                floater = pow(10, -1 * x);
                detector = 1;
            }
            else
            {
                cout << "You have typed the wrong word or format.\n";

                return 0;
            }
        }

I use the ENU visual studio 2013 in Windows 8.1 K(Korean) Environment. When I typed '-' in first space or '.' in any space into "command"(string variable),

it just can't detect the - and.

and two sentences are just always ignored and passed.

What did I forget? Please help me.

Was it helpful?

Solution

The code you posted is not the real code, but chances are that the omission of a setlocale call reflects a similar omission in the real code.

So that's one thing to do, add a setlocale( LC_ALL, "" ) call at the start of main.

You should also take care to trim (remove undesired whitespace from) your input string, and to check whether there are any characters left after that, at all.

Also, the assignment in

if (detector = 0)

is most probably a typo.

Liberal use of const can help avoid such, and also the (in my opinion misguided, because it reduces readability) practice of writing the constant first in a comparison.

Also, using integers as boolean, instead of the bool type, as you apparently do, is ungood practice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top