Reputation:
# include <stdio.h>
void danidev(void){
printf("Dani is a YouTuber and an indie game developer and an fps game developer having his game published in play store he is 22 years old and goes to a university");
}
void brackeys(void){
printf("brackeys is a YouTuber and an indie game developer and also an fps game developer having most of his games published in itch.io and has a team which works on game development");
}
void blackthornprod(void){
printf(" they are two brothers who create video games and teach others how to do the same over on Youtube and Udemy ! they are passionate in sharing their knowledge and game creation journey with other aspiring game developers.");
}
void jabrils(void){
printf("jabrils is a ai programmer and also a machine learning pro coder and also a game developer he has made a lot of ai and has saved millions of people from their tough times");
}
void codingbullet(void){
printf("coding bullet is a multi intelligent ai developer and also a master in machine learning also he owns a youtube channel with 2.06 million subscribers");
}
int main(){
printf("HERE IS THE INFORMATION OF FAMOUS CODING YOUTUBERS(PLS TYPE THE FOLLWOING YOUTUBERS NAME): ");
char b;
scanf("%c",&b);
if(b=='danidev'){
danidev();
}
else if(b=='brackeys'){
brackeys();
}
else if(b=='blackthornprod'){
blackthornprod();
}
else if(b=='jabrils'){
jabrils();
}
else if(b=='codingbullet'){
codingbullet();
}
else{
printf(" i dont know what you are taking about");
}
return 0;
}
I have a problem when I enter a YouTubers name ( complete name) as an input I get a problem that the constant is too long and does not give the proper result and also it says the constant character is too long for its type
Upvotes: 1
Views: 85
Reputation: 21
You cannot directly compare strings using the equality operator (==). Instead, use strcmp() to compare two strings.
char str[20];
gets(str);
if(!strcmp(str, "danidev") danidev(); //Use double quotes for strings, instead of single quotes (used for character literal).
//Same as strcmp(str, "danidev") == 0
//rest of the code
Also, if you're a beginner, I suggest you to check some C's String library functions.
Upvotes: 0
Reputation: 1129
There a couple of errors, from string comparison, string definition and char/char array variables. It looks like JS sometimes, but take care of the big differences. Beside that, I have seen another answer has been added while I was writing this one, I want to add to pay attention at the length of array in scanf so you don't raise a buffer overflow error at runtime.
You can check it out here: https://onlinegdb.com/HyrgvK-sL
#include <stdio.h>
#include <string.h>
void danidev (void)
{
printf ("Dani is a YouTuber and an indie game developer and an fps game developer having his game published in play store he is 22 years old and goes to a university");
}
int main ()
{
printf("HERE IS THE INFORMATION OF FAMOUS CODING YOUTUBERS(PLS TYPE THE FOLLWOING YOUTUBERS NAME): ");
char b[32];
scanf("%31s", b);
if (strncmp(b,"danidev", 32)== 0)
{
danidev ();
}
else
{
printf (" i dont know what you are taking about");
}
return 0;
}
Upvotes: 0
Reputation: 149
You need to allocate more space for the input.
The type char
is only going to hold one character. You need an array of char
s. This can be declared as follows:
char b[30];
This will hold 30 characters.
For string literals, you should use double quotes not single quotes. e.g. "danidev"
as opposed to 'danidev'
.
When comparing strings (arrays of characters) you should use the strcmp()
function.
Please read the documentation for it: strcmp
Since you are starting out, I would also recommend looking at some tutorials for strings in C. String Functions
Upvotes: 1