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().

Recommended Answers

All 3 Replies

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.

oh duh i knew that thanx!

next time, please ask C++ questions in the C++-forum

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.