data verification

Hello, im working on my homework and i have a problem when it comes to data verification.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
do
        {
        cout<<"Feljegyeztuk, hogy egymas koveto hetvegeken hany Forintot nyertunk vagy veszitettunk a loversenyen. Volt-e olyan napunk, amikoru ugy nyertunk, hogy a megelozo k hetvegen mindig veszitettunk?"<<endl<<endl;
        vector <double> feljegyzes; //vesztesegeket/nyeremenyeket tarolja
        int n, k; //n:a vektor merete(hany hetveget jegyzunk fel),k:az adott napot megelozoen hany napot vizsgalunk, hogy azok vesztesegesek voltak-e

        do{//bekeri a vektor meretet
         if(cin.fail())
            {
                cin.clear();
                cin.ignore();
            }

        cout<<"Adja meg hany hetveget kivan feljegyezni (pozitiv egesz szam):";
        cin>>n;
        if(n<1 || cin.fail())
            cout<<"Rossz erteket adott meg!"<<endl;
        }while(n<1 || cin.fail());//ellenorzi, hogy jo-e az ertek,0-nal nagyobbnak kell lennie

        do{//bekeri, hogy hany napot vizsgaljunk
        if(cin.fail())
            {
                cin.clear();
                cin.ignore();
            }

        cout<<endl<<"Adja meg hany hetveget kivan vizsgalni, hogy mind vesztes volt-e az adott hetveget megelozoen (0 es "<<n-1<<" kozotti szam):";
        cin>>k;
        if(k<0||k>=n || cin.fail())
            cout<<"Rossz erteket adott meg!"<<endl;
        }while(k<0||k>=n || cin.fail());//ellenorzi, hogy jo-e az ertek,nem lehet negativ es n-nel kissebbnek kell lennie

 feltolt(feljegyzes,n);


void feltolt(vector <double>& feljegyzes,int n)
{
    feljegyzes.resize(n);
    double a;

    for(int i=0;i<n;i++)
    {
      do
      {
        if(cin.fail())
            {
                cin.clear();
                cin.ignore();
            }

        cout<<"Adja meg a(z) "<<i+1<<". hetvege eredmenyet:";
        cin>>a;
        if(cin.fail())
            cout<<"Rossz erteket adott meg!"<<endl;
      }while(cin.fail());
      feljegyzes[i]=a;
    }
}


Everythings working fine until i give a floating number..
When its asking for the first value (n) and i give it e.g. 3.5 instead of asking the data again, it jumps to the next block, cout 2 or 3 times cout<<endl<<"Adja meg hany hetveget kivan vizsgalni, hogy mind vesztes volt-e az adott hetveget megelozoen (0 es "<<n-1<<" kozotti szam):";
this sequence and then asking for its value (k).. and again when i give a floating for k, it jumps to the next block, go in the for loop forr the first 1-3 loop, cout what is should but ignore cin and starts the appropriate working from that point
(it cout eg: Adja meg a(z) 1. hetvege eredmenyet:
Adja meg a(z) 2. hetvege eredmenyet:
Adja meg a(z) 3. hetvege eredmenyet:

and only ask for data at the third loop

hope sbody can help.. thx
Maybe you should make n a floating point type instead of an int.
but an integer fit better for its job.. and it doesnt seem to me a realy solution
and i also curious why c++ behave like this
but thx for your reply
If an integer is better for the job, why are you trying to feed it a floating point value?

If an integer is better for the job and you don't want input to fail when fed a floating point value, make sure the user didn't input a floating point value.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
        int n, k; //n:a vektor merete(hany hetveget jegyzunk fel),k:az adott napot megelozoen hany napot vizsgalunk, hogy azok vesztesegesek voltak-e

        bool valid_input = false ;
        do{//bekeri a vektor meretet
            if ( !cin )
            {
                cin.clear() ;
                cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            }


            cout<<"Adja meg hany hetveget kivan feljegyezni (pozitiv egesz szam):";
            if( !(cin >> n) || n < 1 || (cin && cin.peek() != '\n') )
                cout<<"Rossz erteket adott meg!"<<endl;
            else
                valid_input = true ;

        }while(!valid_input);//ellenorzi, hogy jo-e az ertek,0-nal nagyobbnak kell lennie 
Topic archived. No new replies allowed.