Well, I was bored and decided to make up a quick game in C++ and at the same time try to use all the new shit I need to learn like classes and such and incorporate it into the program. But one of the simplest errors is occuring. Everytime it hits a certain cin.getline it will skip it and continue without pausing for input. I've tried cin.ignore, cin.clear, blah blah but they all don't work for me... o_O
Anyway code for people to look at... It's probably not good but oh well.
class words {
public:
words();
void getWord();
void greeting();
void initFS();
void fileWrite();
private:
int i;
int a;
int time;
int numChars;
char word[102];
char name[30];
string strungWord;
};
void words::getWord() {
cout << "Please enter the word you would like to use: ";
while(a==1) {
// This is the part being skipped ----> cin.getline(word, 101);
for(i=1;i<3;i++) {
if(word[i] == '\0') {
cout << "That word is too short. Please type a longer one.\n";
a = 1;
break;
}
a = 0;
}
}
}
void words::initFS() {
a = 0;
cout << "Would you like to play in Fullscreen mode? (y/n)\n";
while(a==0) {
cin >> strungWord;
if(strungWord == "y") {
a = 1;
}
else if(strungWord == "n") {
a = 2;
}
else cout << "\"y\" or \"n\" was not entered. Please enter \"y\" or \"n\".\n";
}
if(a==1) {
keybd_event(VK_MENU, 0x38, 0, 0);
keybd_event(VK_RETURN, 0x1c, 0, 0);
keybd_event(VK_RETURN, 0x1c, KEYEVENTF_KEYUP, 0);
keybd_event(VK_MENU, 0x38, KEYEVENTF_KEYUP, 0);
}
cin.clear();
system("CLS");
}
void words::greeting() {
printf("Welcome to the \"Speed Typing\" game.\n"
"In this game you try to type a word as quickly as possible under a certain time limit.\n"
"Press any key when you are ready to begin.\n");
system("PAUSE");
system("CLS");
}
void words::fileWrite() {
cout << "Please enter your name for the scores record: ";
cin.clear();
cin.ignore(29, '\n');
cin.getline(name,29);
ofstream writeto("Scores Record.txt", ios::app);
if(a==1) {
writeto << endl << name << " beat level " << (i-1) << " with the word \"" << word << "\".";
}
else if(a==0) {
writeto << endl << name << " beat level " << (i) << " with the word \"" << word << "\".";
}
system("PAUSE");
}
words::words() {
time = 10;
initFS();
greeting();
getWord();
for(i=1;i<11;i++) {
cout <<"Prepare for level " << i << "..." << endl;
system("PAUSE");
cout << endl << "Go! Type in your word in less than " << time << " seconds!\n";
clock_t start_time;
start_time = clock();
cin >> strungWord;
if((clock() - start_time) > time * CLOCKS_PER_SEC) {
cout <<"Sorry, you didn't type the word quickly enough. You lose.\n";
a = 1;
fileWrite();
i = 11;
break;
}
else if(strungWord != word) {
cout << "Sorry, you didn't type the word correctly. You lose.\n";
a = 1;
fileWrite();
i = 11;
break;
}
else if(strungWord == word && (clock() - start_time) < time * CLOCKS_PER_SEC && i < 10) {
cout << "Congratulations, you progress to the next level.\n";
system("PAUSE");
system("CLS");
}
else if(strungWord == word && (clock() - start_time) < time * CLOCKS_PER_SEC && i == 10) {
cout << "Congratulations!! You've won the game by beating all levels!\n";
for(numChars=0;numChars<101;numChars++) {
if(word[numChars] == '\0') {
break;
}
}
cout << "Your word was " << numChars << " characters long!\n";
a = 0;
fileWrite();
i = 11;
break;
}
time--;
}
}
int main() {
words::words();
return 0;
}
In before "DON'T HELP HIM".
Name:
Anonymous2008-03-08 7:25
Too long; I didn't read it. Text book case of using the extraction operator (>>) before calling the getline method. >> leaves the newline in the buffer, getline consumes it and thus doesn't pause for input. Everywhere where you've used >>, use a cin.ignore() straight after it.
I've done that, as stated in my opening paragraph, and it doesn't work. I've tried it twice in a row even. I don't know why it doesn't, but cin.ignore is not working.
I rewrote the code and replaced all >> with getlines and such. The certain getline is still skipping.
I even deleted every other cin in the code except for the one that is skipping, and it still continues to skip.
Name:
Anonymous2008-03-08 23:00
Have you run it through a debugger and set it to break where the cin skips? If it doesn't break at that line, it's probably because of a logic error on your part.
>>15-17
Same nomad and we have been trolled constantly.
Name:
Anonymous2008-03-09 6:26
Question:
What does :: do in Haskell?
What does -> do in Haskell?
What does <- do in Haskell?
Name:
Anonymous2008-03-09 6:32
>>20
:: denotes type signatures
-> shows type transitions in function signatures (e.g. Int -> [Char] takes a small integer and returns a string)
<- temporarily assigns the results of executing the action on the right to the variable to the left (e.g. text <- getLine).
Now please answer the Sepples question.
Name:
Anonymous2008-03-09 6:57
>>21
:: is the scope operator, -> selects an element through a pointer and <- checks if the left hand argument is less than the negative of the right hand argument.
Name:
Anonymous2008-03-09 7:04
>>22 bullshit, no <- operator in sepples, consider this example a<-b+c; not the same as a<-(b+c) you described
-> shows type transitions in function signatures (e.g. Int -> [Char] takes a small integer and returns a string)
More formally, it is the type constructor for functions. It takes as parameters two type constructors of any kind, the first being the type the function expects as input, and the second being the result type (which may, of course, be another application of (→) to any two type constructors – this is why we can have so-called multi-parameter functions).
Also, in a lambda or a case, ``→'' is the delimiter between a pattern and the corresponding expression.
<- temporarily assigns the results of executing the action on the right to the variable to the left (e.g. text <- getLine).
That is how you think of it in imperative terms, but in actuality it is just syntactic sugar for binding (>>=) the expression on the right to a lambda with the pattern on the left, whose function body is the rest of the do-block. E.g.:
do
pat ← a
b
is desugared to:
a >>= λ pat → b
This also happens to be the reason why you cannot end a do-block with one of these ``assignments''.
Name:
Anonymous2008-03-09 7:54
Question:
What does -- do in Haskell?
What does >- do in Haskell?
What does -< do in Haskell?
Name:
Anonymous2008-03-09 8:29
Question:
What does <=> do in Haskell?
Name:
Anonymous2008-03-09 8:49
Question:
What does cdr do in LISP?
Name:
Anonymous2008-03-09 8:55
>>27
Access the content of the decrement register.
>>25 -< feeds the value of an expression into an arrow.
Name:
Anonymous2008-03-09 10:48
>>1
Some of your functions have branch cuts, across
which the function is discontinuous. For implementations
with a signed zero (including all IEC 60559 implementations)
that follow the specification of annex G, the sign of zero
distinguishes one side of a cut from another so the function
is continuous (except for format limitations) as the cut is
approached from either side. For example, for the square
root function, which has a branch cut along the negative
real axis, the top of the cut, with imaginary part +0, maps
to the positive imaginary axis, and the bottom of the cut,
with imaginary part -0, maps to the negative imaginary axis.
So what this means, OP, is that you should stick to VB.
Naturally I expected to have my code made fun of. I'm only just scraping the surface of C++.
And I only commented out when I typed it into /prog/, so that people may see the area being skipped. I guess I could have commented out the line before it and left the actual skipping part without commenting it out, but I thought the smart people of /prog/ would see it and figure they needed to take out the "// This is the part being skipped ---->".
Name:
Anonymous2008-03-09 17:37
>>42 when I typed it into /prog/
Now you have two problems.
Name:
Anonymous2008-03-09 17:40
cat myProgram.cpp |/prog/
Name:
Anonymous2008-03-09 18:01
>>44
I'm going to write a /dev/prog device (read it as ``DPD prog device'') that you can pipe to to create new threads in /prog/.