Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with inputting multidimensional string arrays....
#31
I'm an idiot. For the last couple of questions, I haven't been compiling the right file. I had a file loaded into my workspace known as practice.cpp. Then when I started to change the code I saved it as practice2.cpp. The workspace compiled and ran practice.cpp, so everytime I edited practice2.cpp nothing happened and no change occured. But, I got it all sorted out and it is now trying to compile practice2.cpp. Notice I said trying. When I try to compile I get these errors:

Quote:C:\Program Files\Microsoft Visual Studio\MyProjects\LilPractice\LilPractice2.cpp(33) : error C2819: type 'entry' does not have an overloaded member 'operator ->'
C:\Program Files\Microsoft Visual Studio\MyProjects\LilPractice\LilPractice2.cpp(13) : see declaration of 'entry'
C:\Program Files\Microsoft Visual Studio\MyProjects\LilPractice\LilPractice2.cpp(33) : error C2227: left of '->name' must point to class/struct/union
C:\Program Files\Microsoft Visual Studio\MyProjects\LilPractice\LilPractice2.cpp(35) : error C2819: type 'entry' does not have an overloaded member 'operator ->'
C:\Program Files\Microsoft Visual Studio\MyProjects\LilPractice\LilPractice2.cpp(13) : see declaration of 'entry'
C:\Program Files\Microsoft Visual Studio\MyProjects\LilPractice\LilPractice2.cpp(35) : error C2227: left of '->age' must point to class/struct/union
C:\Program Files\Microsoft Visual Studio\MyProjects\LilPractice\LilPractice2.cpp(46) : error C2819: type 'entry' does not have an overloaded member 'operator ->'
C:\Program Files\Microsoft Visual Studio\MyProjects\LilPractice\LilPractice2.cpp(13) : see declaration of 'entry'
C:\Program Files\Microsoft Visual Studio\MyProjects\LilPractice\LilPractice2.cpp(46) : error C2227: left of '->name' must point to class/struct/union
C:\Program Files\Microsoft Visual Studio\MyProjects\LilPractice\LilPractice2.cpp(46) : error C2819: type 'entry' does not have an overloaded member 'operator ->'
C:\Program Files\Microsoft Visual Studio\MyProjects\LilPractice\LilPractice2.cpp(13) : see declaration of 'entry'
C:\Program Files\Microsoft Visual Studio\MyProjects\LilPractice\LilPractice2.cpp(46) : error C2227: left of '->age' must point to class/struct/union

here's my code:

Code:
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <string>
#include <stdlib.h>
#include <conio.h>
#include <iostream.h>

using namespace std;

void wait_for_key(void);

typedef struct {
    int age;
    string name;
} entry;

int main()
{  
    
    int number, i;
      
    cout << "How many names do you want to enter: ";
    cin >> number;
    if (!number) return 0;

    entry *info;
    info = new entry[number];    
    
    for(i = 0; i < number; i++) {
        cout << endl << "Name " << i + 1;
        //getline(cin, info[i]->name);
        cin >> info[i]->name;
        cout << "AGE: ";
        cin >> info[i]->age;
        while(kbhit());
    }
        
    system("cls");
    if (number == 1)
        cout << "Here is the " << number << "entry:" << endl << endl;
    else
        cout << "Here are the " << number << "entries:" << endl << endl;

    for(i = 0; i < number; i++) {
       cout << i + 1 << ") " << info[i]->name << " AGE: " << info[i]->age;
    }
    wait_for_key();         
        
    delete info;
    return 0;
}

void wait_for_key(void)
{
    while(kbhit());
    while(!(kbhit()));
}
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
#32
Ok, got it to work, but it messes everything I thought about structure arrays up.
instead of
Code:
info[i]->name
I used
Code:
info[i].name
This is crazy because info is defined like this:
Code:
entry *info;
info = new entry[number];
I read a tutorial (http://computer.howstuffworks.com/c12.htm) which showed that an array of pointers to a struct would look like my first try rather than my second. Can someone explain this to me?
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
#33
That's because C++ stinks. When you use "new" it considers arrays as objects, so they kinda stop being pointers and are considered "references". A big incongruence.

If you had used (malloc) to create the array, you should have to have used -> instead of . to access to the registers.

C++ is a bad language 'cause it lets you using OOP stuff in non-OOP programs, like yours. That makes you feel dizzy when stuff don't work as expected. That "new" stuff is the way to create an object in OOP, but you are then accessing the object's attributes directly, which is kinda a bad policy in OOP (you should have to write functions to do that instead). Basicly, you think you are defining a structure (typedef), but with new you are turning it an object. That's why you use "." and not "->".
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#34
As an aside, a struct is exactly the same as a class in C++, except that it's members are public by default. If you follow good programming practice of always declaring the public and private sections of your class, there's no difference. Many people use struct in C++ for classes that have no functions as you have here, but really it's poor practice as you never know if you want to add methods in the future. And then you'll either have to change it to a class, or have a struct with methods. struct is just in there for C compatibilty really.

Oh, and I prefer C++ over C as I find the syntax simpler to understand, and love OO methodology despite the rants of the C purists Tongue
In a world without walls and doors, who needs Windows and Gates?
Reply
#35
Quote:As an aside, a struct is exactly the same as a class in C++, except that it's members are public by default.

Thats very incorrect. A struct is simpy a way of grouping a collection of variables together. Classes on the other hand have methods, are stored and accessed in a completely different way and have other nifty features such as abstraction, inheritance and polymorphism.

Quote:Oh, and I prefer C++ over C as I find the syntax simpler to understand, and love OO methodology despite the rants of the C purists
Na_th_an, myself and most other people who dislike C++, dislike the OO nature of it because it isn't a pure OO language. Programs that are designed using OO methods do not need procedural code mixed in with them, C++ allows this and therefore allows programmers to write horrible code. Java is what C++ should have been (and before anybody complains, Im talking about the language, not the compiler and JVM). Other OO languages like Smalltalk provide a much better interpretation of the OO methodology (the design patterns book for Smalltalk is about quarter the size of the C++ one because it doesnt have the language and OO abnormalities that C++ has).
esus saves.... Passes to Moses, shoots, he scores!
Reply
#36
why do I get an unhandled exception error when running a compiled program that is trying to
char *fname;

this isn't picked up when I compile.
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
#37
Quote:Thats very incorrect. A struct is simpy a way of grouping a collection of variables together. Classes on the other hand have methods, are stored and accessed in a completely different way and have other nifty features such as abstraction, inheritance and polymorphism.

Well, that's not true. structs in C++ can also have methods and all the OO stuff we associate with a class. You can write structures that have constructors, protected and private data members, functions and destructors. In other words - a class.

http://msdn.microsoft.com/library/defaul...struct.asp

Quote:Na_th_an, myself and most other people who dislike C++, dislike the OO nature of it because it isn't a pure OO language. Programs that are designed using OO methods do not need procedural code mixed in with them, C++ allows this and therefore allows programmers to write horrible code...
Yes, and I agree with most of it. But a bad coder is a bad coder whatever the language, it's just that with C++ there is more freedom to write horrible code.

I like Java very much as a language. If only I could compile to native code :roll:
In a world without walls and doors, who needs Windows and Gates?
Reply
#38
Opps, you're right Piptol :oops:. I dont actually use C++ at all and I though you were comparing Standard C structures to C++ classes (in which case they are very different). Anyway, its one more reason for me to dislike C++, structs should be for holding grouped data and classes should be for templating objects. That'll learn me for arguing on a subject I don't know much about *runs of with tail between legs*.

If you want to compile Java code to native executables you can use a JIT (Just In Time) compiler, like this one: http://www.shudo.net/jit/
esus saves.... Passes to Moses, shoots, he scores!
Reply
#39
termites are burrowing through my skull. I cannot figure out why I am getting this unhandled exception error when I run my program. I've found out exactly what line it has a problem with (a line that defines a char pointer [char *finame;] inside a function). I googled this and found that other people too have had the problem and that some were related to char *. Here is the code for my program. I'm trying to put everything I've learned together and make a game that you guess a word by using tokens to buy clues (pretty lame, but so is my C).

Code:
#include "stdafx.h"
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <string>
#include <stdlib.h>
#include <conio.h>
#include <iostream>
#include <windows.h>
#include <time.h>

using namespace std;

void display_menu(int highlight);
int init_game(void);
int load_word(int w);
int make_word(void);
int main_menu(void);
int quit_game(void);
void setcolor(unsigned short color);
void wait_key(void);

typedef struct {
    int tokens, level, guesses, turns, score, show_def, show_word;
    int time_minutes, time_iseconds, time_millisecs;
} game_struct;

typedef struct {
    string theword, definition;
    int clues;
} word_struct;

game_struct *game;
word_struct *word;
char *clues;

int main(void) {
    
    int i, a, quit = 0;
    init_game();
    while(!quit) {
    
        i = main_menu();
        display_menu(i);
    
        if (i == 1) {
            cout << endl << "you want a new game!";
            wait_key();
            load_word(1);
        }    
        if (i == 2) {
            cout << endl << "you want intructions!";
            wait_key();
        }
        if (i == 3) if (quit_game()) quit = 1;
        if (i == 4) a = make_word();
    }
    return 0;
}


void display_menu(int highlight) {
    system("cls");
    setcolor(10);
    cout << ".............GUESS THAT WORD..............." << endl << endl;
    if (highlight == 1) setcolor(15); else setcolor(7);
    cout << "(N)ew Game" << endl;
    if (highlight == 2) setcolor(15); else setcolor(7);
    cout << "(I)nstructions" << endl;
    if (highlight == 3) setcolor(15); else setcolor(7);
    cout << "(Q)uit" << endl;
    setcolor(7);
}


int init_game(void) {
    srand(time(NULL));
    game = (game_struct *) malloc(sizeof(game_struct));
    word = (word_struct *) malloc(sizeof(word_struct));
    return 0;
}


int load_word(int w) {
    int i;
    char buffer[100];
    char *filename;
    char finame[40];
    
    _itoa(w, buffer, 10);
    
    if (w < 10)
        i = sprintf(finame, "word0%d.dat", w);
    else
        i = sprintf(finame, "word%d.dat", w);
    
    filename = (char *) malloc(sizeof(strlen(finame)));
    strcpy(filename, finame);
    
    FILE *f;
    f = fopen(filename, "r");
    if (!f) return 1;
    fscanf(f, "%s%s%d", word->theword, word->definition, word->clues);
    clues = (char *) malloc(word->clues * sizeof(char[60]));
    for (i = 0; i < word->clues; i++)
        fscanf(f, "%s", clues[i]);
    fclose(f);
    free(filename);
    return 0;
}


int make_word(void) {
    int ii;
    // ************************Here's the problem!!
   char *finame;
    char tmpc[60];
    system("cls");
    cout << "Word: ";
    cin >> word->theword;
    cout << endl << "Definition: ";
    cin.getline(tmpc, 59);
    word->definition = string(tmpc);
    cout << endl << "Clues: ";
    cin >> word->clues;
    clues = (char *) malloc(word->clues * sizeof(char[60]));
    for (ii = 0; ii < word->clues; ii++) {
        cout << "clue " << ii;
        cin.getline(tmpc, 59);
        strcpy(&clues[ii], tmpc);
    }
    strcpy(tmpc, "word01.dat");
    finame = (char *) malloc(sizeof(strlen(tmpc)));
    finame = "word01.dat";
    FILE *f;
    f = fopen(finame, "w");
    if (!f) return 1;
        fprintf(f, "%s%s%d", word->theword, word->definition, word->clues);
        for (ii = 0; ii < word->clues; ii ++)
            fprintf(f, "%s", clues[ii]);
    fclose(f);
    free(finame);
    return 0;
}


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


int quit_game(void) {
    int quit = 0;
    char sel;
    display_menu(3);
    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);    
    }
    return quit;
}


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


void wait_key(void) {
    while(kbhit());
    while(!(kbhit()));
}
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
#40
A very quick perusal of your code:

Code:
filename = (char *) malloc(sizeof(strlen(finame)));
   strcpy(filename, finame);

Quote:DESCRIPTION
The strlen() function calculates the length of the string
s, not including the terminating `\0' character.

You need to allocate space for the terminating null character otherwise you will get a buffer overflow and the string wont terminate correctly if you try and print it, try replacing it with.
Code:
filename = (char *)malloc(sizeof(strlen(finame) + 1));
   strcpy(filename, finame);
esus saves.... Passes to Moses, shoots, he scores!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)