Help me with sandwich making code

Hello,
Im a beginner and was practising to write functions. Here i am trying to write a function to write sandwich recipe including an ‘if-else’ statement.
`
#include

#include

void make_sandwich(std::string ing1, std::string ing2, bool morning) {

std::string sandwich = " ";

if (morning == true) 


{

sandwich += “bread\n”;

sandwich += “ing1+\n”;

sandwich += “ing2+\n”;

sandwich += “bread\n”;

}

else {

sandwich += “bread\n”;

sandwich += “ing1+\n”;

sandwich += “bread\n”;

}

}
`
int main() {

std::string ingre1 = “cheese”;

std::string ingre2 = “meat”;

bool morning = true;

std::cout << make_sandwich(ingre1, ingre2, morning);

}
`
PS:- here include iostream and include vector is also written at the beginnig, I MENTIONED IT COZ THEY were not visible

You’ll need to change the void function to a std::string
and also
return sandwich;
at the end of the function.

full code

(spoiler below)

#include <iostream> #include <vector> std::string make_sandwich(std::string ing1, std::string ing2, bool morning) { std::string sandwich = " "; if (morning == true) { sandwich += "bread\n"; sandwich += "ing1+\n"; sandwich += "ing2+\n"; sandwich += "bread\n"; } else { sandwich += "bread\n"; sandwich += "ing1+\n"; sandwich += "bread\n"; } return sandwich; } int main() { std::string ingre1 = "cheese"; std::string ingre2 = "meat"; bool morning = true; std::cout << make_sandwich(ingre1, ingre2, morning); }

here’s a possible code answer using the code from the original post:

#include <iostream> #include <vector> std::string make_sandwich(std::string ing1, std::string ing2, bool morning) { std::string sandwich = " "; if (morning == true) { sandwich += "bread\n"; sandwich += "ing1+\n"; sandwich += "ing2+\n"; sandwich += "bread\n"; } else { sandwich += "bread\n"; sandwich += "ing1+\n"; sandwich += "bread\n"; } return sandwich; } int main() { std::string ingre1 = "cheese"; std::string ingre2 = "meat"; bool morning = true; std::cout << make_sandwich(ingre1, ingre2, morning); return 0; }

hey mate, how u brought this code in this way in the comment section? i need to know and also this code is giving ingre1 and ingre2 after execution but we have already declared ingre1 and ingre2 as cheese and meat.

you’d have to take ing1+ and ing2+ out of the quotation marks;
and, as you mentioned, declare those variables beforehand.

Instead of

sandwich += "ing1+\n";

do

sandwich += ing1+"\n";

(spoiler below)

I used the Create a Codebyte button to do this:

#include <iostream> #include <vector> std::string ing1 = "cheese"; std::string ing2 = "meat"; std::string make_sandwich(std::string ing1, std::string ing2, bool morning) { std::string sandwich = " "; if (morning == true) { sandwich += "bread\n"; sandwich += ing1+"\n"; sandwich += ing2+"\n"; sandwich += "bread\n"; } else { sandwich += "bread\n"; sandwich += "ing1+\n"; sandwich += "bread\n"; } return sandwich; } int main() { std::string ingre1 = "cheese"; std::string ingre2 = "meat"; bool morning = true; std::cout << make_sandwich(ingre1, ingre2, morning); return 0; }

Create a Codebyte is on the top right of this posting thing when you’re writing a post.

last question, i have seen several codes where the return0 was not mentioned but in some like this it is mentioned so pls tell me what is the concept of writing "return 0 " and when do we have to use it??

also why after ing1 and ing2 there i was made to write ‘+’ sign??

I’m close to a beginner at C++
but here’s what I’ve seen:

return 0; inside the main function is used to show the the program has run and finished successfully, return [something else] would be done to end the program and show there was some kind of problem (and the [something else] might be an error code number maybe).

However, doing this seems to be optional as far as I can tell.
The programs seems just fine without having return 0 in the main function.

I don’t actually know if any of this is correct, since I’m not an expert on C++, and I haven’t really researched this.

The += and the + are necessary to paste the strings together (or concatenate them).

"ing1+\n"; would show up on the screen as ing1+
and \n is the nextline character (as in - going to the next line).

ing1 + "\n"; would show up on the screen as cheese (since the string "cheese" is stored in the variable ing1).

ing1 "\n"; would give you an error because the compiler doesn’t understand what’s going on with "\n" - it has no instructions on what to do with it;
so the + is necessary.

here’s a different example:
ing1 is cheese
ing2 is meat
so
ing1 + ing2 is cheesemeat

Similarly,
if you have:

std::string x1 = "ab";
x1 += "cd";

x1 started out as just "ab"
but "cd" was added (and x1 was changed using +=)
so afterward,
x1 is "abcd"

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.