Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 A program with functions
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Ceevu




PostPosted: Sun Apr 15, 2007 8:23 pm   Post subject: A program with functions

I'm trying to get this program running. It's purpose is to act as an information grabber (think of a MUD when creating a new character... a few of the fields are similar).

Quote:
#include <stdio.h>
#include <string.h>


/* Declarations of Functions */
char name(char *first, char *last); /* just testing this out. Originally was: char name (char *first, char *last) */
int age(int how_old);
int height(int tall);
int weight(int mass);
char desc(char *description);

int main()
{
/* call the functions */
char name(char *first, char *last);
int age();
int height();
int weight();
char desc();

/* Not certain about adding 'name, age, height, weight, desc to 'return', just trying it out */
return name, age, height, weight, desc;
}

char name (char *first, char *last)
{

printf("Please enter a first name: \n");
scanf("%s", &first);
printf("Enter a last name: \n");
scanf("%s", &last);
return 0;
}

int age (int how_old)
{

printf("How old are you? \n");
scanf("%d", &how_old);
return 0;
}

int height (int tall)
{
printf("How tall are you, in inches? \n");
scanf("%d", &tall);
return(tall);
}

int weight (int mass)
{
printf("What is your weight, in Kg.? \n");
scanf("%d", &mass);
return (mass);
}

char desc (char *description)
{
printf("Write two sentences to describe yourself.\n\n");
gets(description);
return(*description);
}
Sponsor
Sponsor
Sponsor
sponsor
rdrake




PostPosted: Sun Apr 15, 2007 9:55 pm   Post subject: Re: A program with functions

c:

/* Declarations of Functions */
char name(char *first, char *last);   /* just testing this out. Originally was: char name (char *first, char *last) */
int age(int how_old);
int height(int tall);
int weight(int mass);
char desc(char *description);
Not sure why you are using pointers at all here.

c:

int main()
{
/* call the functions */
     char name(char *first, char *last);
     int age();
     int height();
     int weight();
     char desc();
     
/* Not certain about adding 'name, age, height, weight, desc to 'return', just trying it out */
     return name, age, height, weight, desc;
}
You really aren't calling the functions correctly... try passing values to them instead of non-existent variables. Call them like the following...

c:
age(5);
height(42);
etc.


Perhaps you should consider assigning the values returned from the functions into a variable so you can actually use said values.

The main function should only return 0 on success or 1 on failure, nothing more. It is suppost to return an integer of some kind at the very least.

c:

char name (char *first, char *last)
{
     
     printf("Please enter a first name: \n");
     scanf("%s", &first);
     printf("Enter a last name: \n");
     scanf("%s", &last);
     return 0;
}
Really I'm not too sure why you are using pointers here. Also, you stated your function should return a character, yet it returns an integer.

c:

int age (int how_old)
{

     printf("How old are you? \n");
     scanf("%d", &how_old);
     return 0;
}
I'm really not sure why you are not returning how_old instead of 0.

c:

int height (int tall)
{
     printf("How tall are you, in inches? \n");
     scanf("%d", &tall);
     return(tall);
}
Now you're getting it!

c:

int weight (int mass)
{
     printf("What is your weight, in Kg.? \n");
     scanf("%d", &mass);
     return (mass);
}
Again no issues here.

c:

char desc (char *description)
{
     printf("Write two sentences to describe yourself.\n\n");
     gets(description);
     return(*description);
}
Again unsure of why you are using pointers here.

Uh, I think that's it for now. Calculus has fried my brain for the day.
Bobrobyn




PostPosted: Sun Apr 15, 2007 10:15 pm   Post subject: Re: A program with functions

c:
/* Not certain about adding 'name, age, height, weight, desc to 'return', just trying it out */
return name, age, height, weight, desc;


I just looked through your code quickly....and just look at this. You can't return more than one thing, I hope you realize that. Part of a functions definition is that it only returns ONE thing.

c:
/* call the functions */
char name(char *first, char *last);
int age();
int height();
int weight();
char desc();


I've never seen functions used like this before, in C....but it seems to compile, but I don't really know what it does, so I won't say anything. However, whenever I call a function, I usually do something like:

c:

age = age();
/* or */
age();


Depending on what I want done.

Another thing, I don't see anything being sent into your functions. You functions take a paramter, but you aren't sending one in. For example:

[syntax="c"]
age(&how_old);
[syntax]

However, for what you want, I think you'd want the function to get the age from the user, then return it to a variable in the main.

Here's some sample code:

c:

#include <stdio.h>
#include <string.h>

/* This is the function prototype */
int getNumber();

int main() {
        int number;

        number = getNumber();

        printf("The number you entered is:  %d\n", number);
       
        return 0;
}

int getNumber() {
        int numberEntered;
       
        printf("Enter a number:  ");
        scanf("%d", &numberEntered);
        printf("\n"); /* Just putting a newline in */
       
        return numberEntered;
}



You did this for the height of the person, or something similiar to it. However, you never set a variable in the main function equal to it, so it was kind of pointless.

Alright, and here's another bit of your code:

c:

char name (char *first, char *last)
{

printf("Please enter a first name: \n");
scanf("%s", &first);
printf("Enter a last name: \n");
scanf("%s", &last);
return 0;
}


I don't think you're at the part of the game where you want to use pointers and dynamic memory. That's for later. I'd suggest you use array of characters. And just so you know, because it's an array, you CAN pass it into a function and have it manipulate the actual array data. Here's an example for strings:

c:

#include <stdio.h>
#include <string.h>

/* This is the function prototype */
void getString(char *string);

int main() {
        char string[64]/* I just made it 64 characters long for the sake of it */

        getString(string);

        printf("The string you entered is:  %s\n", string);
       
        return 0;
}

void getString(char *string) { /* The * means it's a pointer...essentually, it's pointing to the array of characters called string.  You can learn more about them later :-P */
       
       
        printf("Enter a string:  ");
        scanf("%s", string);
        printf("\n"); /* Just putting a newline in */
}


So you know, a string is an array of characters, and is terminated (basically, it ends with) the character '\0'. Without the '\0', there is no string.

I hope this helps. Here are some tutorials from the C++ section that are for C, that you might find helpful:

http://www.compsci.ca/v3/viewtopic.php?t=408
http://www.compsci.ca/v3/viewtopic.php?t=12911
http://www.compsci.ca/v3/viewtopic.php?t=6424

Also, I might suggest the book "C For Dummies" by Dan Gookin. It does a good job of explaining a lot of concepts, including strings, pointers and complex data types. There are probably other good books out there, as well as ebooks available, as well.
Andy




PostPosted: Mon Apr 16, 2007 10:24 pm   Post subject: RE:A program with functions

err rdrake, do you know c? strings are represented as character arrays in c, thus the character pointers...
rdrake




PostPosted: Mon Apr 16, 2007 11:18 pm   Post subject: Re: RE:A program with functions

Andy @ Mon Apr 16, 2007 10:24 pm wrote:
err rdrake, do you know c? strings are represented as character arrays in c, thus the character pointers...
It's been a while since I've used C, evidently Confused. Always thought of strings in C as character arrays terminated with '\0' myself, never thought about the character pointer bits.

Pointers seem a bit unnecessary at this point in time for the OP Smile.
Bobrobyn




PostPosted: Thu Apr 19, 2007 11:11 pm   Post subject: Re: RE:A program with functions

rdrake @ Tue Apr 17, 2007 12:18 am wrote:
Andy @ Mon Apr 16, 2007 10:24 pm wrote:
err rdrake, do you know c? strings are represented as character arrays in c, thus the character pointers...
It's been a while since I've used C, evidently Confused. Always thought of strings in C as character arrays terminated with '\0' myself, never thought about the character pointer bits.

Pointers seem a bit unnecessary at this point in time for the OP Smile.


I'd agree here. I'm a bit late in saying, but the guy isn't into the whole dynamic memory thing yet, so he shouldn't be using character pointers. Instead, he should be using character arrays.
wtd




PostPosted: Thu Apr 19, 2007 11:56 pm   Post subject: Re: RE:A program with functions

Bobrobyn @ Fri Apr 20, 2007 12:11 pm wrote:
rdrake @ Tue Apr 17, 2007 12:18 am wrote:
Andy @ Mon Apr 16, 2007 10:24 pm wrote:
err rdrake, do you know c? strings are represented as character arrays in c, thus the character pointers...
It's been a while since I've used C, evidently Confused. Always thought of strings in C as character arrays terminated with '\0' myself, never thought about the character pointer bits.

Pointers seem a bit unnecessary at this point in time for the OP Smile.


I'd agree here. I'm a bit late in saying, but the guy isn't into the whole dynamic memory thing yet, so he shouldn't be using character pointers. Instead, he should be using character arrays.


False dichotomy alert!
md




PostPosted: Fri Apr 20, 2007 5:52 pm   Post subject: RE:A program with functions

Ahh the joys of fake arrays.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: