What did I do wrong?

Hello everyone!
I've just started with c and I think it's really fun! even though I can't do much... I have previous knowledge in a program language called GML, it's not an advanced language, it's about as hard as Javascript.

Anyway I tried to make a fairly short program in c :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
using namespace std;

int main ()
{
    int  yes ;
    string name;

    cout << "Stranger: Hello";
    cout << "Stranger: What's your name?";
    cout <<"\n\nThe stranger who you just met wants to know your name, what should you do?\n\n";
    cout <<"type in 1 if you want to tell the stranger your name\nor 0 if you want to run away from that maniac.";

    cin >> yes;







        if (yes = 1){
        cout << "You decide to tell the stranger your name...";
        cout << "Stranger: What is your name?";
        cin >> name;
        cout << name <<":" << " My name is " <<name <<".";
        cout << "Okey, It was niec meating you..." << name;

    }

if (yes = 0)
    {
        cout << "You ran away\n";
        cout << "blablabla";
        cout << "blablablabla";




    }




    return 0;
}


The following code would probably work in GML with just a few fixes but in c it seems as if the programm ignore the "if (yes=0)" statment. What did I do wrong? and also what should I do to be able to get a string from the player, for example

if (yes = "yes")

Thanks in advance! :)
'=' is assignment. if (yes = 0) means "assign 0 to yes, and then check the result for truthiness". The result is 0, which is implicitly converted to false, so the "if-part" is never executed. Comparisons for equality are done with "==". As for the second question, just change these two lines:

intstring yes ;

if (yes = 1 == "yes")

The language is called C++ by the way. C is a different language.
Last edited on
First of all you should decide for yourself what language you are using. Because what you showed is not the C.
Secondly, this (in bold)

if (yes = 1){

is not a comparision. It is an assignment. So the expression will be always equal to true because it is not equal to zero.
Please excuse my mistake. The language I'm trying to code in is c++ but I must have forgotten the plusses(facepalm) Anyhow, thanks for the enlightenments :) it made things much clearer now.
Last edited on
Topic archived. No new replies allowed.