fontenot@rice.edu (Dwayne J. Fontenot) (09/16/89)
Hello. I am having a problem with keyboard I/O in C++. System: GNU g++ version 1.34 Sun 3/280 My problem is this: I am implementing three functions which are members of the class `Command`. The first function, Command::GetCmd prints the prompt and reads a line of input into a character array called buf (char buf[MaxLine];). Then GetCmd needs to read the first character of the array and return some value based on what this character is. This works really well with the lines: cin.get(buf, MaxLine, `\n`); istream cmdStream(MaxLine, buf); cmdStream >> cmdChar; // where cmdChar is char cmdChar; Now the problem. I need the istream cmdStream to hang around after Command::GetCmd returns. I need to be able to call two other functions GetInt and GetString which operate on the same istream. I have tried various things to get CmdStream to hang around; declaring it as a private class data object, declaring it as static to GetCmd, and allocating it with `new`. I must be doing something wrong. When I try istream* CmdStream = new istream(MaxLine, buf); the compiler tells me "too few parameters to constructor". Any help on this problem would b greatly appreciated. Note that I have to structure to functions this way because this is for a class and the function headers have been declared for us. Note also that I am not `using the Net to do my homework` because I have spent approx. 20 hours on this problem already and neither my prof nor the labbies know C++ (this is the first year the prof has taught it and most of the labbies have not used the language before). If you are interested, code follows: // This file contains routines responsible for accepting and processing // the user's commands. #include <stream.h> #include <File.h> #include "Command.h" Cmd Command::GetCmd(char *prompt) { char cmdChar; char* buf = new char[MaxLine]; cout << prompt; cin.get(buf, MaxLine, '\n'); istream cmdStream = new istream(MaxLine, buf); if((istream)cmdStream >> cmdChar) return(SomeCmd); return(BadCmd); } int Command::GetInteger(int& value) { if(*streamPtr >> value) return(TRUE); value = 0; return(FALSE); } int Command::GetString(char *value) { if(cmdStream >> value) return(TRUE); value = "\0"; return(FALSE); } main() { Command myCommand; Cmd myCmd; char myString[MaxLine] = "default"; cout << "main: " << myCommand.GetCmd("minimal prompt: ") << "\n"; cout << "string: " << myCommand.GetString(myString) << myString << "\n"; }