The following returns an odd result in objective c.
#import <Foundation/Foundation.h>
#import <math.h>
#import <stdio.h>
#import <string.h>
char risk[2];
char gen1[1];
printf("Enter gender: ");
scanf("%s",gen1);
printf("Enter risk: ");
scanf("%s",risk);
And I get as gen1 the result of gen1 + risk instead of just gen1. In other words if gen1 = “m” and risk = “ns” then I’m getting gen1 = “mns” instead of just “m”.
In a previous version this worked just fine.
Recently updated OS X to 7.8 along with the new preview XCode.
Not sure what I’m doing wrong here.
Thanks all,
Lou
You have to make room for the terminating NUL character:
The probable reason for gen1 being “mns” is that allocated on the stack immediately after risk, and scanf overwrites the terminating NUL chatacter of gen1, effectively concatenating it with risk.