Thread: Blood Typing

  1. #1
    Premier Member
    Join Date
    May 2010
    Location
    Antarctica
    Posts
    31

    Post Blood Typing

    Here's a blood typing program I wrote on a sick bag on an airplane; it's been giving me mounds of trouble. The latest development is that when I try to print the blood types it doesn't show anything.

    Sorry for making your eyes bleed, I'm not really going for efficiency here and I've hackneyed more solutions that I can count.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    char bloody [3] = "O- ";
    
    void get_blood_type() {
        if ((rand() % 1) == 1)
            if ((rand() % 1) == 1)
    			bloody [0] = 'A';
    	if ((rand() % 1) == 1)
    		if ((rand() % 1) == 1)
    			bloody [0] = 'B';
    	if ((rand() % 1) == 1)
    		bloody [0] = '+';
    	if ((rand() % 1) == 1)
    		if ((rand() % 1) == 1)
    			bloody [0] = "AB+";
    		if ((rand() % 1) == 1)
    			bloody [0] = "AB-";
    	return;
    }
    int verify(char recp [3], char blood [3]) {
    	int eval [3];
    	if (blood [1] == 'B') {
    		eval [0] = 1;
    		eval [1] = 1;
    	
    	if (blood [2] == '+')
    		eval [2] = '1';
    	else
    		eval [2] = '0';
    	}
    	else {
    		if (blood [0] == 'O') {
    			eval [0] = 0;
    			eval [1] = 0;
    		}
    		else if (blood [0]  == 'A') {
    			eval [0] = 1;
    			eval [1] = 0;
    		}
    		else if (blood [0] == 'B') {
    			eval [0] = 0;
    			eval [1] = 1;
    		}
    		if (blood [1] = '+')
    			eval [2] = 1;
    		else
    			eval [2] = 'O';
    	}
    int rval [3];
    		if (recp [1] == 'B') {
    			rval [0] = 1;
    			rval [1] = 1;
    		
    		if (recp [2] == '+')
    			rval [2] = '1';
    			else
    				rval [2] = '0';
    		}
    		else {
    			if (recp [0] == 'O') {
    				rval [0] = 0;
    				rval [1] = 0;
    			}
    			else if (recp [0]  == 'A') {
    				rval [0] = 1;
    				rval [1] = 0;
    			}
    			else if (recp [0] == 'B') {
    				rval [0] = 0;
    				rval [1] = 1;
    			}
    			if (recp [1] = '+')
    				rval [2] = 1;
    			else
    				rval [2] = 'O';
    		}
    		int z = 0;
    		for (int i = 0; i == 2; i++)
    			if (eval [i] <= rval [i])
    				z++;
    		if (z == 3)
    			return 1;
    		return 0;
    	}
    int main() {
    	char answer;
    	int answerint;
    	char donor [3];
    	char recip [3];
    	printf ("Hello! I'm going to ask you a few questions about blood typing. Answer lowercase \"y\" or \"n\" for yes\
     or no respectively; answer \"q\" to quit.\n");
    	for (;1==1;) {
    		get_blood_type();
    		for (int i = 0; i == 2; i++) {
    			donor [i] = bloody [i];
    		}
    		get_blood_type();
    		for (int i = 0; i == 2; i++) {
    			recip [i] = bloody [i];
    		}
    		printf("Can %s donate to %s?\n", donor, recip);
    		scanf("%c", &answer);
    		if (answer == 'y')
    			answerint = 1;
    		if (answer == 'n')
    			answerint = 0;
    		if (answer == 'q')
    			return 0;
    		if (answerint == verify(recip, donor))
    			printf("Good job! Next question.\n");
    		else
    			printf("Nope.\n");
    	}
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void get_blood_type() {
        if ((rand() % 1) == 1)
            if ((rand() % 1) == 1)
    What's the remainder of any whole number divided by 1?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    None of your char strings have enough space to hold AB+ or AB- because you've neglected to account for the required null terminator character.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by mszegedy
    Code:
    char bloody [3] = "O- ";
    
    void get_blood_type() {
        if ((rand() % 1) == 1)
            if ((rand() % 1) == 1)
    			bloody [0] = 'A';
    	if ((rand() % 1) == 1)
    		if ((rand() % 1) == 1)
    			bloody [0] = 'B';
    	if ((rand() % 1) == 1)
    		bloody [0] = '+';
    	if ((rand() % 1) == 1)
    		if ((rand() % 1) == 1)
    			bloody [0] = "AB+";
    		if ((rand() % 1) == 1)
    			bloody [0] = "AB-";
    	return;
    }
    Your bloody array doesn't have enough space to store the '\0' character. And that whole function can be simplified to (Assuming you're trying to do what I think you are):
    Code:
    char bloody[4] = { 0 };
    
    void get_blood_type()
    {
      char suffix = (rand() % 2) ? '-' : '+';
      char *types[] = { "O", "A", "B", "AB" };
    
      strcpy(bloody, types[rand() % 4]);
      bloody[strlen(bloody)] = suffix;
    }
    Last edited by itsme86; 03-22-2011 at 04:59 PM.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Premier Member
    Join Date
    May 2010
    Location
    Antarctica
    Posts
    31
    Well, I was originally gonna have g_b_t return an array of chars, but it refused to accept that, so it was made into a global variable.

    Maybe I'd be better off writing this in Lisp... :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Speed in Touch Typing
    By stevesmithx in forum A Brief History of Cprogramming.com
    Replies: 60
    Last Post: 12-01-2008, 10:01 AM
  2. How to avoid typing std:: (scope resolution operator)
    By Sharan in forum C++ Programming
    Replies: 9
    Last Post: 04-30-2006, 08:25 PM
  3. Hide or Mask Typing
    By Jperensky in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2002, 12:10 PM