0

hey this is a just for fun topic (a.k.a. NOT homework) but if you can please reply:
I am working in Visual C++ and I can't get any <iostream> operation to write a single character to a variable and then check it in an if() statement. Here is my last try:

#include <iostream>
using namespace std;

int main()
{
   char k;
   cin>>k;
   if(k='r')
      cout<<k;
   else if(k='f')
      cout<<k<<"!";
   else{}
   cin>>k;
   return 0;
}

Even when I input a 'f', it prints 'r'. It's almost like it's skipping the if().

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Nick Evan
1

Your if statements are both using the assignment operator "=" instead of the equality operator "==". The first one will always set k to 'r' and then print it.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.