Converting int to char in for loop?

So I'm struggling with a game that I made in class, its call the Game of Nim. Anyway, there are a number of objects in the game. What I'm struggling with is how to make the game print out a visual character x number of objects.

Something like:

There are 8 objects left.
########

There are 5 objects left.
#####

etc.

I'm suppose to do this with a for loop. So far all I have been able to get is the number of objects the game starts with and then it just messes up the rest of the game.

So I'm back to square one... Any help? Suggestions? I'm really new to this.

I know the code is probably a mess, I haven't had the time to clean it up yet as I was in hurry and I just went with things that I knew worked.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// The game of Nim
#include<iostream>
#include<cstdlib>
using namespace std;
int game();

int main() {

    int num_objects = 23;
    int current_player = 1;
    int move;
    double y;
    double n;
    char answer;

    cout << " Luke_Lyon " << endl;
    cout << " Assignment_#1 " << endl;
    cout << " Section_#2 " << endl << endl;

    cout << "   The Game of NIM" << endl << endl;
    cout << "   Remove 1-4 objects, try to force me to remove the last one." << endl << endl;

 int num_objects = 23;
    int current_player = 1;
    int move;
    double y;
    double n;
    char answer;

    cout << " Luke_Lyon " << endl;
    cout << " Assignment_#1 " << endl;
    cout << " Section_#2 " << endl << endl;

    cout << "   The Game of NIM" << endl << endl;
    cout << "   Remove 1-4 objects, try to force me to remove the last one." << endl << endl;


     do {
        game();
        cout << "How about another game? y/n" << endl;
        cin >> answer;
        cin.ignore();
        cout << "\n";
             }
    while (answer=='y');
    cout << "Thanks for entertaining me!";
    cin.get();
        }

    int game(){

  int num_objects = 23;
    int current_player = 1;
    int move;

        do { // beginning of the game loop
                cout << num_objects << " ObJeCtS LeFt!" << endl;
        if (current_player == 1)  { // user's move
            do {
                cout << "It's your move buddy: ";
                cin >> move;
            } while (move < 1 || move > 4 || move > num_objects);
        } else { // computer's move

            do {
               if (current_player != 1 && num_objects == 4) move == 3;
               if (current_player != 1 && num_objects == 3) move == 2;
               if (current_player != 1 && num_objects == 2) move == 1;

                move = 1 + rand() % 4;
            } while (move < 1 || move > 4 || move > num_objects);
                 cout << "I burned " << move << endl;

        if (num_objects == 22) cout << "We're still in the early stages of play, folks!" << endl;
        if (num_objects == 21) cout << "We're still in the early stages of play, folks!" << endl;
        if (num_objects == 20) cout << "We're still in the early stages of play, folks!" << endl;
        if (num_objects == 19) cout << "We're still in the early stages of play, folks!" << endl;
        if (num_objects == 18) cout << "We are really getting into this game now!" << endl;
        if (num_objects == 17) cout << "We are really getting into this game now!" << endl;
        if (num_objects == 16) cout << "We are really getting into this game now!" << endl;
        if (num_objects == 15) cout << "I'm betting on User 1 folks, what about you?" << endl;
        if (num_objects == 14) cout << "I'm betting on User 1 folks, what about you?" << endl;
        if (num_objects == 13) cout << "I'm betting on User 1 folks, what about you?" << endl;
        if (current_player != 1 && num_objects == 6) cout << "Uh-oh, the computer's been known to make a mistake at this poi$
        if (current_player != 1 && num_objects == 5) cout << "Uh-oh, the computer's been known to make a mistake at this poi$
        if (current_player != 1 && num_objects == 4) cout << "Uh-oh, the computer's been known to make a mistake at this poi$
        if (current_player == 1 && num_objects == 4) cout << "This is it folks. User 1 could win it all right here!" << endl;
        if (current_player == 1 && num_objects == 3) cout << "This is it folks. User 1 could win it all right here!" << endl;
        if (current_player == 1 && num_objects == 2) cout << "This is it folks. User 1 could win it all right here!" << endl;
        if (current_player == 1 && num_objects == 1) cout << "This is it folks. User 1 could win it all right here!" << endl$

        }

         num_objects = num_objects - move; // adjust objects
         current_player = (current_player + 1) % 2; // adjust turn
       } while (num_objects > 0); // end of the game loop
 cout << "User " << current_player << " YOU ARE THE WINNER!!!" << endl;
       }

cout << "There are " << num_objects << " objects remaining." << endl;
No, you must not be getting what I'm trying to say.

I already have the game saying there are num_objects left and it prints out the how many are left numerically.

What i need is for it to print out "objects" that are equal to how many objects are left. And the objects can be any character I choose.
Ohhh lol !
I thought those '#' were just to aesthetically please my eyes :p hahaha
1
2
3
4
5
cout << "There are " << num_objects << " objects remaining." << endl;
for(int i=0; i< num_objects ; i++) //the index may be off by 1 depending on ur code
{
   cout << "#";
}

Sleepy...
Last edited on
Thank you for the help!
Topic archived. No new replies allowed.