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]
#include <iostream>
usingnamespace 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;
}
elseif (cards = 'red')
{
reds = reds + 1;
}
elseif (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 (').
#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++;
}
elseif (cards == 2) // red
{
reds++;
}
elseif (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.