Unexpected data

Hello, guys.
Happy New Year and Merry Christmas!

I try to display in console array, that user input, i try protect program from user, so when user input numbers of array that equal 0 or have negative value, program re-ask user to input correct value, but, when a user input, for example, - (minus without value) program go to close loop, have You can know, how to protect program from this error?

Thanks a lot!
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
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
int *B;                                                         //array B pointer
int n;                                                          //array B numbers

beginning:

cout<<"Please enter array B numbers: "<<endl;
cin>>n;

if(n<=0) 
	{
	cout<<"n - not correct input"<<endl;
	goto beginning;
	}

B = (int *) malloc (n*sizeof(int));                             //array B declaration


for (int i=0;i<n;i++)                                           //array B filling
	{
	cout<<"B["<<i<<"]= ";
	cin>>B[i];
	}


cout<<endl;

for (int i=0;i<n;i++)
	{
	cout<<"B["<<i<<"]= "<<B[i]<<endl;
	}

return 0;
}
closed account (SECMoG1T)
Hello, I would like to help but it seems like you are mixing both c and c++ stuff, gotos aren't so cool in c++ I'd recommend loops..

then reason your program behaves that way is because ( - ) will set an error state on your stream you can clear the stream to use it again.

1
2
3
4
5
if (! cin)
 {
    cin.clear ();  cin.ignore (1000,'\n'); //clear and empty the input buffer. 

 }
Last edited on
Why are you using malloc() instead of new?

Why are you using goto instead of one of the more acceptable loops like while() do{}while{} or for(;;)?


but, when a user input, for example, - (minus without value) program go to close loop,


Where is this "close loop" you are talking about?

Do you realize that the extraction operator knows that you can't insert a non-numeric value into an integer? If it detects an error it will set the input stream into an error condition and the variable will be default initialized (set to zero in this case)? You may want to check the state of the stream to insure it is not in an error state.



Let's use C++ new/delete
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
#include <iostream>

using namespace std;

int main()
{
    int n = 0;

    while (n <= 0)
    {
        cout << "Please enter how many numbers: " << endl;
        cin >> n;

        if (!cin ||  n <= 0)
        {
            cout << "n - not correct input" << endl;
            cin.clear();              // reset error flags
            cin.ignore(1000, '\n');   // discard remaining characters from input buffer
        }
    }


    int * B = new int[n];             // allocate memory for array

    cout << "\nPlease enter array numbers:\n";

    for (int i=0; i<n; i++)
    {
        cout << "B[" << i << "]= ";
        cin >> B[i];
        if (!cin)
        {
            --i;                      // reduce loop count by 1
            cout << "not correct input\n";
            cin.clear();              // reset error flags
            cin.ignore(1000, '\n');   // discard remaining characters from input buffer
        }
    }

    cout << endl;

    for (int i=0; i<n; i++)
    {
        cout << "B[" << i << "]= " << B[i] << endl;
    }

    delete [] B;                      // deallocate memory

    return 0;
}


Happy New Year and Merry Christmas!
Thanks a lot!
Topic archived. No new replies allowed.