(C++) Help with yes or no diagram (C++)

Hey, I am new to programming and trying my best to learn this in my spare time.
but I am standing stuck trying to figure out how to create a simple YES or NO diagram.

example
if yes then this or if no then this and so on
( horrible example tho )

So far I haven’t found any tutorials on it that I understand.
so I was hoping that someone here could create a small one for me just so I can see how it is built up and also so I can try to understand the structure.

Are you looking for help making a diagram or a program?

I want to know how I can get it to run in the terminal :slight_smile:

So, not a diagram? What code have you written so far? What part are you having trouble with?

I’ve only done this in javascript but in c++ it wasn’t as easy
I just don’t understand how to do it
I am very unsure of the switch command
and I am also unsure if I’ve done this right at all.

#include <iostream>
int main()
{
    using namespace std;
    int answer1, answer2;
    std::cout    << "are you having a good day";
    std::cin     >> answer1 = "yes";
    std::cin     >> answer2 = "no";

    switch  answer1 = "yes"
                  answer2 = "no"

{
    case "yes";
    cout    << "Glad you are having a good day";
    break;

    case "no":
    cout    << "that is sad to hear";
    break;

    default:
    cout    << "you have to type yes or no";



system("pause");
return 0;
}

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Nice, Thank you :slight_smile:

There’s a few things I don’t understand about your code. What type of value do you expect answer1 and answer2 to be? You declare them as integers.

    std::cin     >> answer1 = "yes";
    std::cin     >> answer2 = "no";

What are you trying to do with the assignment operators here?

 switch  answer1 = "yes"
                  answer2 = "no"

Again, what are these lines with assignment operators?

i thought it would be the same as i did in javascript. When no or yes is entered in the console or terminal it would take you to next question and so on going until the if statements run out and ends it

This is what i did on a JavaScript and then entered it into jconsole

<P>   
var answer1 = prompt("do you need it");
var answer2 = "no"


if (answer1 === "yes"){
    console.log("Keep it");


} else if (answer2 === "no") {
    prompt("do you want it?");

}


 </P>

now i want to figure out how to do this in c++ :slight_smile:

What resources are you using to learn C++? It’s worth starting from the very beginning, even though you will be able to go through many sections quickly. One of the first things that you learn with C++ is that variables are always of a certain type, which is one of the key differences with JavaScript. If you want if/else statements, C++ does have those and they look very much like they do in JavaScript. I can’t tell whether you want to take answer1 and answer2 from the user (cin) or to be hardcoded in your program. You’re sort of trying to do both maybe? And therefor not doing either correctly.

This is the yes-no diagram that I am trying to re-create


I’ve been following an online course on c++
and so far they have learned me how to create a math type of game.
But if I could implement a yes-no diagram to make it more interesting I would think that could be a bit more interesting, and also a great exercise for me to get a better understanding of scripting!
But this might be too much to ask for in this stage I am in now at this point.

I think you are misunderstanding how C++ cin works. See here.
cin takes the next input received and saves it to a variable.

Also, you are misunderstanding how a switch works. See here.

You cannot use string comparison for a switch statement like this. I’d recommend looking at strcmp.

1 Like

aaah, sweet! I’ll have a look at this and thank you both for good response! :smiley:

1 Like