Keb_Tse
Keb_Tse

Reputation: 39

I could not figure out why my code is not working - codeforce - Brain's Photos

This is my code programmed to print #Color if the user entered any of C, Y, M characters. But if the user entered W ,B, G then the program is suppose to print out #Black&White. Unfortunately, my code skips my first for loop and goes straight to printing out #Black&White, what am I doing wrong?

#include <iostream>
#include <vector>

using namespace std;

int main()
{
  vector<char>colors;
  int row, column, letters = 0;
  cin>>row, column;
  letters = row * column;

  for(int i = 0; i < letters; i++)
  {
    char temp;
    cin>>temp;
    colors.push_back(temp);
  }

  for(int j = 0; j < letters; j++)
   {
     if(colors[j] == 'C' || colors[j] == 'Y' || colors[j] == 'M')
     {
       cout<<"#color"<<endl;
       return 0;
     }
   }

  cout<<"#Black&White"<<endl;

  return 0;
}

Upvotes: 1

Views: 67

Answers (1)

orphen
orphen

Reputation: 66

You have two expressions in the line cin>>row, column; The comma separates them, and column is a useless expression in this case.

You probably mean cin >> row >> column;

column is being used uninitialized. If it happens to take the value 0, the body of your first loop never executes. Beware, using uninitialized variables in this way can lead to undefined behavior.

Upvotes: 2

Related Questions