weird input skipping

Hey all!, I've made a class with will add fractions, but when I try to loop it in the driver code as shown below, it works great on the first iteration, but after that, it skips over the first input and all I can enter is the second fraction. for example on the second go-around, it will display
enter a fraction
enter another.
I would link the rest of the code, but its huge and annoying, and I believe (hope anyway) that the problem is here.

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
  #include "rational.h"

int main()
{
	char ans;
	int selection;
	rational *fraction1, *fraction2;

	
	do
		{

			fraction1 = new rational;
		    fraction2 = new rational;
			cout << "enter a fraction\n";
			cin >> *fraction1;
			cout << "enter another\n";
			cin >> *fraction2;
			cout << *fraction1 + *fraction2;
			cout << "go again y/n\n";
			cin >> ans;
			delete fraction1, fraction2;


	} while (ans == 'y');
	

}
lol my bad fixed it already, cin.ignore() so fail
I'm not sure the delete function behaves in the way you have used it above.

Example:
http://coliru.stacked-crooked.com/a/8fd67e9687ea2c14

To fix the issue, with the code, after the last call to cin, put this line:

1
2
3
4
#include <iostream>
#include <limits>
...
cin.ignore(numeric_limits<streamsize>::max(), '\n');


...And ninja'd by OP
Last edited on
That works like a champ too, thanks!
Topic archived. No new replies allowed.