Hamza Díaz
Hamza Díaz

Reputation: 175

How to display a given number into graphic digits in c++

I have a serious problem about printing a number as a graphic element, i have developed a game using c++, oriented object programming, and when a player wins he receives a certain points, so I want to transform these points in a sequence of a graphic digits.

For example:

Firstly the player has 0 points. He wins the level 1 and gets 100 points so I want to display 4 digits like this:

  1. Firtly: 0000

enter image description here

  1. Wins: 100 points to 0100

enter image description here

The game works with integers, What I did was: create a Graphic Array that contains all the graphic numbers from 0 to 9.

gGraphic digits[10];

gGraphic is a class that creates and draws an object.., hence digits[0] is a graphic element that represents 0, digits[1] represents 1, digits[2] represents 2...successively.. for example if I want to draw number 1, like 0001, this is what I do:

digits[0].draw(Xposition, Yposition);
digits[0].draw(Xposition + digits[0].ObtainScaleX(), Yposition);
digits[0].draw(Xposition + digits[0].ObtainScaleX()*2, Yposition);
digits[1].draw(Xposition + digits[0].ObtainScaleX()*3, Yposition);

As you can see I don't change the position Y, but I change the position X so I can get the sequence of digits represented at the X axis, the function ObtainScaleX() returns the width scale of an Object.

The problem is how can I do this for 1000 points, 1234 points or 3409???, the are thousands of possibilities and an if statement or switch case won't work, I really need an explanation, a basic idea, do I have to use pointers?

Upvotes: 2

Views: 1949

Answers (3)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

The very simplest solution for your problem would be to turn the number in question to a std::string and retrieve the single digits from the converted character values:

std::ostringstream oss;
oss << value;
int scale = 0;
for(std::string::const_iterator it = oss.str().begin();
    it != oss.str().end();
    ++it, ++scale) {
    int drawDigitIndex = *it - `0`;
    digits[drawDigitIndex].draw(Xposition + 
                                digits[drawDigitIndex].ObtainScaleX() * scale
                               , Yposition);
}

Upvotes: 3

R Sahu
R Sahu

Reputation: 206667

There are two parts to your problem:

  1. Get the digits of a given number.
  2. Display the digits.

You can use the method used in this SO answer to get the digits.

You already seem to have a method to display the digits.

Upvotes: 1

Cory Kramer
Cory Kramer

Reputation: 117926

You can use math to figure out the values you should use. For example

   7632
//    ^ ones
//   ^  tens
//  ^   hundreds
// ^    thousands

So you can find the digit to draw in each place as

int digits[4];
int value = 7632;
digits[0] = value % 10;              // ones
digits[1] = value % 100 / 10;        // tens
digits[2] = value % 1000 / 100;      // hundreds
digits[3] = value % 10000 / 1000;    // thousands

You can obviously do this in a loop, I just wanted to show step by step where each value would come from. This would result in the array digits having the values

{ 2, 3, 6, 7 }

Once you know the digits, you can use your drawing code that you already have.

Upvotes: 1

Related Questions