-1

For age >= 25 and a Unmarried Female the Output is not correct.

/* A company insures its drivers if either of the following conditions are satisfied Driver is married. Driver is an unmarried, male and above 30 years of age. Driver is unmarried, female and above 25 years of age. Write a program to decide if a driver is to be insured using logical operators. */

#include <stdio.h>

int main()
{
  char name[100]; 
  char ms, gender;
  int age;

  printf("\n\tNAME: ");
  scanf("%[^\n]%*c", name);

  printf("\n\tAGE: ");
  scanf("%d", &age);

  printf("\n\tMARRIED [Y/N]: ");
  scanf("%s",&ms);

  printf("\n\tGENDER [M/F]: ");
  scanf("%s",&gender);

  if(ms == 'Y'|| ms == 'y')//married
      printf("\n\t1. INSURED\n");

  else//unmarried
  {
      if(gender == 'M' || gender == 'm')//male
      {
          if(age >= 30)
              printf("\n\t2. INSURED\n");
          else
              printf("\n\t3. NOT INSURED\n");
      }
      else//female
      {
          if(age >= 25)
              printf("\n\t4. INSURED\n");     
          else
              printf("\n\t5. NOT INSURED\n");  
      }
  }
  return 0;
}

Kindly help me find the mistake. To locate the mistake I have numbered the output to know. Where the fault is. But I am not able to find any. the output:

NAME: 

AGE: 26

MARRIED [Y/N]: n

GENDER [M/F]: f

5. NOT INSURED
J...S
  • 5,079
  • 1
  • 20
  • 35
gaufler
  • 165
  • 1
  • 9
  • 1
    Fix-my-code requests are off topic. Compile with all warnings and debug info (`gcc -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/)....). **Use the debugger** `gdb` (to run your program step by step and query its state) and [valgrind](http://valgrind.org/). Beware of [undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior). Read [documentation](http://en.cppreference.com/w/c) – Basile Starynkevitch Oct 01 '17 at 06:49
  • @BasileStarynkevitch Sorry! I am not sure what you meant by "gcc -Wall -Wextra -g". – gaufler Oct 01 '17 at 06:52
  • GCC is a free software compiler (and the usual one on Linux distribution). I recommend using it (and I also recommend installing and using some Linux distribution if you are learning C programming). You should spend a few days reading documentation of your compiler and your debugger. – Basile Starynkevitch Oct 01 '17 at 06:53
  • I know about that. But, what is gdb and how am i to use it. – gaufler Oct 01 '17 at 06:54
  • GIYF. See [GDB](https://www.gnu.org/software/gdb/) and read its [documentation](https://sourceware.org/gdb/download/onlinedocs/gdb/index.html). Both `gcc` and `gdb` are command line tools, and you will use them in some terminal – Basile Starynkevitch Oct 01 '17 at 06:55
  • 3
    @rgo - have you heard of google? Its surprisingly good at answering trivial questions... – enhzflep Oct 01 '17 at 06:56
  • @enhzflep Oh! I have :) – gaufler Oct 01 '17 at 07:00

2 Answers2

2

Your variables ms and gender are single characters and not character arrays storing a string.

You are reading into ms with %s format specifier. Use %c format specifier instead which the one for reading into a char.

Like

scanf(" %c",&ms);

instead of

scanf("%s",&ms);

The space before the %c is to ignore the white spaces that may remain in the input buffer which would other have been read into ms instead of the real input.

J...S
  • 5,079
  • 1
  • 20
  • 35
  • 2
    BTW, any good compiler with all warnings enabled would have detected this mistake, hence my advice for `gcc -Wall -Wextra -g` – Basile Starynkevitch Oct 01 '17 at 06:57
  • @BasileStarynkevitch I did compile as you said: gcc -Wall -Wextra -g [filename].c and I didn't get any error message. – gaufler Oct 01 '17 at 06:59
  • I'm sure you got some warnings (in the terminal where you run that `gcc -Wall -Wextra -g filename.c -o progname` command). BTW, you might avoid [IDE](https://en.wikipedia.org/wiki/Integrated_development_environment) and compile with a command line in some terminal – Basile Starynkevitch Oct 01 '17 at 06:59
  • I didn't. There were no warnings or any message. I am using the terminal on Ubuntu 16.04 – gaufler Oct 01 '17 at 07:01
  • 1
    Ok. But did you use `gdb` ? – Basile Starynkevitch Oct 01 '17 at 07:02
  • @J...S When I compiled with "%s" as you asked. I was not able to input any data for the married and gender column. It went straight to infer 'Not Insured' I did use 26 as age. Sorry type error. I meant %c – gaufler Oct 01 '17 at 07:03
  • 1
    @rgo Don't compile with `%s`. Use ` %c` for both `ms` and `gender`. Notice the space before `%c`. – J...S Oct 01 '17 at 07:04
  • @J...S When I used "% c" with a space I got message: unknown conversion type character 0x20 in format [-Wformat=] scanf("% c",&ms); – gaufler Oct 01 '17 at 07:09
  • @rgo Space before the whole `%c`. Not between the % and c. – J...S Oct 01 '17 at 07:10
  • @J...S If you can Kindly check with this data: Age 26, Married: N, Gender: F. It suppose to output '4.Insured' but it prints out '5.Not Insured' – gaufler Oct 01 '17 at 07:11
  • @rgo See [demo](https://ideone.com/f7C66U). – J...S Oct 01 '17 at 07:13
  • @J...S Thank you! Kindly explain, why the space in " %c" is required. – gaufler Oct 01 '17 at 07:13
  • 1
    @rgo After reading into `age`, you press enter for `\n` or some white space, right? That won't be consumed by the `scanf()` reading `age` and would remain in the input buffer. This would be read by the next `scanf()` and seeing the `\n` it thinks the input for `ms` is over and goes to the next line. – J...S Oct 01 '17 at 07:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/155687/discussion-between-rgo-and-j-s). – gaufler Oct 01 '17 at 07:18
2

You declare ms and gender as char variable and you use format specifier of a char array.

So, you replace your scanf statement from any statement written below for ms and gender input.

 1. scanf("\n%c",&ms);

 2. scanf("\t%c",&ms);

 3. scanf("\r%c",&ms);

 4. scanf(" %c",&ms);

Similarly replace scanf statement for gender also.

Aashish Kumar
  • 2,771
  • 3
  • 28
  • 43
  • 1
    Notice that every space-like character `\n` `\t` `\r` and the space `' '` itself behave identically for `scanf` control string; so always using a space makes IMHO more readable code – Basile Starynkevitch Oct 01 '17 at 07:23