-3

so I'm starting off with c and c++ and I am at structures, I've made a strcture for employees, defined the number of employees and made 2 functions: 1 to fill employees info and 1 to print them:

#include <iostream>
#include <cstdio>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define NO 3
using namespace std;
  struct employee {
    char fName[15];
    char lName[15];
    unsigned int age;
    char gender;
    bool married;
    char Pp[5];
  };

    typedef struct employee Employee;
    void fillEmployeeDetails(Employee * const employeePtr);
    void printEmployeeDetails(const Employee * const employeePtr);

  int main(){
    Employee Employees[NO];
    fillEmployeeDetails(Employees);
    printEmployeeDetails(Employees);
    //printf("%s",&(Employees[0].Pp));
    return 0;
  }

    void fillEmployeeDetails(Employee * const employeePtr){
        char marriageStatus;
        int i;
      for(i = 0; i < NO + 1; ++i){
            marriageStatus = '\0';
        system("CLS");
        printf("No. of employees: %d\n\n", NO);
        printf("Employee No. %d:\n\n", i+1);
        printf("First name: ");
        scanf("%14s", employeePtr[i].fName);
        printf("Last name: ");
        scanf("%14s", employeePtr[i].lName);
        printf("Age: ");
        scanf("%u", &(employeePtr[i].age));
        while(employeePtr[i].age > 150 || employeePtr[i].age < 0){
            printf("Age: ");
            scanf("%u", &(employeePtr[i].age));
        }
        while(employeePtr[i].gender != 'M' && employeePtr[i].gender != 'F' ){
            printf("Gender: ");
            scanf("%s", &(employeePtr[i].gender));
            employeePtr[i].gender = toupper(employeePtr[i].gender);
        }
        while(marriageStatus != 'Y' && marriageStatus != 'N'){
            printf("Married?(Y/N): ");
            scanf("%s", &marriageStatus);
            marriageStatus = toupper(marriageStatus);
            if(marriageStatus == 'Y' && employeePtr[i].gender == 'F'){
                employeePtr[i].married = true;
                strcpy(employeePtr[i].Pp, "Mrs.");
                }
            else{
                if(marriageStatus != 'Y' && employeePtr[i].gender == 'F'){
                 strcpy(employeePtr[i].Pp, "Miss");
                    }
                else{
                  strcpy(employeePtr[i].Pp, "Mr.");
                    }
                }

            }
      }
    }
    void printEmployeeDetails(const Employee * const employeePtr){
        system("CLS");
        for(int i = 0; i < NO; ++i){
          printf("Employee No.: %d\n\nName: %s %s %s\nAge: %u\nGender: %c\n\n\n",i,employeePtr[i].Pp,employeePtr[i].fName,employeePtr[i].lName,employeePtr[i].age,employeePtr[i].gender);
        }
    }

I have a problem in function fillEmployeeDetails at

scanf("%s", &marriageStatus); 

in which this line is changing my i(if i =>1) to 0, any help would be appreciated.

Galik
  • 47,303
  • 4
  • 80
  • 117
Onion
  • 1
  • 1
  • 7
    `scanf("%s", &marriageStatus);` you can't scan a string into a `char`, change to `%c` – David Ranieri Nov 17 '16 at 07:56
  • `scanf("%s", &(employeePtr[i].gender));` --> `scanf(" %c", &employeePtr[i].gender);`, Also `while(employeePtr[i].gender != 'M'...` : First time using uninitialized variable. – BLUEPIXY Nov 17 '16 at 07:58
  • 2
    Why are you mixing C and C++ functions when writing new code? – George Nov 17 '16 at 08:02
  • @SouravGhosh, if you remove the `c++` headers and `namespace`, it's pure `c` s far as I can see – StoryTeller - Unslander Monica Nov 17 '16 at 08:05
  • 2
    To the OP: Don't *"start with c and c++"*. Those two are different languages. You can learn both, but learn each separately and properly. – StoryTeller - Unslander Monica Nov 17 '16 at 08:07
  • @StoryTeller having same syntax does not mean the language properties will be the same. They __are__ different. – Sourav Ghosh Nov 17 '16 at 08:07
  • @SouravGhosh You don't have to lecture me about the difference. But look at how the code is written. Nix the `C++` parts as I said, add an include for `stdbool.h` and you have legal `c` here. – StoryTeller - Unslander Monica Nov 17 '16 at 08:09
  • @StoryTeller I did not mean to "lecture" you, please ignore the comment if you disagree but be polite, please. :) – Sourav Ghosh Nov 17 '16 at 08:11
  • I was polite (or at least, wasn't trying to be rude). I was pointing out I'm aware of the difference, very intimately even. My point still stands. – StoryTeller - Unslander Monica Nov 17 '16 at 08:13
  • @StoryTeller The language is determined by the compiler being used to compile it, rather than the lowest common denominator. – Galik Nov 17 '16 at 08:13
  • @Galik I'd say the language is determined by the grammar being followed. And while this isn't `C` *as written*, it's `C` *in spirit*. And therefore, in my opinion, should be made proper `C` instead of a mutant hybrid of the two. – StoryTeller - Unslander Monica Nov 17 '16 at 08:16
  • @StoryTeller The compiler enforces the grammar being followed. – Galik Nov 17 '16 at 08:23
  • @Galik, no, the compiler verifies the grammar being followed. And if a program is a valid word in the compilers programming language, the compiler transforms it into something else following some semantic rules. None of the above preclude a program from being valid in both languages (although maybe not idiomatic), they *do* have a common denominator. – StoryTeller - Unslander Monica Nov 17 '16 at 08:26
  • @StoryTeller You said "the language is determined by the grammar being followed". I pointed out that the *compiler* dictates what grammar is followed. Therefore the language is determined by the compiler. The fact that the same program can be valid in different languages doesn't change that. – Galik Nov 17 '16 at 08:32
  • @Galik A program can be valid in two different grammars, than it follows two different grammars, irregardless of verification. This is basic automata theory. – StoryTeller - Unslander Monica Nov 17 '16 at 08:38
  • I thank you for your tips, I appreciate the help enormously – Onion Nov 17 '16 at 20:26

1 Answers1

1

You've declared marriageStatus as type char. The call to scanf("%s", &marriageStatus); is overwriting adjacent areas of memory because marriageStatus can only hold 1 char.

Try replacing the declaration with: char marriageStatus[10];

Beware that the program will crash with input greater than 9 (leaving room for the NULL byte), so you need to replace your scanf call with: scanf("%9s", marriageStatus);

If you don't specify the number of characters to be read you create massive security holes in your program.

EDIT: Actually looking through the code it looks like you actually intended marriageStatus to be a char, in which case, you want scanf(" %c", marriageStatus); as has already been pointed out.

If you are expected either 'Y' or 'N', you may want to include a check that it was read correctly.

Forrest
  • 236
  • 1
  • 7