Printing symbols using a for loop

Hello!
I am taking my first ever coding class, and we are using C++. I kind of understand how a for loop works, but not for the purpose that I am trying to accomplish. Currently the game is called NIM, and is played by inputting a number 1-3 that counts down the number of objects, and whoever makes the counter reach 0 first loses. Our instructor gave us the base code, and one of our assignments is to include a loop that counts out the number of objects left, and displays them using symbols.

For my purposes, I only want it to print symbols when num_objects <= 10, however I am unsure of how to get it to print symbols (I would like to use #'s). They should also print in a line. (I am referring to the for loop on line 33) (The current for loop on there is a little pointless and doesn't accomplish anything I want to do, it was just leftover from myself messing around to learn how it works)

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
 
 #include<iostream>     // include two libraries
 #include<cstdlib>
 using namespace std;
 int main()             // main() starts the actual program
 {
    // ---------------- Variable declarations ---------------------
    int num_objects = 23;
    int current_player = 1;
    int move;
    int high = 3;
    int low = 1;
    // ----------- Beginning of the main game loop ----------------
    do {
       if (current_player == 1) {
            cout << "Player 1 enter your move ("<<low<<"-"<<high<<"): ";
            cin >> move;
            while (move < low || move > high || move > num_objects){
               cout << "Illegal move. \nEnter a new move ("<<low<<"-"<<high<<"): ";
               cin >> move;
            }
       }
         else {
            do {
               move =  1+ rand() % 3;
            } while (move < 1 || move > 3 || move > num_objects);
                 if (num_objects <= (high+1)) {
                        move = (num_objects - 1);
                }

            cout << "Computer removed " << move << endl;
       }
        for (int i=1; num_objects <= 10 ; i++){
                cout << i << endl;
        }
       num_objects = num_objects - move;  // implement the move
       cout << num_objects << " objects remaining.\n";
       current_player = (current_player + 1) % 2;
   } while (num_objects > 0);
   // ------------  end of the game loop --------------------------
   cout << "Player " << current_player << " wins!!!\n";
   cin.ignore();
   cout << "\nPress enter to quit.\n";
   cin.ignore();
   return 0;
}
Last edited on
Try using a character variable to display characters. Something like this:

1
2
3
char display = 20;
for(int i = 0; i < 20; i++)
    std::cout << display - i << std::endl;


Try this code.
This is a step in the right direction!
It prints numbers 20-1 after every turn, however now I need to make it do the following:
Should print symbols instead of number (10 #'s instead of 10 numbers)
Should be dependent on how many objects are left
Should print in a single horizontal line
If i understand you correctly, then you want something like this:
1
2
for (int i=0; i < num_objects; i++)
        cout << "#";
That's exactly what I needed. I ended up figuring it out on my own, but you did post exactly what I had :D
Topic archived. No new replies allowed.