Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[C++] Why doesn't textcolor(x) work?
#11
Ah. I thought you meant my code didn't work. Next time quote Wink
I'll take a galnce at yer code.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#12
You only need to use extern in files where the variable is external. Try this simple example:

number.h
Code:
/* Number.h */
void increment(void);

typedef struct {
  int a;
} number;

main.c
Code:
/* main.c */
#include <stdio.h>
#include "number.h"

number testNumber;

int main() {
  testNumber.a = 0;
  printf("Test number = %d\n", testNumber.a);
  increment();  
  printf("Test number = %d\n", testNumber.a);
}

increment.c
Code:
/* Increment.c */
#include "number.h"

extern number testNumber;

void increment(void) {
  testNumber.a++;
}

Compile both increment.c and main.c together to produce an executable. The structure number is defined in main.c and is an external structure in increment.c
esus saves.... Passes to Moses, shoots, he scores!
Reply
#13
I just don't understand. What's wrong here?

MAIN.CPP
Code:
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include "messages.cpp"
#include "display.cpp"

typedef struct
{
    char name;
} number1;

number1 character;

int main()
{
    setup_video();
    setup_game_data();
    splash_screen();
    return 0;
}

DISPLAY.CPP
Code:
void setup_video();

void setup_video()
{
    textmode(C80);
    clrscr();
    textcolor(LIGHTGRAY);
}

MESSAGES.CPP
Code:
void setup_game_data();
void splash_screen();
char ask(char question);

extern number1 character;

void setup_game_data()
{
    character.name = ask("What is your name?");
}

void splash_screen()
{
    cout << "Message Here.";
}

char ask(char question)
{
    cout << question;
    cin << ask;
    return ask;
}
In a race between a rock and a pig, don't varnish your clams." -- "Dilbert"
Reply
#14
You will get an error with your code that character has an unknown storage class in messages.cpp. From your code for messages.cpp:
Code:
void setup_game_data();
void splash_screen();
char ask(char question);

extern number1 character;

void setup_game_data()
{
    character.name = ask("What is your name?");
}

void splash_screen()
{
    cout << "Message Here.";
}

char ask(char question)
{
    cout << question;
    cin << ask;
    return ask;
}

What exactly is the type number1? You can't carry the definition over from main.cpp, you either need to move the typedef for number1 into a common header file, like I did in the example I gave you, or have the typedef in both main.cpp and messages.cpp.
esus saves.... Passes to Moses, shoots, he scores!
Reply
#15
Ah... of course. Silly me Wink.
In a race between a rock and a pig, don't varnish your clams." -- "Dilbert"
Reply
#16
Quote:
Code:
cin << ask;
I should change this to:
Code:
cin >> ask;
Wink
Reply
#17
Yeah... I noticed that (and managed to fix something for once.) I've still got compiler errors up the billy-oh, but I'll get over it.
In a race between a rock and a pig, don't varnish your clams." -- "Dilbert"
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)