Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[C++] Why doesn't textcolor(x) work?
#1
Why doesn't this program work?

Code:
#include <iostream.h>
#include <conio.h>

void main()
{
    textmode(C80);
    clrscr();
    textcolor(14);
    printf("Text");
}

I'm trying to change the colour of the word "text" to yellow. I've high-videoed, low-videoed, tried all the colours and modes of the rainbow. I've checked and double-checked, making sure that I have spelt "colour" the correct wrong way. What's wrong? Am I missing something obvious? Is it my system?

PS: Borland C++ 5.5.1 for Win32

PPS: How do I make a variable global, ie., so I can pass it between functions?
In a race between a rock and a pig, don't varnish your clams." -- "Dilbert"
Reply
#2
Quote:PPS: How do I make a variable global, ie., so I can pass it between functions?

To make it global, declare the variable outside main. If you access from a different cpp file, then you must declair the variable within that scope using the extern keyword.
Code:
#include<iostream>
using anmespace std;

int globalvariable = 5;

void testglobal(){
  cout << globalvariable << endl;
}

int main(){
  cout << globalvariable << endl;
  testglobal;
  return 0;
}

output :
5
5

However, you really should avoid global variables when you don't need them. The best way to allow a function to modify the variables it to pass them by reference. See these 2 examples:

pass by value:
Code:
#include <iostream>
using namespace std;

void func(int anum){
   cout << anum << endl;
   anum++;
   cout << anum << endl;
}

int main(){
   int x = 5;
   cout << x << endl;
   func(x);
   cout << x << endl;
   return 0;
}
output:
5 <--in main
5 <-- in func
6 <--in func
5 <-- back in main


pass by reference:
Code:
#include <iostream>
using namespace std;

void func(int& anum){
   cout << anum << endl;
   anum++;
   cout << anum << endl;
}

int main(){
   int x = 5;
   cout << x << endl;
   func(x);
   cout << x << endl;
   return 0;
}
output:
5 <--in main
5 <-- in func
6 <--in func
6 <-- back in main

Note...we pass by reference (int& in the calling function), and now, when we change the value of variable in the function, the change affects x in main.

I hope this helps.
Reply
#3
Quote:Why doesn't this program work?
Code:
#include <iostream.h>
#include <conio.h>

void main()
{
    textmode(C80);
    clrscr();
    textcolor(14);
    printf("Text");
}

printf just prints to the standard output stream and is equivalent to saying:
Code:
fprintf(stdout, "Hello world!\n");
Because the standard output stream can be anything from a file to a printer to a terminal screen, it doesn't support features such as colour, or screen position.

The conio library (console IO) is often available with DOS C compilers and provides some nice screen functions like colours. To get it to print correctly you will need to use the conio libraries print function, offhand I think it is called cprintf, but Im not sure. You could either find out by using the manual for conio, or alternatively open up conio.h, scroll down to the function declarations and see if you can find something that looks like a print function.
esus saves.... Passes to Moses, shoots, he scores!
Reply
#4
In BORLAND there is a command to print in colour: cprintf (check in the help file) as Loosecaboose points out. I think it ignores \n so be careful to use gotoxy before every cprintf. And yes, it is in conio.h

But careful: cprintf is not portable. I would not use it.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#5
Thanks all. However, there is still one thing that I don't understand. Where do I put the "extern" keyword? I'm declaring variables with it in the form "extern char a_letter" and it still says they don't exist. Or is it that they are structs? Should I expect this to work:
Code:
struct number1
{
    char name;
};

extern number1 character[2];
In a race between a rock and a pig, don't varnish your clams." -- "Dilbert"
Reply
#6
On top:

Code:
#inlcude <stdio.h>

extern int polsky;

int main(void)
{
   return 0;
}

You can also declare global variables in header files, but you have to be careful to include precompiler directives to avoid stuff being declared every time you include the file.

Code:
#ifndef _STUFF_H
#define _STUFF_H

int polsky;

#endif

But IMHO, this is spaghetti. I would rather use only in-module globals. If you want to watch or modify these variables from another module use observer/modifier functions:

Code:
/* module1.h */
void blah(int bleh);
void modify_integer_data(int new_data);
int read_integer_data(void);

Code:
/* module1.c */
#include "module1.h"

int integer_data;   /* In-module global */

void blah (int bleh)
{
   /* do stuff */
}

/* Modifier function: */
void modify_integer_data (int new_data)
{
   integer_data = new_data;
}

/* Observer function */
int read_integer_data(void)
{
   return integer_data;
}
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#7
That struct still doesn't work... Sad
In a race between a rock and a pig, don't varnish your clams." -- "Dilbert"
Reply
#8
Which struct? And how it doesn't work?

I think that then we are not talking about the same thing, 'cause my code works for me.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#9
Quote:Where do I put the "extern" keyword? I'm declaring variables with it in the form "extern char a_letter" and it still says they don't exist. Or is it that they are structs? Should I expect this to work:
Code:
struct number1
{
    char name;
};
extern number1 character[2];

It doesn't work because 'number1' is not a valid type. You need to do either this:
Code:
typedef struct {
  char name;
} number1;

number1 character[2];

or this:
Code:
struct {
  char name;
} number1;

struct number1 character[2];
esus saves.... Passes to Moses, shoots, he scores!
Reply
#10
Undefined symbol 'character' in function setup_game_data()
Undefined symbol 'character' in function setup_game_data()
Undefined symbol 'character' in function setup_game_data()
Undefined symbol 'character' in function setup_game_data()
Undefined symbol 'character' in function setup_game_data()
...
and so forth. Nothing is working for me. I've tried following exactly the examples, and tried strewing the "extern" command everywhere, but that damn 'character' is still an 'undefined symbol' in 'function 'setup_game_data()''. What gives?

Code:
typedef struct
{
    char name;
} number1;

struct number1 character[2];
doesn't work, nor does

Code:
typedef struct
{
    char name;
} number1;

extern struct number1 character[2];

. No matter where I put extern, it just remains staunchly undefined. What am I missing?
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)