Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with inputting multidimensional string arrays....
#81
Quote:wait a sec, bunk that. I'm not done (though I did finally get it to compile the release version). Is there any way to make DATA statements (like in QB) in C++?

Nope, it was a stupid idea then, so they took the bastard out.
am an asshole. Get used to it.
Reply
#82
Think about it: you don't need'em, 'cause C has the incredibly cool feature of leting you giving initial values to arrays and structures, so you don't need DATA and those READs inside loops to fill'em:

Code:
typedef struct
{
   int a;
   float f;
   char s[10];
} MYSTRUCT;

MYSTRUCT array[3] = { {1, 2.0, "Wee"},
                      {4, 7.898, "Thanatos"},
                      {0, 0.023, "Billy Corgan"} };

Nice uh? Wink
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#83
Ya, but in C, when you declare an array, it's how many elements there are, not what it goes up to. So.... you'd have to take out your last {} in there.

int array[5] has elements 0-4, k?

::EDIT::
C doesn't check to see if your accessing is going past bounds, so it'll access the memory directly following your array.
am an asshole. Get used to it.
Reply
#84
Alright. I'm really done now. Here is a link to the program (nothing special, but it might be fun to play for 30 seconds Big Grin ).
http://www.geocities.com/typosoft/guess.zip
(right click and save target as)

the source is also in the download. I'd very much appreciate someone to point out my mistakes and to show me how I could write better code.

Here is the source:
Code:
#include "stdafx.h"
#include "stdafx.h"
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <conio.h>
#include <iostream>
#include <windows.h>
#include <ctime>
#include <fstream>
#include <io.h>

using namespace std;

void must_guess(void);
void win_game(void);
void draw_play(void);
void do_play(void);
int set_play(void);
char *pick_file(char *s, char *search, int &hit_ret);
int rnd(int lowest, int highest);
char **dir_file(char **files, char *search, int& num_files, int& biggest);
int dir_file_info(int& num_files, int& biggest);
int load_rand(void);
void display_menu(int highlight);
void edit_word_entries(void);
char *handle_input(char *s, char *printwhat, char *errmsg);
int init_game(void);
int load_word(char *filename);
int make_word(void);
int main_menu(void);
int quit_game(void);
void setcolor(unsigned short color);
void wait_key(void);
static char *ltrim(char *s);
static char *rtrim(char *s);
static int get_code (void);
void delay(int millisecs);

enum
{
  KEY_ESC     = 27,
  KEY_ENTER   = 13,           
  ARROW_UP    = 256 + 72,
  ARROW_DOWN  = 256 + 80,
  ARROW_LEFT  = 256 + 75,
  ARROW_RIGHT = 256 + 77,
  KEY_DEL     = 256 + 83
};

typedef struct {
    int tokens, level, turns, score, show_def, show_word;
} game_struct;

typedef struct {
    char *theword;
    char *definition;
    char *filename;
    int clues;
} word_struct;

game_struct *game;
word_struct *word;
char *clue[10];
int show_clue[10] = {0,0,0,0,0,0,0,0,0,0}, end_game = 0;

int main(void) {
    int i, quit = 0;
    init_game();
    do {
        i = main_menu();
        display_menu(i);
        if (i == 1) {
            if (load_rand()) continue;
            if (set_play()) continue;
            do {
                draw_play();
                do_play();
            } while (!end_game);
            if (end_game == 3) {
                system("cls"); setcolor(12);
                cout << "SORRY! You lose....try again.";
                setcolor(7); wait_key();
            }
        }    
        if (i == 2) {
            setcolor(15);
            cout << endl << "- Instructions - " << endl << endl;
            setcolor(7);
            cout << "Playing Guess that Word is simple.  Just select option 1 from the main menu and" << endl;
            cout << "select the difficulty level you wish to play with.  The higher the difficulty," << endl;
            cout << "the higher your score will be." << endl << endl;
            cout << "Each time you play you are given some tokens.  Tokens are used to buy clues" << endl;
            cout << "(which are 1 point each), to buy the definition (2 points), or to guess the" << endl;
            cout << "word (1 point each).  See if you can't guess every word!" << endl << endl;
            cout << "(press any key)";
            wait_key();
        }
        if (i == 3) make_word();
        if (i == 4) quit = quit_game();
    } while(!quit);
    return 0;
}


int load_rand(void) {
    int i, num_files = 0, biggest = 0, rval = 0;
    char **files;
    files = dir_file(files, "*.dat", num_files, biggest);
    if (!num_files) return 1;
    if (load_word(files[rnd(0, num_files)])) rval = 2;
    for (i = 0; i < num_files; i++)    free(files[i]);
    free(files);
    return rval;
}

char **dir_file(char **files, char *search, int& num_files, int& biggest) {
    int i = 0;
    struct _finddata_t file;
    long hFile;
    if((hFile = _findfirst(search, &file)) == -1L) {
        setcolor(12); cout << endl << "No files found in current directory!";
        setcolor(7); cout << " (press any key)";
        num_files = 0; biggest = 0;
        wait_key();
        return NULL;
    }
    else {
        do {
            num_files++;
            if (strlen(ltrim(rtrim(file.name))) > biggest) biggest = strlen(ltrim(rtrim(file.name)));
        } while(_findnext(hFile, &file) == 0 );
        _findclose(hFile);
    }
    files = (char **) malloc (num_files * sizeof(char*));
    for (i = 0; i < num_files; i++)
        files[i] = (char *) malloc ((biggest + 1) * sizeof(char));
    i = 0;
    if((hFile = _findfirst(search, &file)) == -1L) {
        printf("No files in current directory!\n");
        return NULL; }
    else {
        do {
            strcpy(files[i], ltrim(rtrim(file.name)));
            i++;
        } while(_findnext(hFile, &file) == 0 );
        _findclose(hFile);
    }
    return files;
}

void display_menu(int highlight) {
    system("cls");
    setcolor(15);
    cout << ".............GUESS THAT WORD..............." << endl << endl;
    if (highlight == 1) setcolor(15); else setcolor(7);
    cout << "1) New Game" << endl;
    if (highlight == 2) setcolor(15); else setcolor(7);
    cout << "2) Instructions" << endl;
    if (highlight == 3) setcolor(15); else setcolor(7);
    cout << "3) Edit files" << endl;
    if (highlight == 4) setcolor(15); else setcolor(7);
    cout << "4) Quit" << endl;
    setcolor(7);
}


void edit_word_entries(void) {
    int i, ii = 0;
    char tmpc[80], sel;
    while (1) {
        system("cls");
        cout << "FILENAME: "; setcolor(15);
        cout << word->filename << endl; setcolor(7);
        cout << "WORD: " << word->theword << endl;
        cout << "DEFINITION: " << word->definition << endl;
        cout << "CLUES: " << word->clues << endl;
        for (ii = 0; ii < word->clues; ii++) {
            cout << "   CLUE " << ii + 1 << ": " << clue[ii] << endl;
        }
        setcolor(15); cout << endl << "What do you want to do?" << endl << endl; setcolor(7);
        cout << "1) edit WORD" << endl << "2) edit DEFINITION" << endl << "3) edit NUMBER of CLUES" << endl << "4) edit a CLUE" << endl << "5) Save" << endl << "6) Exit" << endl;
        
        sel = getch();
        if (sel == '1') {
            word->theword = ltrim(rtrim(handle_input(word->theword, "Word: ", "ERROR: You must type a word!")));
            continue;
        }
        
        if (sel == '2') {
            word->definition = ltrim(rtrim(handle_input(word->definition, "Definition: ", "ERROR: You must type a definition!")));
            continue;
        }
        
        if (sel == '3') {
            ii = word->clues;
            word->clues = 0;
            while (!word->clues || word->clues > 10) {
                cout << endl << "Clues (1-10): ";
                scanf("%d", &word->clues);
            }
            cin.ignore(1, '/n');
            if (word->clues > ii) {
                for (i = ii; i < word->clues; i++) strcpy(clue[i], "-empty-");
            }
            continue;
        }
        
        if (sel == '4') {
            ii = 0;
            do {
                cout << "Edit which clue (1 - " << word->clues << "): ";
                scanf("%d", &ii);
            } while (!ii || ii > word->clues);
            cin.ignore(1, '/n');
            sprintf(tmpc, "   Clue %d: ", ii);
            clue[ii - 1] = ltrim(rtrim(handle_input(clue[ii - 1], tmpc, "   ERROR: You must type in a clue!")));
            continue;
        }
        
        if (sel == '5') {
            char *finame = (char *) malloc(80 * sizeof(char));
            finame = ltrim(rtrim(handle_input(finame, "SAVE AS: ", "ERROR: You must type a valid filename")));
            ofstream outfile (finame);
            if (!outfile.is_open()) {
                setcolor(12);
                cout << "ERROR creating " << finame << " (press any key)";
                setcolor(7); wait_key(); break;
            }
            else {
                outfile << word->theword << endl;
                outfile << word->definition << endl;
                outfile << word->clues << endl;
                for (ii = 0; ii < word->clues; ii ++) {
                    outfile << clue[ii] << endl;
                }
                outfile.close();
                cout << endl << "SAVE COMPLETE (press any key)";
            }
            wait_key();
            free(finame);
            break;
        }
        if (sel == '6') break;
    }
}

char *handle_input(char *s, char *printwhat, char *errmsg) {
    char buff[80];
    do {
        cout << endl << printwhat;
        fgets(buff, 80, stdin);
        strcpy(s, buff);
        if (!strlen(ltrim(rtrim(s)))) {
            setcolor(12);
            cout << endl << errmsg << endl;
            setcolor(7);
        }
    } while (!strlen(ltrim(rtrim(s))));
    return s;
}

int init_game(void) {
    int i;
    srand(time(NULL));
    game = (game_struct *) malloc(sizeof(game_struct));
    word = (word_struct *) malloc(sizeof(word_struct));
    word->theword = (char *) malloc(80 * sizeof(char));
    word->definition = (char *) malloc(80 * sizeof(char));
    word->filename = (char *) malloc(80 * sizeof(char));
    strcpy(word->filename, "untitled.txt");
    word->clues = 0;
    for(i = 0; i < 10; i++) {
        clue[i] = (char *) malloc(80 * sizeof(char));
    }
    return 0;
}

int load_word(char *filename) {
    char buff[80];
    int i;
    ifstream infile(filename);
    if (!infile.is_open()) return 1;
        infile.getline(buff, 80);
        strcpy(word->theword, buff);
        infile.getline(buff, 80);
        strcpy(word->definition, buff);
        infile.getline(buff, 80);
        word->clues = atoi(buff);
        for (i = 0; i < word->clues; i++) {
            infile.getline(buff, 80);
            strcpy(clue[i], ltrim(rtrim(buff)));
        }    
    infile.close();
    strcpy(word->filename, filename);
    return 0;
}

int make_word(void) {
    int ii = 0, hit_ret = 0;
    char tmpc[80], sel;
    word->clues = 0;
    while (1) {
        system("cls");
        setcolor(15); cout << "- What do you want to do? -" << endl << endl; setcolor(7);
        cout << "1) Create a new file" << endl;
        cout << "2) Edit existing file" << endl;
        cout << "3) Main menu" << endl;
        
        sel = getch();

        if (sel == '1') {
            system("cls");
            setcolor(15); cout << "- Create a new word -" << endl; setcolor(7);
            word->theword = ltrim(rtrim(handle_input(word->theword, "Word: ", "ERROR: You must type a word!")));
            word->definition = ltrim(rtrim(handle_input(word->definition, "Definition: ", "ERROR: You must type a definition!")));
            while (!word->clues || word->clues > 10) {
                cout << endl << "Clues (1-10): ";
                scanf("%d", &word->clues);
            }
            cin.ignore(1, '/n');
            for (ii = 0; ii < word->clues; ii++) {
                sprintf(tmpc, "   Clue %d: ", ii + 1);
                clue[ii] = ltrim(rtrim(handle_input(clue[ii], tmpc, "   ERROR: You must type in a clue!")));
            }
            edit_word_entries();
            continue;
        }
        if (sel == '2') {
            hit_ret = 0;
            word->filename = ltrim(rtrim(pick_file(word->filename, "*.dat", hit_ret)));
            if (hit_ret == 2) continue;
            if (load_word(word->filename)) {
                setcolor(12); cout << endl << "ERROR: File not found!"; setcolor(7);
                cout << " (press any key)"; wait_key();
                continue;
            }
            edit_word_entries();
            continue;
        }
        if (sel == '3') break;
    }
    return 0;
}

int main_menu(void) {
    int good_sel = 0;
    char sel;
    display_menu(0);
    do {
        sel = getch();
        if (sel == '1') good_sel = 1;
        if (sel == '2') good_sel = 2;
        if (sel == '3') good_sel = 3;
        if (sel == '4') good_sel = 4;
    } while (!good_sel);
    return good_sel;
}

int quit_game(void) {
    int quit = 0, i = 0;
    char sel;
    display_menu(4);
    cout << endl << "Do you really want to quit (y/n)?";
    while (!quit) {
        sel = toupper(getch());
        if (sel == 'Y') quit = 1;;
        if (sel == 'N') break;
    }
    if (quit) {
        free(game);
        free(word->theword);
        free(word->definition);
        free(word->filename);
        free(word);    
        for(i = 0; i < 10; i++) {
            clue[i] = NULL;
            free(clue[i]);
        }
    }    
    return quit;
}

void setcolor(unsigned short color)
{
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon,color);
}

void wait_key(void) {
    while(kbhit()); while(!(kbhit()));
}

static char* ltrim(char* s) {
    if (s) { while (isspace(*s)) s++; }
    return s;
}

static char* rtrim(char* s) {
    int i;
    if (s) { i = strlen(s); while ((--i)>0 && isspace(s[i])) s[i]=0; }
    return s;
}

void delay(int secs) {
    clock_t start_time;
    start_time = clock();
    while((clock() - start_time) < secs * CLOCKS_PER_SEC);
}

int rnd(int lowest, int highest) {
    return lowest + int((highest - lowest) * rand() / (RAND_MAX + 1.0));
}

char *pick_file(char *s, char *search, int &hit_ret) {
    int a, num_files = 0, biggest = 0, i = 0, ii = 0, sel = 0, ch = 0, refresh = 1;
    char **files, sell;
    files = dir_file(files, search, num_files, biggest);
    if (!num_files) {
        hit_ret = 2; return NULL;
    }
    do {
        if (refresh) {
            system("cls");
            ii = 0;    setcolor(15);
            cout << "Choose a file to load..." << endl << endl; setcolor(7);
            for (i = 0; i < num_files; i++) {
                if (sel == i) setcolor(15); else setcolor(7);
                cout << files[i]; setcolor(7);
                if (strlen(files[i]) < 16) {
                    for (a = strlen(files[i]); a < 16; a++) cout << " ";
                }
                ii++;
                if (ii == 5) { ii = 0; cout << endl; }
            }
            cout << endl << endl << "Found " << num_files << " files" << endl;
            refresh = 0;
        }
        ch = get_code();    
        if (ch == ARROW_RIGHT) {
            sel++;
            if (sel > num_files - 1) sel = 0;
            refresh = 1;
        }            
        if (ch == ARROW_LEFT) {
            sel--;
            if (sel < 0) sel = num_files - 1;
            refresh = 1;
        }            
        if (ch == ARROW_DOWN) {
            sel += 5;
            if (sel > num_files - 1) sel = num_files - 1;
            refresh = 1;
        }
        if (ch == ARROW_UP) {
            sel -= 5;
            if (sel < 0) sel = 0;
            refresh = 1;
        }
        if (ch == KEY_DEL) {
            cout << endl << "Do you want to delete " << files[sel] << " (y/n): ";
            while (1) {
                sell = toupper(getch());
                if (sell == 'Y') {
                    remove(files[sel]);
                    cout << endl << endl << "DELETED: " << files[sel];
                    delay(1); num_files = 0; biggest = 0; refresh = 1;                
                    files = dir_file(files, search, num_files, biggest);
                    if (num_files == 0) {
                        setcolor(12); cout << "ERROR: There are no files in the current directory";
                        setcolor(7); cout << " (press any key)";
                        wait_key();    free(files); return " ";
                    }
                    break;
                }
                if (sell == 'N') { refresh = 1; break; }
            }
        }
        if (ch == KEY_ENTER) hit_ret = 1;
        if (ch == KEY_ESC) hit_ret = 2;
    } while (!hit_ret);
    if (hit_ret == 1) strcpy(s, files[sel]);
    for (i = 0; i < num_files; i++)    free(files[i]);
    free(files); cout << endl;
    return s;
}

static int get_code (void) {
  int ch = getch();
  if (ch == 0 || ch == 224) ch = 256 + getch();
  return ch;
}

int set_play(void) {
    int i;
    char sel;
    game->level = 0;
    game->turns = 0;
    game->show_word = 0;
    end_game = 0;
    for (i = 0; i < word->clues; i++) show_clue[i] = 0;
    system("cls");
    setcolor(15); cout << "- What level of difficulty do you want to play? -" << endl << endl;
    setcolor(7); cout << "1) Easy" << endl << "2) Normal" << endl << "3) Hard" << endl << "4) Exit" << endl;
    while (1) {
        sel = getch();
        if (sel == '1') { game->level = 1; game->tokens = 10; game->show_def = 1; return 0;}
        if (sel == '2') { game->level = 2; game->tokens = 7; game->show_def = 0; return 0;}
        if (sel == '3') { game->level = 3; game->tokens = 3; game->show_def = 0; return 0;}
        if (sel == '4') return 1;
    }
}

void draw_play(void) {
    int i;
    system("cls");
    setcolor(15); cout << "- Guess that word - " << endl << endl;
    setcolor(7);
    if (game->show_word) cout << "WORD: " << word->theword << endl; else cout << "WORD: ???"<< endl;
    if (game->show_def) cout << "DEFINITION: " << word->definition << endl; else cout << "DEFINITION: ???" << endl;
    for (i = 0; i < word->clues; i++) {
        if (show_clue[i]) cout << "CLUE " << i + 1 << ": " << clue[i] << endl; else cout << "CLUE " << i + 1 << ": ???" << endl;
    }
    cout << endl << "TOKENS: " << game->tokens << "    TURNS TAKEN: " << game->turns << endl << endl;
    cout << "1) Buy CLUE (1 token)" << endl;
    cout << "2) Buy DEFINITION (2 tokens)" << endl;
    cout << "3) Guess WORD (1 token)" << endl;
    cout << "4) Quit" << endl;
}

void do_play(void) {
    char sel;
    int which_clue;
    int match = 0;
    char *guess = (char *) malloc(80 * sizeof(char));
    
    
    while (1) {
        sel = getch();
        if (sel == '1') {
            cout << endl << "Buy which clue (1-" << word->clues << ") 0 = cancel: ";
            scanf("%d", &which_clue);
            cin.ignore(1, '/n');
            if (!which_clue) break;
            if (which_clue > word->clues) {
                cout << endl << "That is an invalid clue number! (press any key)";
                wait_key(); break;
            }
            if (show_clue[which_clue - 1]) {
                cout << endl << "You already know that clue! (press any key)";
                wait_key(); break;
            }
            else {
                game->turns++;
                show_clue[which_clue - 1] = 1;
                game->tokens--;
                if (game->tokens == 0) must_guess();
                break;
            }
        }
        
        if (sel == '2') {
            if (game->show_def) {
                cout << endl << "You already know the definition! (press any key)";
                wait_key(); break;
            }
            if (game->tokens >= 2) {
                game->turns++;
                game->show_def = 1;
                game->tokens -= 2;
                if (game->tokens == 0) must_guess();
                break;
            }
            else {
                setcolor(15); cout << endl << "You don't have enough tokens! (press any key)";
                wait_key(); setcolor(7); break;
            }
        }
        
        if (sel == '3') {
            game->turns++;
            guess = ltrim(rtrim(handle_input(guess, "GUESS: ", "You must guess a word!")));
            game->tokens--;
            if (!strcmpi(ltrim(rtrim(guess)), ltrim(rtrim(word->theword)))) {
                win_game();
                end_game = 2;
                break;
            }
            else {
                setcolor(12); cout << endl << "WRONG!";
                delay(1); setcolor(7); break;
            }
            if (!game->tokens) end_game = 3;
            break;
        }
        
        if (sel == '4') {
            end_game = 1;
            break;
        }
    }
    free(guess);
}

void win_game(void) {
    system("cls");
    setcolor(15);
    cout << "Congratulations!" << endl << endl;
    setcolor(7);
    game->score = ((((game->tokens + 5) * 10) / game->turns) + (game->level * 100) + 30);
    cout << "You correctly guess the word " << word->theword << " in " << game->turns << " turns!" << endl;
    cout << "You have " << game->tokens << " tokens remaining with a difficulty level of " << game->level << endl;
    cout << "Your overall score: " << game->score << endl << endl;
    cout << "Now you should play again but with a harder difficulty.";
    cout << endl << endl << "(press any key)";
    wait_key();
}

void must_guess(void) {
    char *guess = (char *) malloc(80 * sizeof(char));
    cout << endl << "You are out of tokens. Now you must guess the word. (press any key)" << endl;
    wait_key();
    guess = ltrim(rtrim(handle_input(guess, "GUESS: ", "You must guess a word!")));
    if (!strcmpi(ltrim(rtrim(guess)), ltrim(rtrim(word->theword)))) {
        win_game();
        end_game = 2;
    }
    else end_game = 3;
    free(guess);
}
am: "Where should I put this thing so that it doesn't hurt anyone we know or care about?"
Max:"Out the window, Sam. There's nobody but strangers out there."
Reply
#85
Quote:
Code:
fflush(stdin);
Will clear the stdin buffer, which is used for input. It works with scanf, gets and C stuff, I dunno if it will work with that cin/cout scary/evil stuff .

Thats what I thought too, buts its wrong. Standard C only defines fflush for operation on output streams. Its behaviour is undefined when used with an input streams and may cause problems. This pages explains why and also has some really good information on getting input with C.

http://home.tiscalinet.ch/t_wolf/tw/c/ge...input.html

Unfortunately, Standard C doesn't provide any solution for getting rid of type ahead. You would need to use a library such as ncurses that provided managed input functions instead. Standard C only deals with streams of input, and stdin gets buffered by the operating system so there is no way to get around it.
esus saves.... Passes to Moses, shoots, he scores!
Reply
#86
can't figure out how to make an array of structs using the new command. Here is what I got:
Code:
enemy_struct *enemy = new enemy_struct[200];
This doesn't give me an error, but using the array in my program like this: enemy[i]->x = 10; says that left of -> needs to point to a class/struct/union. Anybody?
am: "Where should I put this thing so that it doesn't hurt anyone we know or care about?"
Max:"Out the window, Sam. There's nobody but strangers out there."
Reply
#87
Use DOT.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#88
if you were using a pointer to a single enemy_struct, you would say something like
Code:
enemy_struct *enemy;
enemy->x = 10;
but if you use the arrow operator with an array, which is really just a pointer to a block of memory, you are trying to dereference some 4-byte piece of a enemy_struct... luckily the compiler is smart and catches it. So yes, use
Code:
enemy[i].x = 10;
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)