Home > Back-end >  Novice c language, for a great god
Novice c language, for a great god

Time:12-02

Excuse me, how can change input statement rather than a number? Using vs2019
# include
Int main ()
{
int c;
While ((c=getchar ())!=(EOF)
If (c=='1')
Printf (" Hello, nice to meet you. ");
Else if (c=='2')
Printf (" My name is world. ");
Else if (c=='3')
Printf (" What 's your name?" );
The else
putchar(c);
}
If I change this program to
# include
Int main ()
{
int c;
While ((c=getchar ())!=(EOF)
If (c=='hi')
Printf (" Hello, nice to meet you. ");
Else if (c=='name')
Printf (" My name is world. ");
Else if (c=='3')
Printf (" What 's your name?" );
The else
putchar(c);
}
Then the output will be
Hi
Hi
The name
The name
3
What's your name?

CodePudding user response:

Chat c [256];//string
While (gets (c)!=NULL) {//receiving string
If (STRCMP (c, "hi")==0)//string comparison
Printf (XXX)
.
}

CodePudding user response:

Input statement, it is the string handling, string handling can refer to: the fgets, STRCMP, strcpy, and so on function

 else if (c=='name') 

The name should be a string here, 'name' is illegal, should use STRCMP string comparison

CodePudding user response:

Getchar () can only obtain a single character

CodePudding user response:

The scanf, STRCMP
  • Related