Feb 23, 2014 at 11:39am UTC
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!
Feb 23, 2014 at 12:04pm UTC
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.
Feb 24, 2014 at 12:25am UTC
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.
Feb 24, 2014 at 12:55am UTC
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")
Feb 24, 2014 at 2:59am UTC
Instead of double quotes, use single quotes.
eg: 'Y' 'y' 'N' 'n'
Feb 24, 2014 at 2:58pm UTC
The single quotes will only work if you change the input variable to a char
eg: char input;
Feb 24, 2014 at 4:40pm UTC
also I noticed that he didn't have any brackets after "int main()"
Feb 24, 2014 at 5:05pm UTC
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 Feb 24, 2014 at 5:10pm UTC
Feb 25, 2014 at 6:43pm UTC
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.