Aug 24, 2017 at 8:15pm UTC
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
Aug 24, 2017 at 8:49pm UTC
To test for equality, use the
==
operator.
and
Each of those statements assign the value, then test the result.
Equivalent to
and because
'y'
is non-zero, it always gives boolean
true
.
-------------------------------------------------
Also, this looks like an invalid path
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 Aug 25, 2017 at 12:35pm UTC