0

I checked many links on Stack and to other site. Most of it does it what is supposed to do, but not "exactly" how I want to. Here is the problem and the best solution that i found on online. I want to enter a number, float for example and input should be checked if is a float or not. I'm using this snippet found online. Also to mention that i need to repeat validation each time for loop its iterated.The "n" is entered separately before this function and its in "private".It does its job perfectly, except ...If you enter "55" (number), it checks and validates. That's ok. If you enter "dfgfd" stuff (not a number), it checks and repeats question. That's ok. If you enter "dfgdfgdg55",it checks and repeats question. It's also ok. If you enter "55dfgfd" stuff, it checks and NOT repeats. That's NOT OK.It just discarding characters after numbers.I want to discard this also.So correct input should be JUST "55" entered.(Number 55 is just a example number entered when prompted).Also I tried this on a simpler function "model".First to enter "55", second to enter "55gdf". They been presented on screen as "55" and "55". Then i added some code afterwards to compare these numbers. They are not the same!

    #include <iostream>
    using namespace std;

    int Provera_kucanja()
    {

        cout<<endl;
        cout<<"Input boundary for an array"<<endl;

        cin>>n;

         while (cin.fail() || !(n>0))                                          
            {                                                                
                cin.clear();                                                  
                cin.ignore(numeric_limits<streamsize>::max(), '\n');          
                cout<<"You didnt entered number, please try again... "<<endl;         
                cin>>n;                                                      
            }                                                         

            cout<<endl;
    return 0;

    }

    float Unos_brojeva()
        {

            cout<<"\n";
            cout<<"Now you must enter number into array:\n "<<endl;

            for (int i = 0; i < n ; i++)
            {
                cout<<"Input "<<"["<<i<<"]"<<" number in array: ";                    
                float r;
                cin>>r;

                while (cin.fail())
                {                                                                
                    cin.clear();
                    cin.ignore(numeric_limits<streamsize>::max(), '\n');
                    cout<<"You didnt entered number, please try again... "<<endl;
                    cout<<"Input "<<"["<<i<<"]"<<" number in array: "; 
                    cin>>r;
                }

                unos[i]=r;

            }

            cout<<endl;
            cout<<"Now show unsorted array members: "<<endl;                              
            for(int i = 0; i < n; i++)
            {
                cout<<"This is "<<"["<<i<<"]"<<" member of array named 'unos': "<<unos[i]<<endl;

            }
            cout<<"\n"<<endl;
            cin.get();
            return 0;
        }

         int main(){
         Provera_kucanja();
        Unos_brojeva();
          }

On the down side using suggested answers is that when user enters something like "ghfg" , result of this conversion is a ZERO!. So suggested answers is no-go's.

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;

int main()
{
    string A_number;
    char* An_array=new char[A_number.length()];
    cout<<"Enter value for A_number: ";
    cin>>A_number;
    strcpy(An_array,A_number.c_str());
    float n=atof(An_array);
    cout<<"Value entered is: "<<n;
    cin.get();
    return 0;
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • Exact problem is when i input 55ffghfg stuff is that "ffghfg" is passed to the next member in array as a value, thus this behaviour causes thats first number is 55(thats ok), but creates error and it repeats input for next member even if nothing yet entered for that number! – Veljko Stefanovic Aug 30 '14 at 23:23
  • That is what `cin` does. It just tries to read the data type you asked for (in this case, float), and if it gets one the input pointer stays at the next byte of input data. If you want to consume the rest of the line you need to read in a string up to and including the newline character after you get your float. – par Aug 30 '14 at 23:26
  • I just translated my prompts to english. – Veljko Stefanovic Aug 30 '14 at 23:57
  • @VeljkoStefanovic I wrote an answer that addresses that problem here -- http://stackoverflow.com/questions/23583733/error-when-user-input-numbers-characters – David G Aug 31 '14 at 01:14
  • I saw it just now. Extract only usable part of a string, discard the rest.Then the rest is used in next input, then while loop discards the rest again, now you can enter valid input for second iteration of loop. I wish it can just discard all of it and not tranfer junk part of a string to next iteration, but that the rule. – Veljko Stefanovic Aug 31 '14 at 02:30
  • @VeljkoStefanovic See if replacing `err |= ...` with `return end` helps. – David G Aug 31 '14 at 03:42
  • Dont what are you trying to say ... where is this "err |= ..." and only references i found, is vector container where is used "end" in for iteration loop .Usage of vectors is not allowed in my current assigment if actualy you meant vectors. Basicly what i found so far about passing junk strings into next input is this: If you have while loop that "loops" when is error is found, its correct its error by loop it again and again until you enter acceptable input. It still enoying that it passed it junk string, but at least it allows you to correct its input in next loop pass. – Veljko Stefanovic Aug 31 '14 at 15:54

2 Answers2

0

The line:

cin >> r

is going to try to read a valid float, even if there is bad trailing information after it.

You are saying the problem occurs when someone enters 55x, in this case you think it should not be considered a valid number.

There is an answer to your problem here: https://stackoverflow.com/a/19717896/312594

What you have to do is read the entire data in as a string and confirm that only characters that are possible as float input (digits, ., +, -, etc.) are in the input, and in the correct possible order. Only after you validate the input do you convert the input to float (via cin or some other mechanism).

In most cases you should accept 55x as 55 since that's probably what the user wants, but if you do want to be strict you have to perform the additional validation yourself.

p.s. - I almost hate to say this as I do not want to sound biased, but as you're learning C++ you may find it useful to write all of your code, including prompts, in English. If you later ask for help on StackOverflow, it will be easier for more people to understand what you are trying to do and therefore to help you out.

Community
  • 1
  • 1
par
  • 17,361
  • 4
  • 65
  • 80
  • So basicly i need to enter value into string type variable, then convert it into float? I just dont want to do any kind a conversion, just checks if value is of correct type. – Veljko Stefanovic Aug 31 '14 at 00:07
  • That's one way to do it, yes, but it's also a lot of work, *and* you might be wrong. The user may know that your program will ask for a number then a string and just type 55x. Your program is simple but in some cases that may be correct. In the case of 55x, cin will put 55 in r, and you should ASSUME that is correct. On the next iteration of your loop your code will see the x, *then* it should return an error and say invalid input. In other words, don't try to correct all possible *future* errors. If you expect a float *now*, then look for a float with cin >> r. Does that make sense? – par Aug 31 '14 at 02:05
  • Yeah, i know it man. Nobody can predict all possible errors and warnings. Hope that will change when my future programming skills grows stronger :) Thanks man. – Veljko Stefanovic Aug 31 '14 at 02:24
-1

Sounds like you want to read in the input as strings (or possibly whole lines), and then you can test those strings any way you like.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • I just want to check if its inputed value is a number.As i stated above "55" is only correct answer.Not "55fddfg" of "sdfsf55" or just "dgfg".So just checking if inputed value is a CLEAN NUMBER and nothing else.For example if you input "55dgfdf" it is shown as number 55.Checking must be complete.Only and only number should be inputed. – Veljko Stefanovic Aug 30 '14 at 23:00
  • Is any way possible for complete input checking? Links or answers please help – Veljko Stefanovic Aug 30 '14 at 23:02