making a c function

Started by
2 comments, last by algumacoisaqualquer 16 years, 5 months ago
I'm really new to C. i posted a problem with my code yesterday that i was able to fix thanks a lot. All my program is in the main though and i want to create functions. i can't seem top pass the array down to te function. could someone show me how to do one so i can do the other thanks a lot. this is what i want to make a function:

for(k = 0; k < 40; k++)
	{
		if (open >= close) break;
		if (cLunch == open)
		{
			patient_string[k] = "***At Lunch***";
			open++;
		}
		else if (cMeeting == open)
		{
			patient_string[k] = "***Meeting***";
			open++;
		}
		else
		{
			printf( "How many patients had you between %d", open);
			open++;
			printf(":00 and %d:00: ", open);
			scanf("%d", &patients);
			patient_int[k] = patients;
		}
	}

this is all y code:

#include <stdio.h>
#include <string.h>
int main()
{
	int open, close, o, c, cLunch, cMeeting;
	char holder;
	int patients; 												// padding out with h
	char *patient_string[20] = {"h","h","h","h","h","h","h","h","h","h","h","h","h","h","h","h","h","h","h","h"};
	int patient_int[100] = {0};
	int k, x = 0;
	printf("what time do you open ");
	open = 9;         //scanf("%d",&open);
	printf("what time do you close ");
	close = 15;                  //scanf("%d",&close);
	o = open;
	c = close;

	printf("What time does your surgery close for lunch: ");
	cLunch = 12; //scanf("%d",&cLunch);
	
	printf("Is there a meeting, Y or N: ");
	holder = 'y';         //scanf("%s",&holder);
	if (holder == 'y' || holder == 'Y')
	{
		printf("what time do you have your meeting: ");
		cMeeting = 9;      //scanf("%d",&cMeeting);
	}
	
	
	for(k = 0; k < 40; k++)
	{
		if (open >= close) break;
		if (cLunch == open)
		{
			patient_string[k] = "***At Lunch***";
			open++;
		}
		else if (cMeeting == open)
		{
			patient_string[k] = "***Meeting***";
			open++;
		}
		else
		{
			printf( "How many patients had you between %d", open);
			open++;
			printf(":00 and %d:00: ", open);
			scanf("%d", &patients);
			patient_int[k] = patients;
		}
	}
	
	
	for(x = 0; x < 40; x++)
	{
		if (patient_string[x] != "h")
		{
			printf("%d:00 - ",o);
			o++; 
			printf("%d:00",o);	
			printf("     %s\n", patient_string[x]);
		}
		if (patient_int[x] != 0)
		{
			printf("%d:00 - ",o);
			o++; 
			printf("%d:00",o);
			printf("     %d\n", patient_int[x]);
		}
		if (patient_string[x] == "h" && patient_int[x] == 0) break;
		
	}
	
	
	return 0;
}




Advertisement
Before I answer, is there any particular reason you are using the C language? I ask it because you seem to be having quite a hard time dealing with strings at C. Take your patient_string array for instance. You are initializing each element as a "h" string. But this means that each one of those strings can only be one character long, or you will get quite some problems.

Also, you are using k = 40, but iterating by a 20 element array.

Anyway, if you want to pass an array to a function, you will have to either pass it by a pointer or by reference. But before you learn these, maybe you should try to make simpler programs, things that don't need so much strings like the one you are trying to do.
Quote:Original post by algumacoisaqualquer
Before I answer, is there any particular reason you are using the C language? I ask it because you seem to be having quite a hard time dealing with strings at C. Take your patient_string array for instance. You are initializing each element as a "h" string. But this means that each one of those strings can only be one character long, or you will get quite some problems.

Also, you are using k = 40, but iterating by a 20 element array.

Anyway, if you want to pass an array to a function, you will have to either pass it by a pointer or by reference. But before you learn these, maybe you should try to make simpler programs, things that don't need so much strings like the one you are trying to do.


I use c++ normally so ya i'm not great at c. I initialiesed it with h for comparison purposes and i know the program's really messy at the moment. i haven't begun to clean it up yet.

Ok, but is there any particular reason why you moved to C?
The problem is that you are using a "char*" as if it were a complete string class. It is not, it's just a pointer to an array of chars.

Anyway, the simpler way to make this work is to make char *patient_string[20] a global variable (well, and some others), and then detaching the code you want into a separate function. This is a very poor solution to your problem (if you make everything global, soon you will be left with a great mess), but is the one I can think that doesn't deal with pointers. Actually, don't do the global thing, try to learn how to make a simpler function, and build up from there (do you know how to make that in C++?).

This topic is closed to new replies.

Advertisement