CHAR_INFO question
Ok so I was taking a C coded program and trying to put it into c++ so I can expand on it using classes.
I got everything to work fine in C++ after correcting some things (like bools and whatnot) but I can't get CHAR_INFO to work.
This is the code that isn't working.
info.screenbuffer[position].Char.AsciiChar = ' ';
info.screenbuffer[position].Attributes = BACKGROUND_BLUE;
I am not on my computer so I forget the exact error message, but it was something similar to "error with '.' in line X" which refers to the first line and maybe the second. When I remove those lines the program works fine.
I included everything needed, I think it is just a C code error in C++. When I compile in C it works fine.
info is a struct
screenbuffer is a CHAR_INFO array if infinite length
position is a int that refers to the position in screenbuffer (array)
Thanks
Have you changed the headers that are included when you moved from C to C++? The old C headers don't have the same name in C++.
Quote:Original post by ramen
I am not on my computer so I forget the exact error message, but it was something similar to "error with '.' in line X" which refers to the first line and maybe the second.
Look, if you expect us to give you a precise answer, then you need to ask a precise question. So, go to your computer, copy the exact error message, and paste it here.
BTW, according to MSDN, CHAR_INFO is defined in windows.h. You included this header file, did you ?
Ok here is the code!
If I change it back into C code it works fine... I am just having trouble setting a char_info = to information in C++.
Error messages:
c:\karazin\programming\c++\console functions\main.cpp(119) : error C2059: syntax error : '.'
c:\karazin\programming\c++\console functions\main.cpp(120) : error C2039: 'gamedata' : is not a member of '_CHAR_INFO'
c:\program files\microsoft visual studio\vc98\include\wincon.h(132) : see declaration of '_CHAR_INFO'
c:\karazin\programming\c++\console functions\main.cpp(120) : error C2039: 'screenBuff' : is not a member of '_CHAR_INFO'
c:\program files\microsoft visual studio\vc98\include\wincon.h(132) : see declaration of '_CHAR_INFO'
c:\karazin\programming\c++\console functions\main.cpp(120) : error C2228: left of '.Attributes' must have class/struct/union type
c:\karazin\programming\c++\console functions\main.cpp(123) : error C2228: left of '.screenBuff' must have class/struct/union type
c:\karazin\programming\c++\console functions\main.cpp(123) : error C2059: syntax error : '.'
c:\karazin\programming\c++\console functions\main.cpp(124) : error C2228: left of '.gamedata' must have class/struct/union type
c:\karazin\programming\c++\console functions\main.cpp(124) : error C2228: left of '.screenbugg' must have class/struct/union type
c:\karazin\programming\c++\console functions\main.cpp(124) : error C2228: left of '.attributes' must have class/struct/union type
main.cpp
cobs.h
If I change it back into C code it works fine... I am just having trouble setting a char_info = to information in C++.
Error messages:
c:\karazin\programming\c++\console functions\main.cpp(119) : error C2059: syntax error : '.'
c:\karazin\programming\c++\console functions\main.cpp(120) : error C2039: 'gamedata' : is not a member of '_CHAR_INFO'
c:\program files\microsoft visual studio\vc98\include\wincon.h(132) : see declaration of '_CHAR_INFO'
c:\karazin\programming\c++\console functions\main.cpp(120) : error C2039: 'screenBuff' : is not a member of '_CHAR_INFO'
c:\program files\microsoft visual studio\vc98\include\wincon.h(132) : see declaration of '_CHAR_INFO'
c:\karazin\programming\c++\console functions\main.cpp(120) : error C2228: left of '.Attributes' must have class/struct/union type
c:\karazin\programming\c++\console functions\main.cpp(123) : error C2228: left of '.screenBuff' must have class/struct/union type
c:\karazin\programming\c++\console functions\main.cpp(123) : error C2059: syntax error : '.'
c:\karazin\programming\c++\console functions\main.cpp(124) : error C2228: left of '.gamedata' must have class/struct/union type
c:\karazin\programming\c++\console functions\main.cpp(124) : error C2228: left of '.screenbugg' must have class/struct/union type
c:\karazin\programming\c++\console functions\main.cpp(124) : error C2228: left of '.attributes' must have class/struct/union type
main.cpp
#include <iostream>#include <windows.h>#include <stdio.h>#include "cobs.h"using namespace std;const char MAP_NAMES[MAX_MAZES][64] = { "Map.txt", "Map2.txt" };// This struct will hold ALL of our game datastruct GAMEDATA { CHAR_INFO *screenBuff; // This is buffer (array) we will allocate that will hold // all of our "maze data" that we want to display to the screen HANDLE outputHandle; // This is the handle of where we want to draw our screen buffer HANDLE inputHandle; // This is wthe handle of where we want to recieve user input (keyboard) SMALL_RECT rect; // This defines a rectangular area on the screen where we want to draw // The width and Height of our maze int width; int height; // the (x,y) of the "ending position" of the maze // int xEndPos; // int xEndPos;} gamedata;bool initiate();bool loadmap();bool inithandles();int main() { return(0);}bool initiate() { if( inithandles() == false ) return false; return true;}bool inithandles() { // Get the standard output handle gamedata.outputHandle = GetStdHandle(STD_OUTPUT_HANDLE); // Error Check if (gamedata.outputHandle == NULL ) return false; // Get the standard input handle gamedata.inputHandle = GetStdHandle(STD_INPUT_HANDLE); // Error Check if (gamedata.inputHandle == NULL ) return false; return true; // Handles loaded succesfully}bool loadmap() { FILE *file_ptr = NULL; // a "handle" for the map file // These will be used to calculate the offset into our screenBuff // **NOTE** even though "screenBuff" is a one dimensional array, we're going to // treat it as a two dimensional array int x = 0; int y = 0; int offset = 0; // we'll use this to store the offset into our // screenBuff when we are accessing it as a 2D array char temp; // This will be a "temp" char -- This will hold the current character // we've read in from the file // This randomly loads a maze from our list of map names in MAP_NAMES file_ptr = fopen(MAP_NAMES[rand()%MAX_MAZES],"r"); // Open the file for reading // Error check if (file_ptr == NULL) return false; // First line of "map file" contains two integers. // First int is the width of the map // Second int is the height of the map fscanf(file_ptr,"%d",&(gamedata.width)); // read in the width of the map fscanf(file_ptr,"%d",&(gamedata.height)); // read in the height of the map fgetc(file_ptr); // Skip past the "end of line character" // Now we can create a CHAR_INFO array that is big enough to hold the map gamedata.screenBuff = (CHAR_INFO*)malloc(sizeof(CHAR_INFO) * gamedata.width * gamedata.height); // Error check if (gamedata.screenBuff == NULL) { fclose(file_ptr); // close the file we opened return false; } while(!feof(file_ptr)) { // keep going until we reach the end of the "map file" temp = (char)fgetc(file_ptr); // read in the next character in file offset = x + y * gamedata.width; // computer offset into our screen buffer switch(temp) { case '0': // read in an open space // Fill that location with a space that is background blue gamedata.screenBuff[offset].char.AsciiChar = ' '; gamedata.screenBuff[offset].Attributes = BACKGROUND_BLUE; break; case '1': gamedata.screenBuff[offset].char.asciichar = wall; gamedata.screenbugg[offset].attributes = BACKGROUND_BLUE | FOREGROUND_GREEN; break; } return true; }}
cobs.h
#ifndef cobs#define cobs#include <windows.h>#include <stdio.h>#define MAX_MAZES 2 // Maximum number of "random" mazes for loading// How far from the upper left corner of the window (0,0) we want to // "indent" in for when we draw to the screen#define INDENT_X 5#define INDENT_Y 5#endif
It is correct, all of it. Except not the:
gamedata.screenBuff[offset].char.AsciiChar = ' ';
and the three other lines similar to it. That code right three is C code, not C++. I am just trying to figure out how to change that into C++. I couldn't find anything online.
gamedata.screenBuff[offset].char.AsciiChar = ' ';
and the three other lines similar to it. That code right three is C code, not C++. I am just trying to figure out how to change that into C++. I couldn't find anything online.
C++ code is case-sensitive.
EDIT: I fail to see how this could compile in C either!
Also, for your future questions,
1. Use [ source ] tags instead of
gamedata.screenBuff[offset].char.AsciiChar = ' '; gamedata.screenBuff[offset].Attributes = BACKGROUND_BLUE; gamedata.screenBuff[offset].char.asciichar = wall; gamedata.screenbugg[offset].attributes = BACKGROUND_BLUE | FOREGROUND_GREEN;
EDIT: I fail to see how this could compile in C either!
Also, for your future questions,
1. Use [ source ] tags instead of
tags for long code.<br>2. Clearly indicate IN THE source where are the errors occuring. I won't always be in a good enough mood to copy-paste your code into an editor so I can count the lines and find where the errors are.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement