Hi,
I am fairly new to C++ and am struggling somewhat with keeping control of a simple console program that needs to extract input from cin (I want to keep to the Standard Library).
I am attempting to find an elegant way to extract strings from the console using the Microsoft V6 and Borland V5.5 compilers. I would like to have as much control as the C# Console.ReadLine().
I have come across a bug in the Microsoft Standard library which seems to rule out the simple solution of just using the string getline (
Here is the code I have come up with - it seems a clunky way to do it,
#include <iostream>
#include <string>
using namespace std;
const int MAX_STRING_LENGTH = 255;
/*
Microsoft libraries define _MSC_VER = 1200 for C++ compiler version 6
Borland libraries define __BORLANDC__
*/
class ConsoleInput
{
public:
#ifdef _MSC_VER
static string getStringLine()
{
char inputChar[MAX_STRING_LENGTH];
cin.getline(inputChar, MAX_STRING_LENGTH, '\n');
return inputChar;
}
#else
static string getStringLine()
{
string inputString;
getline(cin, inputString);
return inputString;
}
#endif
};
int main()
{
while(true)
{
cout << "Press q to quit or any key to continue => ";
string inputString = ConsoleInput::getStringLine();
cout << inputString << endl;
if( inputString[0]== 'q')
break;
}
return 0;
}
Aaron
I am fairly new to C++ and am struggling somewhat with keeping control of a simple console program that needs to extract input from cin (I want to keep to the Standard Library).
I am attempting to find an elegant way to extract strings from the console using the Microsoft V6 and Borland V5.5 compilers. I would like to have as much control as the C# Console.ReadLine().
I have come across a bug in the Microsoft Standard library which seems to rule out the simple solution of just using the string getline (
Here is the code I have come up with - it seems a clunky way to do it,
#include <iostream>
#include <string>
using namespace std;
const int MAX_STRING_LENGTH = 255;
/*
Microsoft libraries define _MSC_VER = 1200 for C++ compiler version 6
Borland libraries define __BORLANDC__
*/
class ConsoleInput
{
public:
#ifdef _MSC_VER
static string getStringLine()
{
char inputChar[MAX_STRING_LENGTH];
cin.getline(inputChar, MAX_STRING_LENGTH, '\n');
return inputChar;
}
#else
static string getStringLine()
{
string inputString;
getline(cin, inputString);
return inputString;
}
#endif
};
int main()
{
while(true)
{
cout << "Press q to quit or any key to continue => ";
string inputString = ConsoleInput::getStringLine();
cout << inputString << endl;
if( inputString[0]== 'q')
break;
}
return 0;
}
Aaron