Very new to programming! Need help with a for loop not working.

Hello, I've recently started programming and I've had lots of trouble figuring out why my code is wrong. I need to do a for loop, which asks the same question (Which is "What color is card number [number]") 20 times. The user can then input either Blue, red, or green, which is displayed once all 20 questions have been answered. For some reason, it only lets me input once, then asks me the 20 questions, and finishes.

[Code]
#include <iostream>
using namespace std;
int main()
{
int blues=0, reds=0, greens=0, cards=0;
for(int i=1; i<=20; i=i+1) {
cout<<"Enter the color of card number "<<i<<endl;
cin>>cards;
if (cards='blue') {
blues=blues+1;
}
else if (cards='red') {
reds=reds+1;
}
else if (cards='green') {
greens=greens+1;
}
}
cout<<"The number of blue cards is "<<blues<<endl;
cout<<"The number of red cards is "<<reds<<endl;
cout<<"The number of green cards is "<<greens<<endl;
return 0;
}
[/code]
Please read the "Introduction to strings" section of this tutorial: https://cplusplus.com/doc/tutorial/variables/

'int' is a specific type of variable. It holds a non-fractional number (-4, 5, 11, etc.).
It cannot hold a value like "blue".

Change your cards variable to be a string, and add #include <string> to your code.

Also, for strings you must use ", not '.
Finally, = is the assignment operator. == tests for equality.

So you must do:
1
2
3
4
5
6
7
8
9
10
11
string cards;
cin >> cards;
if (cards == "blue")
{

}
else if (cards == "red")
{

}
// etc. 
You had the right idea about code tags, you just didn't use them correctly (the tag shouldn't be capitalized; it is [code], not [Code]):
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
#include <iostream>
using namespace std;
int main()
{
   int blues = 0, reds = 0, greens = 0, cards = 0;
   for (int i = 1; i <= 20; i = i + 1)
   {
      cout << "Enter the color of card number " << i << endl;
      cin >> cards;
      if (cards = 'blue')
      {
         blues = blues + 1;
      }
      else if (cards = 'red')
      {
         reds = reds + 1;
      }
      else if (cards = 'green')
      {
         greens = greens + 1;
      }
   }
   cout << "The number of blue cards is " << blues << endl;
   cout << "The number of red cards is " << reds << endl;
   cout << "The number of green cards is " << greens << endl;
   return 0;
}

Your if statements are assigning a value (=) to the variable cards, not doing a comparison (==).

You are trying to compare a string value to an int variable. Not gonna work the way you expect even if you used double quotes (") for the strings instead of single quotes (').

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
#include <iostream>

int main()
{
   int blues { }, reds { }, greens { }, unknown { }, cards { };

   std::cout << "Blue: 1, Red: 2 & Green: 3\n\n";

   for (int i { 1 }; i <= 20; i++)
   {
      std::cout << "Enter the color of card number " << i << ": ";
      std::cin >> cards;

      if (cards == 1) // blue
      {
         blues++;
      }
      else if (cards == 2) // red
      {
         reds++;
      }
      else if (cards == 3)
      {
         greens++;
      }
      else
      {
         unknown++;
      }
   }

   std::cout << "\nThe number of blue cards is " << blues << '\n';
   std::cout << "The number of red cards is " << reds << '\n';
   std::cout << "The number of green cards is " << greens << '\n';
   std::cout << "The number of unknown color cards is " << unknown << '\n';
}

Blue: 1, Red: 2 & Green: 3

Enter the color of card number 1: 1
Enter the color of card number 2: 2
Enter the color of card number 3: 3
Enter the color of card number 4: 4
Enter the color of card number 5: 1
Enter the color of card number 6: 2
Enter the color of card number 7: 3
Enter the color of card number 8: 3
Enter the color of card number 9: 5
Enter the color of card number 10: 1
Enter the color of card number 11: 1
Enter the color of card number 12: 2
Enter the color of card number 13: 2
Enter the color of card number 14: 2
Enter the color of card number 15: 3
Enter the color of card number 16: 3
Enter the color of card number 17: 3
Enter the color of card number 18: 3
Enter the color of card number 19: 1
Enter the color of card number 20: 2

The number of blue cards is 5
The number of red cards is 6
The number of green cards is 7
The number of unknown color cards is 2
Thank you so much! After a bit of fixing it finally worked. I had tried using string but I forgot to add #include <string> so I thought something else was wrong.
Topic archived. No new replies allowed.