cin을 오버로딩했는데 입력을 받지 않고 그냥 지나갑니다.

rjaechang의 이미지

입력을 연속으로 두번 받는데 첫번째 입력은 제대로 됩니다.
그런데 두번째 입력받으려 하면 이상하게 입력부분이 그냥 넘어가서 오류가 납니다.
fflush(stdin)을 해봐도 안되네요..

int main()
{
	Matrix A, B;	
	cin >> A;
	fflush(stdin);	
	cin >> B;	
	//A+B;
	return 0;
}

istream& operator >> (istream& is, Matrix& matrix)
{	
	Triple s;
	int p;
	is >> s.row >> s.col >> s.value;  //문제의 부분!!!!
	if(s.row > s.col)p = s.row;
	else p = s.col;
	matrix.headnode = new MatrixNode(FALSE, &s);		
	if(p == 0){
		matrix.headnode -> right = matrix.headnode;
		return is;
	}	
	MatrixNodePtr *head = new MatrixNodePtr[p];
	for (int i=0; i<p; i++)
		head[i] = new MatrixNode(TRUE, 0);
	int CurrentRow = 0;
	MatrixNode *last = head[0];
	for (i=0; i<s.value; i++){
		Triple t;
		is >> t.row >> t.col >> t.value;
		if(t.row > CurrentRow){
			last->right = head[CurrentRow];
			CurrentRow = t.row;
			last = head[CurrentRow];
		}	
		last = last->right = new MatrixNode(FALSE, &t);
		head[t.col]->next = head[t.col]->next->down = last;
	}	
	last->right = head[CurrentRow];
	for(i=0; i<s.col; i++)
		head[i]->next->down = head[i];
	head[p-1]->next = matrix.headnode;
	matrix.headnode->right = head[0];
	delete[]head;
	return is;
}
litdream의 이미지

다른것보다도, fflush(stdin) 하시면 안됩니다.

http://c-faq.com/stdio/stdinflush.html

http://c-faq.com/stdio/stdinflush2.html

삽질의 대마왕...

삽질의 대마왕...

rjaechang의 이미지

아.. 예전에 봤을때는 이해가 잘 안갔는데 이제 이해가 갑니다.

fflush는 출력스트림에만 정의되어 있는 함수군요.

좋은 답변 감사합니다.^^

(~`0`)~