Yes/No Statement

Hi,
How can I write this in C;

“Is your name Maria?” Then if your answer is ‘Y’ (Yes) the program should print “Great!” and if your answer is ‘N’ (No) it should print “Sorry.Come here with Maria!”

Please help!
In C++:

string input;

std::cout << "Is your name Maria?\n> ";
std::cin >> input; // Use can type something, will be stored in the input string

if(input == "Y" or "y")
{
std::cout << "Great!\n";
}
else
{
std::cout << "Sorry. Come here with Maria\n";
}

Not sure how to in C, but you would use the printf command to output text. There is probably something similar to cin and if in C too though.

Lookup the scanf function. I think that will work for user input.
That doesn't work and I have no idea about the reason.
Simply should be;

#include<stdio.h>
int main ()
printf("Is your name Maria?");
.
.
.

bla bla. But how :(
(by the way I'm using Microsoft Visual Studio 2010)

Thanks anyway.




Hey not sure if you're still stuck but there was a problem with exitcode's code:

instead of

if(input == "Y" or "y")

try:

if(input == "Y" || input == "y")
Instead of double quotes, use single quotes.

eg: 'Y' 'y' 'N' 'n'
The single quotes will only work if you change the input variable to a char

eg: char input;
also I noticed that he didn't have any brackets after "int main()"
You mean curly braces? ({})

'cout' is part of a different language:

Are you programming in 'C' or 'C++'???

If you are using 'C' you should make it clearer, this is a C++ website after all.
Any C++ code will not work if you are programming with C.

Sorry for typing 'or' I forgot you had to type ||, I thought both would work.

Also, when code doesn't work, give us the full error message.
Last edited on
exitcode wrote:
Are you programming in 'C' or 'C++'???

If you are using 'C' you should make it clearer
op wrote:
How can I write this in C;


Also the op needs to put in a little effort and show us what he is having problems with if it is control structures maybe he needs to check out this page: http://www.cplusplus.com/doc/tutorial/control/

Though he will have to make a few transitions such as using printf instead of cout
I realise the op said 'C', but it wasn't clear - I thought this was a C++ forum?

A lot of those suggested or C++ only options, though I would guess they are very similar.

I said to research printf, if, and scanf. All are C compatible, as it were.
Topic archived. No new replies allowed.