graphics.h and outtextxy to display int

I am trying to display to the screen player name and status.

I can do the name via outtextxy as follows:


but when I try doing the following where "hits" is an integer it won't work. I tried doing static_cast but it didn't work.


I can do the cout method as follows and all is fine. It is only in outtextxy that there is a problem.

cout << grpchrs[i].hits<<endl;


  • All I can say is you are trying to pass an integer to a function that only takes strings. You need to convert the integer to a string first. There are many ways to do this. I use this:

    int intToConvert = 10001;
    char intAsString [10]; //int will be stored here as a string
    itoa (intToConvert,intAsString,10);
    cout << intAsString << endl;

    itoa() converts an int to a string. The paramaters to that function are an int you want to convert to a string, a pointer to a character array, and the 10 means we are using a base ten number system (instead of binary or octal).

    you will probably soon realize you want a function to convert floats to a string also. a little more tricky. Here is an admitedly inefficient way that I do it. I only am using this to debug my opengl code so efficiency is not really an issue.

    string floatToString(float f)
    string out = "";
    char buffer [33];
    int ipart = f;
    int fpart = 0;
    f = (f - ipart);

    itoa (ipart,buffer,10);
    out = out + buffer;
    out = out + ".";

    for(int count = 0; count < 7; count++)
    f = f*10;
    ipart = abs((int)f);
    out = out + buffer;
    f = f-ipart;
    return out;

    In this code the float has an accuracy of 7 digits meaning there will allways be 7 digits after the decimal point even if they are all zeros. You can change the accuracy easily by changing the number the for loop goes up to. There are faster ways probably but this works. For instance if you don't care about the decimal part of a float you could just cast the float to an int and use the first method.

    good luck.
  • You can also use sprintf(char*, const char*, ...) if you want easier formatting. You need to watch out though, that your output buffer is large enough or your program might produce veird and hairy errors.
    // sprintf() declaration from stdio.h

    // Variables
    int iNumber=15;
    float fNumber=167.1;
    double dNumber=186.4;
    char *cText="Printing numbers> ";
    char output[256];

    // Put numbers into text
    sprintf(output, "Text: %s Integer: %d Float: %.2f Double: %.0lf",
    cText, iNumber, fNumber, dNumber);

    // Write the text
    outtextxy(40, 70, output);

    // Output will be at coordinates (40, 70)
    // Text: Printing numbers> Integer: 15 Float: 167.10 Double: 186.4
    (More thorough description of sprintf() can be found from:

    Good luck,
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!