image choice

So another test program I am writing will pull up a different image depending on the answer I give this is what I have but no matter what answer I put, it gives me the image for yes even if I put something that isnt y or n

// lab2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;
char response;

int main()
{
string mystring;

mystring = "you have made a terrible mistake, haven't you? Would you like a second chance? (y/n)";

cout << mystring << "\n";
cin >> response;

if (response = 'y')
system("mspaint.exe happy_link.bmp");

else if (response = 'n')
system("mspaint.exe C:\ben.jpg");

system("pause");


return 0;
}

what could I be doing wrong here? they are both in the folder yet only one of them is accessed
To test for equality, use the == operator.

 
if (response = 'y')
and
 
else if (response = 'n')

Each of those statements assign the value, then test the result.
Equivalent to
 
if ('y')

and because 'y' is non-zero, it always gives boolean true.


-------------------------------------------------
Also, this looks like an invalid path
 
    "C:\ben.jpg"

It's safer to use a forward slash "C:/ben.jpg" or with a double backslash "C:\\ben.jpg"


See "escape codes" on this tutorial page:
http://www.cplusplus.com/doc/tutorial/constants/
Last edited on
Topic archived. No new replies allowed.