Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i thought that to ask a question you used get_char but its not and i don't know how to ask one

What I have tried:

#include <stdio.h>
int main (void)

char c = get_char("do you like cheese");

if (c == 'n')
{
  printf("why not ")
}
else if (c == 'y')
{
  printf("me too")
}
Posted
Updated 26-Apr-23 9:51am
Comments
jeron1 26-Apr-23 11:59am    
What you have posted will not compile, so start a little simpler with a Hello World program (see the first link). Take note some of some of the syntax differences between the example and your attempt.
https://www.tutorialspoint.com/learn_c_by_examples/hello_world_program_in_c.htm

Then move on to something a little more complex, like the following link, and do the same thing. Hopefully things will get a little clearer.
https://www.tutorialspoint.com/c_standard_library/c_function_getchar.htm

1 solution

I would support the suggestion to acquire the required programming knowledge with a book or a tutorial. On the other hand, you also learn a lot by finding and fixing your own mistakes. The compiler can give good hints. Sometimes you have to ask him for it by specifying compiler options. Unfortunately you forgot to specify which compiler or programming environment you use. Also the operating system can play a role, if you want to access system functions.

If you want, you can easily write a function get_char() if you want to have the above program like that. For example, the function you are looking for might look like this:
C
char get_char(char* txt)
{
	printf("%s: ", txt);
	int ch = getchar();
	return ch;
}

It has already been pointed out that the program cannot be compiled as presented even then. Changes are needed in at least 5 other places. A proper C program needs e.g. at least a return at the end, in order to correspond to the standard. In addition, various brackets and semicolons are missing.
 
Share this answer
 
Comments
CPallini 27-Apr-23 2:30am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900