"for" loop not running program multiple times

Write your question here.
I need some help on my cpp code and "for" loops, when I try to run it will only run through the program 1 time no matter what I input. I need it to clear the screen and run the code again with a maximum of 5 games played. Any input on what will fix my issue and make it clear the screen and run multiple games would be appreciated. (Ignore all of the cout stuff in the middle, I am not very good at making the code look clean yet)

[code]
#include<iostream>
#include <cstdlib>
#include <ctime>
#include<iomanip>
using namespace std;

int main()
{
int num;
const unsigned short max = 5;



cout << "How many hands would you like to play?\n";
cin >> num;

srand(time(0));
system("cls");
cout << "\nGame starts:\n\n";


for (0; num < max; num += 1); {

const int MIN_VALUE = 3; const int MAX_VALUE = 6; int suit; int suit2;
unsigned seed = time(0);
srand(seed);
suit = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
suit2 = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;

const int MIN = 2; const int MAX = 14; int num1; int num2;
unsigned seed1 = time(0);
srand(seed1);
num2 = (rand() % (MAX - MIN + 1)) + MIN;
num1 = (rand() % (MAX - MIN + 1)) + MIN;

cout << " "; cout << char(201); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(187) << endl;
cout << " "; cout << char(186); cout << " The Card Game: \"War\" "; cout << char(186) << endl;
cout << " "; cout << char(200); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(205); cout << char(188); cout << "\n\n\n";
cout << " Your Card Opponent's card \n";
cout << " "; cout << char(218); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(191); cout << " "; cout << char(218); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(191) << endl;

cout << " "; cout << char(179); switch (num1) { case(11): cout << "J"; break; case (12): cout << "Q"; break; case (13): cout << "K"; break; case (14): cout << "A"; break; case(1):case(2):case(3):case(4):case(5):case(6):case(7):case(8):case(9):case(10): cout << num1; } switch (num1) { case(10): cout << " "; break; case(1):case(2):case(3):case(4):case(5):case(6):case(7):case(8):case(9):case(11):case(12):case(13):case(14): cout << " "; } cout << char(179); cout << " "; cout << char(179); switch (num2) { case(11): cout << "J"; break; case (12): cout << "Q"; break; case (13): cout << "K"; break; case (14): cout << "A"; break; case(1):case(2):case(3):case(4):case(5):case(6):case(7):case(8):case(9):case(10): cout << num2; } switch (num2) { case(10): cout << " "; break; case(1):case(2):case(3):case(4):case(5):case(6):case(7):case(8):case(9):case(11):case(12):case(13):case(14): cout << " "; } cout << char(179) << endl;
cout << " "; cout << char(179); cout << " "; cout << char(179); cout << " "; cout << char(179); cout << " "; cout << char(179) << endl;
cout << " "; cout << char(179); cout << " "; cout << char(179); cout << " "; cout << char(179); cout << " "; cout << char(179) << endl;
cout << " "; cout << char(179); cout << " "; cout << char(suit); cout << " "; cout << char(179); cout << " "; cout << char(179); cout << " "; cout << char(suit2); cout << " "; cout << char(179) << endl;
cout << " "; cout << char(179); cout << " "; cout << char(179); cout << " "; cout << char(179); cout << " "; cout << char(179) << endl;
cout << " "; cout << char(179); cout << " "; cout << char(179); cout << " "; cout << char(179); cout << " "; cout << char(179) << endl;
cout << " "; cout << char(179); cout << " "; cout << char(179); cout << " "; cout << char(179); cout << " "; cout << char(179) << endl;
cout << " "; cout << char(179); switch (num1) { case(10): cout << " "; break; case(1):case(2):case(3):case(4):case(5):case(6):case(7):case(8):case(9):case(11):case(12):case(13):case(14): cout << " "; } switch (num1) { case(11): cout << "J"; break; case (12): cout << "Q"; break; case (13): cout << "K"; break; case (14): cout << "A"; break; case(1):case(2):case(3):case(4):case(5):case(6):case(7):case(8):case(9):case(10): cout << num1; } cout << char(179); cout << " "; cout << char(179); switch (num2) { case(10): cout << " "; break; case(1):case(2):case(3):case(4):case(5):case(6):case(7):case(8):case(9):case(11):case(12):case(13):case(14): cout << " "; } switch (num2) { case(11): cout << "J"; break; case (12): cout << "Q"; break; case (13): cout << "K"; break; case (14): cout << "A"; break; case(1):case(2):case(3):case(4):case(5):case(6):case(7):case(8):case(9):case(10): cout << num2; } cout << char(179) << endl;
cout << " "; cout << char(192); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(217); cout << " "; cout << char(192); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(196); cout << char(217) << endl;

system("pause");
system("cls");

}return 0;

}
Please edit your post and apply a closing code tag.

[/code]
for (0; num < max; num += 1);

ok...
0 does nothing. if you meant that, you can just say ;
for (; num < max; num++)

and loops don't have a ; on them.

this does nothing zero or more times, then the would-be body of the loop (the stuff you think is the loop body but isnt because the ; is the loop body) happens once regardless of num & max.


most likely you meant
for(num = 0; num < max; num++)
{
things
Last edited on
Even with code tags that ; would be hard to spot.
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
for (0; num < max; num += 1); {
  /* things */
}

// is same as
for (0; num < max; num += 1)
  ;

{ /* things */ }

// is same as
for (0; num < max; num += 1) {
  ;
}

{ /* things */ }

// is same as
for (0; num < max; num += 1) {
}

{ /* things */ }

// and equivalent to
if ( num < max ) {
  num = max;
}

{ /* things */ }



PS. Does the compiler warn about comparison between signed int and unsigned short?
It probably should.
Rather than having 25 of std::cout << char(205) why not simply:

 
std::cout << std::string(25, 205);


and the same for char(196) etc etc.

Or even define a string for this:

1
2
3
const auto str205 {std::string(25, 205)};
...
std::cout << str205;


and likewise for the others.

Also, remove all those srand() calls inside the loop.
You're not making it any more random, in fact you're making it non-random.

You have one srand() at the start of main, and that's all you need.
Using c++ random, removing all those unnecessary switch statements and simplifying the output and using names for the various graphics chars, then as a first refactor consider:

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
#include <iostream>
#include <string>
#include <random>
using namespace std;

std::mt19937 rng(std::random_device {}());

constexpr unsigned char dblHor { 205 };
constexpr unsigned char dblVrt { 186 };
constexpr unsigned char dblTopLft { 201 };
constexpr unsigned char dblTopRht { 187 };
constexpr unsigned char dblBotLft { 200 };
constexpr unsigned char dblBotRht { 188 };

constexpr unsigned char sglHor { 196 };
constexpr unsigned char sglVrt { 179 };
constexpr unsigned char sglTopLft { 218 };
constexpr unsigned char sglTopRht { 191 };
constexpr unsigned char sglBotLft { 192 };
constexpr unsigned char sglBotRht { 217 };

int main() {
	const auto strDblHor { std::string(25, dblHor) };
	const auto strSglHor { std::string(25, sglHor) };
	constexpr auto names { "23456789TJQKA" };
	std::uniform_int_distribution<int> distribs(3, 6);    // These are the char codes for the card suits (3 - 6)
	std::uniform_int_distribution<int> distribn(0, 12);
	unsigned short num {};

	cout << "How many hands would you like to play? ";
	cin >> num;

	cout << "\nGame starts:\n\n";

	while (num--) {
		const auto suit { distribs(rng) };
		const auto suit2 { distribs(rng) };
		const auto num1 { distribn(rng) };
		const auto num2 { distribn(rng) };

		cout << " " << dblTopLft << strDblHor << dblTopRht << '\n';
		cout << " " << dblVrt << " The Card Game: \"War\"    " << dblVrt << '\n';
		cout << " " << dblBotLft << strDblHor << dblBotRht << "\n\n\n";
		cout << " Your Card Opponent's card \n";
		cout << " " << sglTopLft << strSglHor << sglTopRht << " " << sglTopLft << strSglHor << sglTopRht << '\n';
		cout << " " << sglVrt << names[num1] << sglVrt << " " << sglVrt << names[num2] << sglVrt << '\n';
		cout << " " << sglVrt << " " << sglVrt << " " << sglVrt << " " << sglVrt << '\n';
		cout << " " << sglVrt << " " << sglVrt << " " << sglVrt << " " << sglVrt << '\n';
		cout << " " << sglVrt << " " << char(suit) << " " << sglVrt << " " << sglVrt << " " << char(suit2) << " " << sglVrt << '\n';
		cout << " " << sglVrt << " " << sglVrt << " " << sglVrt << " " << sglVrt << '\n';
		cout << " " << sglVrt << " " << sglVrt << " " << sglVrt << " "<< sglVrt << '\n';
		cout << " " << sglVrt << " " << sglVrt << " " << sglVrt << " " << sglVrt << '\n';
		cout << " " << sglVrt << " " << names[num1] << " " << sglVrt << " " << sglVrt << ' ' << names[num2] << sglVrt << '\n';
		cout << " " << sglBotLft << strSglHor << sglBotRht << " " << sglBotLft << strSglHor << sglBotRht << '\n';
	}
}


The output isn't formatted properly, but this is a readable starter.
Last edited on
In your loop, you have to increment the index using ++i or i++ statrting from a base value.
https://www.w3schools.com/cpp/cpp_for_loop.asp

You can create a loop this way :
index as start; limitation; increment or decrement;
1
2
for (int i = 0; i < 5; ++i)
    doSomething();

it runs 5 times ++

Your code seems really hard coded. Maybe you could find some help studying this one which is not bad at all. However you have to fix a little error (help given). Take a look at the link below ++

https://cplusplus.com/forum/general/168698/
Last edited on
you have to increment the index using ++i or i++ statrting from a base value.

I would not say "have to".
1. The num += 1 increments num by one, just like ++num or num++ does.
Granted, ++num is more common style than num+=1, but on some loops you might have to do num+=4 and some loops have no incrementing statement at all.

2. We have "starting base value" here.
1
2
3
4
cin >> num;
for (; num < max; num += 1) {
  // something
}

The tricky part is that it is from user, so there are two possibilities. The initial value of num could be
A) less than max, in which case the loop iterates max-num times
B) >=max, in which case loop iterates 0 times

Even in the case A, if I give "4", expecting to get four hands according to the prompts, the game will give 5-4, that is only one hand. The UI and logic do not match.

If the user really should decide, then:
1
2
3
4
cin >> num;
for (int hand=0; hand < num; ++hand) {
  // something
}

or, like seeplus had it:
1
2
3
4
cin >> num;
while ( num-- ) {
  // something
}

In that while loop the --num and num-- do produce different result.

If you want both user to decide and have an upper limit, then you can for example sanitize the input first:
1
2
3
4
5
cin >> num;
if ( num > max ) num = max;
while ( num-- ) {
  // something
}

I agree. All what you said is true. However I wanted to be clear in order to help a beginner who strugled hard to understand how to get an efficient loop - a basic explanation. Notice that I did not mentionned goto and other bad alternatives :)
Topic archived. No new replies allowed.