-3
#include <stdio.h>
#include <conio.h>
#include <string.h>

main()
{
float gross=0,otp=0,ot=0,pay=0;
int IC,hours=0;
char name[50];
char category;
char rep = 'y';


while(rep == 'y')
{

printf("\n\n Name : ");
gets(name);
printf("\n NRIC : ");
scanf ("%d",&IC);
printf("\n Category  : ");
scanf ("%s",&category);
printf("\n Total Hours  : ");
scanf("%d",&hours);

if (category = 'A1')  //Line 25
{
 if (hours < 44)
 {
    printf("\n INVALID INPUT\n");
 }
 else if (hours >= 44 && hours <= 60) 
 {
    gross= 44*5;
    ot= (hours-44)*(1/2*5);
    pay=gross+ot;
     printf("\n          Syarikat Smart Store Hypermarket Sdn. Bhd. ");
     printf("\n  =============================================================="); //Line 39
     printf("\n Name: %s", name);
     printf("\n NRIC: %d", IC);
     printf("\n Category: %s", category);
     printf("\n Total Hours: %d", hours);
     printf("\n Gross Pay: RM %.2f", gross);
     printf("\n Overtime Pay: RM %.2f", ot);
     printf("\n Net Pay: RM %.2f", pay);
 }
 else 
  {
     printf("\n\n INPUT NOT VALID");

I don't see any error in the code except for Line 25 which states multi-character character constant warning but the program works till line 39 and crashes. Any idea why or is there any error in my code that causes this?

Durairaj
  • 3
  • 3
  • Hello and welcome to stackoverflow. In order to enable other people to help you, you should aim to post a [to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Without this it becomes more difficult to spot what the problem is as it might be in parts of the code you are not showing – Erik Aug 25 '15 at 14:01

1 Answers1

1

There are at least two problems, one of which is already indicated by the compiler: 'A1' is not a single character which is what you would enclose in single quotes ('). You would need double quotes.

In addition and probably more important: you are not actually performing a comparison in line 25, you are changing the value of category ( = vs == ) so this means that you will enter this condition also if your input has nothing to do with A1 and thus other things could be wrong or unsuited for entering this particular if.

EDIT: if you are using indeed C, then you should use a difference function to compare your strings as indicated in this page pointed to in this thread

so you need to add at least

#include <string.h>

then change your if to something like

  if ( strncmp(category,"A1",2) == 0 )  //Line 25  

where I assume that you change the definition of your category variable to e.g. char[50] as you have for name and that your categories always only have two letters.

My compiler in addition warns

warning: ‘char* gets(char*)’ is deprecated

so you should probably also consider moving away from that.

Community
  • 1
  • 1
Erik
  • 2,137
  • 3
  • 25
  • 42
  • Hello Erik, thanks for the welcome. I did try '== "A1"' but then again another error saying ISO C++ forbids comparison between pointer and integer. Sorry I'm new to C language. What does this issue indicate? – Durairaj Aug 26 '15 at 02:49
  • @DJRaj it is going to be very hard to help without seeing the relevant part of the code. The message indicates that you are comparing an integer number to a `pointer`, i.e. a variable that holds a memory address. Especially if you are new to a language, you should make sure to read up on the basics, otherwise your progress will be slow and people on this site are unlikely to be willing to help you if you are coming with very basic questions that are part of any introductory course in C++. – Erik Aug 26 '15 at 09:11
  • By the way, you say you are new to `C` but in the title of your question you say `C++`. Be sure that you understand what language you want to work in as they are very different (even if technically C++ supports large parts of C, see e.g. [here](https://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B) ) – Erik Aug 26 '15 at 09:13
  • Alright, I'll edit those immediately for a better view and the title I meant, when I run my code in Dev C++ program . – Durairaj Aug 27 '15 at 14:55
  • That works like magic. Thanks, now that's a new learning. It didn't show any error for strcmp. – Durairaj Aug 28 '15 at 16:26
  • @DJRai in case this answer helped you solve your problem you can consider 'accepting' it by clicking on the hollow check mark on the top left of the answer ( see http://stackoverflow.com/help/accepted-answer ). – Erik Aug 28 '15 at 18:10