Program Loop ignoring cin

I've have some basic knowledge in c and i'm rather new to c++. However, i've heard that there are problems with scanf and cin not reading in values to refresh the memory location when looping.

This is my code, I'm trying to loop the entire program. That's where the problem begins. When the user enters 'y' it reloops but ignoring any user input.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <math.h>

int main(void)

{
        float xmax, xmin, xtotal, ymax, ymin, ytotal, slope,x,y,xytotal,xy,loopn,xsquared,xsqtotal,xmean,ymean, yin,xrange,yrange;
        char choice;

        while(1)
        {
        system("cls");
        xtotal = 0;
        ytotal = 0;
        xytotal = 0;
        loopn = 1;



        cout << "Please enter in your first X and Y values. \nTo stop entering data, please press Ctrl-Z and hit Enter.\n\n";
        cout << "Value of x = ";
        cin >> x;
        cout <<"\n";
        cout << "Value of y = ";
        cin >>y;
        cout <<"\n";
        xy = x*y;
        xsquared = pow(x,2);
        xsqtotal = xsqtotal+xsquared;
        xytotal = xytotal+xy;
        xtotal = xtotal+x;
        ytotal = ytotal+y;

        xmax = x;
        xmin = x;
        ymax = y;
        ymin = y;


        while(!cin.eof())

        {
                cout << "Please enter your next X and Y values.\n\n";
                cout << "Value of x = ";
                cin >> x;
                cout <<"\n";
                cout << "Value of y = ";
                cin >>y;
                cout <<"\n\n";


                if (x>xmax)
                {
                        xmax = x;
                }

                if (x<xmin)
                {
                        xmin = x;
                }

                if (y>ymax)
                {
                        ymax = y;
                }

                if (y<ymin)

                {
                        ymin = y;
                }
                xtotal = xtotal+x;
                ytotal = ytotal+y;
                xy = x*y;
                xsquared = pow(x,2);
                xsqtotal = xsqtotal+xsquared;
                xytotal = xytotal+xy;
                loopn++;

        }

        xmean = xtotal/loopn;
        ymean = ytotal/loopn;

        slope = ((xytotal)-(xtotal*ymean))/((xsqtotal)-(xtotal*xmean));
        yin = ymean-(slope*xmean);
        xrange = xmax-xmin;
        yrange = ymax-ymin;

        cout << "The equation of the line which best fits the given data is: \n\ny=  "<<slope<<"x + "<<yin<<"\n\n";

        cout << "The range of x is = "<<xrange<<"\n\n";
        cout << "The range of y is = "<<yrange<<"\n\n";

        cout << "Would you like to key in another set of data? <y/n>\n";
        choice = getchar();
        if (choice=='n')
        break;

        else
        continue;


        }




}


I want to avoid having to use getchar() to input data though i remember vaguely that it could be used with more success. Please help!
try preceding each cin line with

cin.ignore(80, '\n');

It is possible that there is a stray \n in the stream, and that Cin is taking this as the input instead of waiting for something else EDIT: so this will flush the buffer for you
Last edited on
Thank you for the reply! Tried inserting one before the cin for x on line 24. Unfortunately when i ran the program, it ignored the first value i typed as an input to x. Had to type in a second value for it to input.

And even so I don't think the input went smoothly; I got a mathematical error(probably zero divisor) since I haven't included error checking loops yet..


EDIT:

Tried poking around searching for more solutions, found some code that was used to remove characters from error checking(i think)

Somehow, it worked in looping my entire program. With one flaw - the values were off =p. Obviously my understanding of the functions is next to nil, however, it's interesting to note that it Could loop after adding that - just with wrong values!

The code is:

1
2
cin.clear();
cin.ignore(cin.rdbuf()->in_avail());
Last edited on
before each use of cin, try the following:

1
2
3
if (!(cin))
    cin.clear();
//... 


that's what I normally do. It clears the failbit in cin. it's what I do, and it works fairly well, so give it a shot.
Thanks! Your code managed to loop the program. It interfered with the eof at line 42. I replaced that with a 'for' loop to test the values without it.

Worked well the first run. During the reloop, even though the same values were keyed in, the answer was different.

Strange because I included the initialization values within the loop. I even threw in a few extra to make all values involved in the calculation to be equal to 0 at the start of each loop.

EDIT:

I think i'm going to bed right now, will check on this first thing tomorrow morning. Any and All help is truly appreciated.
Last edited on
Instead of .eof() try .good() And don't use system clear please. There are alot of problems that come with system commands in general and my compiler wont even allow them.
Last edited on
@Taran: Hey, your code actually worked, it was MY mistake. The value problems were due to the fact that I didn't re-initialize ALL the values and my loop entered the last cin data during the loop with eof.

forgot to put
1
2
if(cin.eof())
          continue;

between lines 73 and 74.

@ui uiho: Thanks man, you helped me realize my mistake. Made me look back to see if there was problem with the eof. And i'm truly sorry about the system clear, my tutor asked for a cls somewhere in the program as part of the requirements..
Last edited on
if you want to do a clear screen you can do
std::string clear(100, '\n');
std::cout << clear;
this makes it safer and will achieve the same effect. i am not sure why your tutor would ask you to put a hole in your program, bringing that topic up with him/her might be a good idea.
Ahh, so this inputs 100 lines? Works fairly well but spacing is a bit off. No matter, perhaps playing with the value might work.

EDIT:
And just to clarify once and for all, I found that this code also worked to clear the cin buffer.(from post 3)

1
2
3
cin.clear();
cin.ignore(cin.rdbuf()->in_avail());


so, between this and:

1
2
if (!(cin))
    cin.clear();


How are they different and are there different implications/situations that they should and should not be used?
Last edited on
Topic archived. No new replies allowed.