Click here to Skip to main content
15,891,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, How do I get a string into a char variable correctly?
I have this code in C programming language but it doesn't work.
C
#include <stdio.h>
#include <stdlib.h>

int main(){

    char yesman;

    printf("Are you a yes man? ");
    scanf("%c", &yesman);

    /* getchar(); */

    if(yesman == 'yes'){
        printf("It is bad to be a yes man.");
    } else if(yesman == 'no'){
     printf("Good.");
    } else if(yesman == 'No.'){
     printf("Good, and you can spell at that.");
    } else {
     printf("Invalid Operator");
    }

    return 0;
}


What I have tried:

I have tried with
char yesman[4];
to try to allocate room for the characters in the string but it did not help.
Posted
Updated 22-Dec-20 20:08pm
v2

This should work :
C++
char yesman[64];
scanf( "%s", yesman );
I prefer to call gets or fgets - C++ Reference[^]. You can pass the stdin handle to it. Keep in mind that a newline character will be included with the string. It can be removed with this function :
C++
void RemoveNewLines( char * buffer )
{
    int length = (int) strlen( buffer );
    for( int n = length - 1; n >= 0; --n )
    {
       if( buffer[ length ] == '\r' )
           buffer[ length ] = 0;    // set to null
       else if( buffer[ length ] == '\n' )
           buffer[ length ] = 0;    // set to null
       else
           break;                   // not a newline so bail out
    }
}
 
Share this answer
 
Comments
CPallini 23-Dec-20 1:56am    
5.
In addition to Rick remarks,
Quote:
if(yesman == 'yes'){
That's not the correct way to compare C strings (and 'yes' is not a valid string literal), you should use the strcmp (or strncmp) function[^].
Try
C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
  char yesman[256];

  printf("Are you a yes man? ");

  if ( scanf("%s", yesman) == 1)
  {
    if( strcmp( yesman, "yes") == 0)
    {
        printf("It is bad to be a yes-man.\n");
    }
    else if( strcmp(yesman, "no")==0 )
    {
     printf("Good.\n");
    }
    else if( strcmp(yesman, "No.")==0)
    {
      printf("Good, and you can spell at that.\n");
    }
    else
    {
     printf("Invalid Operator\n");
    }
  }
  return 0;
}
 
Share this answer
 
Comments
Mieczyslaw1683 25-Dec-20 17:30pm    
I wonder one thing. When it comes to line 11.
Line 11: if ( scanf("%s", yesman) == 1)

I understand that scanf is scanning for input which goes to the yesman variable. What I don't understand is what the equal 1 (== 1) does? You could maybe spell out the meaning of the code to me in this line?
CPallini 25-Dec-20 18:01pm    
That is a check on scanf return value (scanf returns the number of arguments successfully matched).
Mieczyslaw1683 25-Dec-20 18:10pm    
Okey. Thank you for the answer.
CPallini 26-Dec-20 9:35am    
You are welcome.

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