'Type name expected
"Error C:\BORLANDC\BIN\PIXEL.CPP 6: Type name expected" on trying to define "kolor" string. Editor: Borland C++ on DOS. This program asks about x, y and color of pixel, clears the screen and put pixel on screen. What's wrong?
#include <graphics.h>
#include <conio.h>
#include <iostream.h>
#include <string.h>
int x, y;
string kolor;
void main()
{
cout << "PIXEL TEST";
cout << "WPISZ LICZBE X";
cin >> x;
cout << "WPISZ LICZBE Y";
cin >> y;
cout << "WPISZ KOLOR";
cin >> COLOR;
cout << "DZIALAM... TRWA UMIESZCZANIE PIKSELA...";
int driver = DETECT,mode;
initgraph(&driver,&mode,"c:\\BORLANDC\\bgi");
putpixel(x,y,kolor);
getch();
closegraph();
}
Solution 1:[1]
string is in the std namespace.
So you need to write std::string kolor;, and #include <string>.
Ditto with your cout and cin calls.
An alternative - using namespace std; - is possible, but it's poor advice except in short tutorial programs (where it affords clarity), but seldom used in production code due to the resulting namespace pollution.
If this doesn't fix the compiler errors, then it's really time to upgrade your compiler. The Borland compiler has never been a standard C++ compiler, although it did have its uses when it was first released all those years ago.
If you are far too attached to the Borland compiler to migrate, then you can use a 3rd party early version of the C++ Standard Library: STLPort is one such example.
Solution 2:[2]
Well problem is that there is no string data type in Borland c++. You will need to use a char[] or char*
But looking at your implementation, that won't be what you need, because the syntax for putpixel is
void putpixel(int x, int y, int color);
So you need to be passing an integer as the color. Look into the integer values of all the color codes. You can also call it as
putpixel(x,y,RED);
putpixel(x,y,BLUE);
Some standard color codes have already been defined like this.
So you need to take input in a char[], compare ( hard code ), and then pass on the correct color code
The color codes are
0 BLACK
1 BLUE
2 GREEN
3 CYAN
4 RED
5 MAGENTA
6 BROWN
7 LIGHTGRAY
8 DARKGRAY
9 LIGHTBLUE
10 LIGHTGREEN
11 LIGHTCYAN
12 LIGHTRED
13 LIGHTMAGENTA
14 YELLOW
15 WHITE
Although it is highly suggested to move on to never versions of c++. If your school forces you to learn it, then okay, but you need to learn the never versions as well, as Borland C++ is too outdated and no one uses it anymore
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Arun A S |
