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