Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

cin skipping

Name: Anonymous 2008-03-08 7:18

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.



#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
#include <windows.h>
using namespace std;

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: Anonymous 2008-03-09 6:26

Question:
What does :: do in Haskell?
What does -> do in Haskell?
What does <- do in Haskell?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List