There are quite a few errors her so bear with me.
Never use gets. LINK Replace it with fgets. LINK
What is this??? What is gets supposed to be? I think that you want to check the string that you just got with gets. That would just be
Code:
if( firstname[0] == '\0')
That would check to see if the first character was a null character symbolizing the end of the string.
Code:
strcpy(name, lastname);
strcpy is just putting lastname over the previous contents of name. You want strcat. strcat adds the second argument to the end of the first one. So you would use the same to arguments as you used in strcpy.
Code:
while(y != '\0')
{
printf("%C", string[x]);
}
y is initialized as an exclamation point(!). Therefore '!' will never be '\0'. This is an infinite loop. And the does not do what you want it to. x has not yet been initialized. This is just taking any random number and trying to access that number's spot in string. There is a very good chance that this is way out of the array's bounds. I think that you wanted
Code:
x = 0; /*Set x to 0 so that it will start at the beginning of the array*/
while( string[x] != '\0' ) /*While the current spot in string is not the end of the string*/
{
putchar( string[x] ); /*Print out the current letter*/
x++; /*Increment x by 1 so that it will move up to the next spot in the array*/
}
There is no need to use getch at the end. You can just replace this with getchar. If you do use getch you would need to include conio.h which is not a standard header and not all compilers have the same version of this.
The defines do not do much. You are only using the numbers once so it is not necessary but it can be desired for easier reading. It is not a rule but it is common practice to make your defines all caps. so firstnames would be FIRSTNAMES or something like that. Also you have two defines that both are the same thing so if you wanted to you could make them both NAMELEN.
exit is not necessary. Exit is used to return a number to the OS. exit(0) is the same as return 0;. If you do use exit you need to include stdlib.h.
This would make the final code:
Code:
#include <stdio.h>
#include <string.h>
#define NAMELEN 15
#define FULLNAMELEN 30
int main ()
{
char firstname[NAMELEN];
char lastname[NAMELEN];
char name[FULLNAMELEN];
char *string;
int x;
char y = '!';
string = name;
printf("What yo first name be son?\n");
fgets(firstname, sizeof(firstname), stdin);
if(firstname[0] == '\0')
{
printf("you know yo name ain't dat long.\n");
return 1;
}
strcpy(name, firstname);
printf("and yo last name?\n");
fgets(lastname, sizeof(lastname), stdin);
if(lastname[0] =='\0')
{
printf("you know yo name ain't dat long.\n");
return 1;
}
strcpy(name, lastname);
printf("hmm. so this be yo name then?");
while(string[x] != '\0')
{
putchar(string[x]);
}
getchar();
}
I hope that this helps!.
~Sven