Question about do while loops

Sep 11, 2015 at 6:50pm
I'm having a problem with a do while loop, with it not returning to the beginning while the given parameter is still met, any advice?

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
 #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;
    char pagn[2] = "y";
    int round = 1;
    // ----------- Beginning of the main game loop ----------------

        do{
                cout << "Round" << round << "Fight!" << endl << endl;
                        do {
                                 if (current_player == 1) {    // conditional: if
                                  cout << "Player 1 enter your move (1-3): ";  // output
                                  cin >> move;                 // input
                                  while (move < 1 || move > 3 || move > num_objects){
                                  cout << "Illegal move. \nEnter a new move (1-3): ";
                                  cin >> move;
                           }
                         } else {                          // else part of conditional
                        do {                         // make sure move is legal
                         move =  1+ rand() % 3;    // random computer move
                 } while (move < 1 || move > 3 || move > num_objects);
                 cout << "Computer removed " << move << endl;
          }
          num_objects = num_objects - move;  // implement the move
          cout << num_objects << " objects remaining.\n";
          current_player = (current_player + 1) % 2;  // switch players
         } while (num_objects > 0);
         // ------------  end of the game loop --------------------------
         cout << "Player " << current_player << " has scored a FATALITY!!!\n";
                round = round + 1;
           cout << "Would you like to play again? (y , n): ";
          cin >> pagn;
         cout << endl << endl << endl << endl;
        } while (pagn == "y");

   cin.ignore();
   cout << "\nPress enter to quit.\n";
   cin.ignore();
   return 0;
}

Sep 11, 2015 at 7:23pm
Erm, do you think you could fix your indentation so I could read that?
Sep 11, 2015 at 7:32pm
The program contains three do-while loops. Which one are you talking about?
Sep 12, 2015 at 2:20am
@Peter the outer most one
Sep 12, 2015 at 2:38am
I had to pick through with a fine tooth comb to figure out those loops.

Try adding a comment to each scope opening brace and closing brace, naming each scope on the opening brace, then on the closing brace state which scope just closed.

It really help in parsing out what is going on.
Last edited on Sep 12, 2015 at 2:38am
Sep 12, 2015 at 6:55am
You can not compare C strings (character arrays) using the == operator. What happens when you do is that you compare the address (pointer) to the first element in the string. The array pagn and the string literal "y" is not stored at the same location in memory so pagn == "y" will always be false.

To compare two C strings you should use the strcmp function from the <cstring>. Another possibility is to use the string class, which allows you to compare strings using == like you do in your program.

http://www.cplusplus.com/reference/cstring/strcmp/
http://www.cplusplus.com/reference/string/string/
Last edited on Sep 14, 2015 at 5:56pm
Sep 12, 2015 at 11:33am
I think char is supposed to be only single quotes, like so 'y' while strings are double quotes like so "y" at least in visual studio that rule is a must.

Also, I know I can compare char with char, and do so in several programs, but in single quotes and as singles, not arrays.

Also, maybe I missed it, but I don't see any reason for pagn to be an array, thus make it only one char and switch to single quotes and that should work.
Sep 14, 2015 at 5:50pm
@Peter87 by string class do you mean including the library then creating the variable as a string? and whenever i remove the quotes it says y is not declared

~Thanks everyone
Sep 14, 2015 at 5:51pm
Oops put the quotes back in and it runs good thanks!
Sep 14, 2015 at 8:19pm
This is a c string, which is just an array of char, inlcudes a closing null char, pagn is a pointer to array, pagn[0] is the first char of the array,
char pagn[2] = "y";

This is just a single char,
char pagn = 'y';

This is a string class,
string pagn = "y";

The double quotes indicate a string (probably denotes the inclusion of the closing null char, but I can't be sure), while single quotes are for single char, which because they are single, they don't require a closing null char nor a pointer.
Topic archived. No new replies allowed.