I have input stream problem where my Console::ReadLine() command gets "eaten" when I try to loop my program back to start. Here is the code

#include "stdafx.h"

using namespace System;
int factorization();

int main(array<System::String ^> ^args)
{
	int terminate;

	do{
		terminate = factorization();
	}while(terminate == 'y' || terminate == 'Y');
	

	Console::ReadLine();
	return 0;
}

int factorization()
{
    Console::WriteLine(L"This application will factor any interger into its prime number form!");
	int intfac = 0;
	
	do{
	try
	{		
	    Console::Write(L"Please enter your integer:");
		intfac = Int32::Parse(Console::ReadLine());
	}
	catch (FormatException^)
	{
		Console::WriteLine(L"Your input is not an integer\n");
		intfac = 0;
	}
	}while(intfac == 0);

	array<int>^ data = gcnew array<int>(intfac);

	for( int i = 0; i<intfac; i++)
	{
		data[i] = 0;
	}

	int factor = intfac;

	for( int i = 0; i<factor ; i++)
	{
		while(factor%(i+2)==0)
		{
			factor /= (i+2);
			data[i]++;
		}
	}

	Console::Write(L"The unique factorization of {0} is:",intfac);
    Console::Write(factor);

	for( int i=0 ; i<intfac; i++)
		if (data[i] != 0)Console::Write(L"*({0})^{1}",i+2,data[i]);

	Console::Write(L"\n");
	Console::Write(L"Do you wish to continue?y/n:");
	return Console::Read();
}

the command: intfac = Int32::Parse(Console::ReadLine());
works fine when I initiate the program. However, it gets "eaten" when I loop the code back to start when I input 'y' in the end of factorization() function with this code line
return Console::Read();

It loops backs to begining and skip intfac = Int32::Parse(Console::ReadLine());, which cause the FormateException. This time, after the exception loop me back to the begining, the input command worked once again.

I want to know if there is anyway to fix this type of problem in C++/CIL programming

Console::Read() only reads a single key from the keyboard buffer. When you type 'Y' followed by a <Enter> key there are two keys, not one, in the keyboard buffer. You have to remove the '\n' so that Console::Readline() can work properly.

One way to fix that is to use Console::Readline(), not Read() to get the 'Y' or 'N' response. Another way is to flush the keyboard buffer of all keys by calling Console::Read() in a loop until it returns '\n',

What is the point of Console::Read() when we have Console::ReadLine()?
It seems that I have to do more work when using Console::Read() as input command.

and by Console::Read() returning '\n', does it mean that I have to overload the Console::Read() function so that when I type 'Y' then press <enter> it only reads 1 key or something?

Thanks for the solution for my problem. I guess I'll avoid using Console:Read()

Console::Read() only reads a single key from the keyboard buffer. When you type 'Y' followed by a <Enter> key there are two keys, not one, in the keyboard buffer. You have to remove the '\n' so that Console::Readline() can work properly.

One way to fix that is to use Console::Readline(), not Read() to get the 'Y' or 'N' response. Another way is to flush the keyboard buffer of all keys by calling Console::Read() in a loop until it returns '\n',

Thank you very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.