ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi I have had some problem with these functions I am using to write a program that is supose to ask the user for his/her first and last name and then accept input and store it in a string array, and then print it to the screen. I can do it C++ but C is different so any help is appreciated thank you.
Code:
#include<stdio.h>
int main()
{
char firstName[21];
char lastName[21];
char fullName[50];
printf("Please enter your first name: ");
scanf("%s", firstName);
printf("Please enter your last name: ");
scanf("%s", lastName);
strcat(fullName, firstName, lastName); //This is supose to assign both the firstName[] and lastName[] string arrays to the fullName[] string array.
printf("%s", fullName);
fflush(stdin);
getchar();
}
Last edited by InvisibleSniper; 09-21-2005 at 01:13 AM.
just breifly looking at it have not done c in awhile but I don't know if c recognizes the new commenting // try using the old style */ it may be having trouble interpreting the comment styles...
Originally posted by kencaz just breifly looking at it have not done c in awhile but I don't know if c recognizes the new commenting // try using the old style */ it may be having trouble interpreting the comment styles...
KC
These comments were only added in this forum for the readers to understand what that function is trying to do. But just to let you know, I have used the "//" comments in Dev-C++ compiler while writing the source code in a file that was saved as .C and it compiled and worked perfectly.
So it's not the comments but something else, I don't exactly know what but when you compile it compiles fine although the out-put is unexpected.
Last edited by InvisibleSniper; 09-21-2005 at 01:25 AM.
Yes, you need "#include <stdio.h>" for "printf()", "scanf()", "getchar()" and friends, and "#include <string.h>" for "strcat()". But generally I'd expect these to be warnings - not fatal errors.
And I don't see anything in your source that would cause a crash.
So what exactly is the problem: is it a compile error? A runtime error? Can you cut/paste the error message?
"string.h" is a header, not a library. You #include <string.h> before you compile your program. We're assuming that maybe you got a compile error (or warning) complaining about "strcat()", which is one of the string-related functions declared in string.h.
Originally posted by paulsm4 No, kenkaz misspoke himself slightly.
"string.h" is a header, not a library. You #include <string.h> before you compile your program. We're assuming that maybe you got a compile error (or warning) complaining about "strcat()", which is one of the string-related functions declared in string.h.
strcat(); works fine without
Code:
#include<string.h>
. I only inlude <stdio.h> for strcat to work. There was no compile error, only a runtime error. I think it has to do with how many parameters I put in strcat, which was
Code:
strcat(fullName, firstName, lastName);
I am not quite sure as to how many parameters are legal, can someone please tell me how many are legal?
and replace
strcat(fullName, firstName, lastName);
with
strcat(fullName,firstName);
strcat(fullName,lastName);
but for such things i like sprintf(); more because you can easily format your string:
sprintf(fullName,"%s %s",firstName,lastName);
just because of safety:
you have a buffer overflow problem in your prog
e.g. what if the user enters more than 21 characters for his name?
or to be more correct, if the user enters more than 42 characters you get a segmentation fault because sscanf wants to read from stdin but the buffer it want's to write in (firstName and lastName) are 42 characters in size
so sscanf would write to a buffer where it isn't allowed to
to be more safe
replace
sscanf();
with
fgets(firstName,21,stdin);
and
fgets(lastName,21,stdin);
So what exactly is the problem: is it a compile error? A runtime error? Can you cut/paste the error message?
If you gave us the exact error, perhaps we could be of a bit more help ;-)
PS:
In most environments I know of, "strcat()" is declared in "string.h". If you try to compile a C++ program that uses "strcat()" on one of those platforms, the compile will die a fiery death. So it was a reasonable guess on our part.
And I hope you're clear about the difference between a compile-time header, a link-time library, and a runtime error. Apparently you're experiencing a runtime error. We're eager to hear exactly what that error is!
PS:
"man strcat" in a Linux environment should give you the prototype for "strcat()". It takes two arguments, not three. But I *don't* know if that would necessarily cause your crash.
PPS:
If you're on Linux, try compiling with "gcc -g ..." and running the program under the gdb debugger.
The error I got, as I said was a run time error. This is basically what happened, I compiled and ran the program and typed my full name into char firstName[21]; char lastName[21]; and once I hit enter for it to display my name I got a bunch of characters that were not part of the alphabet or even part of the numerical system.
I mean it was supose to display my name. Anyway I done something similar to what Thinking said and it worked fine. But still out of interest is it so that strcat() can only take 2 parameters?
And just out of interest again, will I ever need these console functions later on in life when I'm a professional programmer?
well i understand your runtime error but i don't really know what could cause the problem in your code
i didn't have this on my suse linux
as you said you get some crazy numbers and characters after you entered the lastName
this means that your printf(); does something strange
it's caused because you overwrote the ending binary 0 from the fullName string
so printf(); didn't know where the end of the string is and it printed what was in memory after the fullName string
maybe it would work if you initialize your arrays:
char firstName[21]={0};
char lastName[21]={0};
char fullName[50]={0};
btw: yes, strcat only takes 2 arguments
it concatenates the 2 strings
char *strcat(char *dest, const char *src);
if you have "hello" in buffer dest and " world" in src
then after strcat you have "hello world" in dest! and it returns dest
and for your second question:
hmm
good question
i would say yes
BUT
if you get better you can solve problems that are more complex than what you prog now
so yes, i think you will use them in future too, but in future there will be no problem for you to use them!
it will be as simple as to pick one's nose *g*
It still wouldn't work and would most likely print garbage like the stuff you are seeing. The reason is simple: In memory, "strings" are stored as arrays of characters with the final character in the string being followed by the character 0 (that's 0, as in null, not as in the letter "O" or the digit "0").
The strcat() function concatenates the second string onto the end of the first string, and it works by finding the terminating 0 character in the first string and then copying the second string to that location. But you did not initialize the data in fullName to anything before passing it to strcat(); so whatever garbage was in memory in the location that fullName happens to be in is what's there when you call strcat() on it, and there is most likely a lot of junk before the first random 0 occurs.
That sets the first character of fullName to 0, which effectively initializes it to an empty string. Then you can safely call strcat().
Your buffers are small, but that is not the source of your problem assuming you are typing less than 20 characters in for your first and last names. Not many people have a first or last name longer than 20 characters so anything larger would probably be overkill.
Additionally, if you want to play it safe, you can prevent overruns with scanf like this (once again, this is probably not the source of your original problem, but it's just if you're curious):
So they have old random data in them.
so when strcat comes along it cats stuff onto the random data.
I would put them outside the main, then they will be static buffers and intialised to zero.
Usually best to put buffers in static data rather than stack data as they are generally
larger storage areas.
and give them size, man.
Code:
#include<stdio.h>
#include<string.h>
#define BUFSZ 1024
char firstName[BUFSZ];
char lastName[BUFSZ];
char fullName[2 * BUFSZ];
int main()
{
printf("Please enter your first name: ");
scanf("%s", firstName);
printf("Please enter your last name: ");
scanf("%s", lastName);
strcat(fullName, firstName, lastName); //This is supose to assign both the firstName[] and lastName[] string arrays to the fullName[] string array.
printf("%s", fullName);
fflush(stdin);
getchar();
}
Last edited by bigearsbilly; 09-21-2005 at 03:55 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.