LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-18-2005, 10:16 PM   #1
its_godzilla
LQ Newbie
 
Registered: Jan 2005
Posts: 10

Rep: Reputation: 0
C program that takes input "FName|LName" and outputs to std out


FAST HELP NEEDED!!!!!!

I am new to the C language....... This is my problem:
I need to write a C program to read in a persons name. The name will be passed to me in this format
FName | LName
If I was able to prompt for first name and last name that would no problem but in this situation I can't. I need to read in the single string and and store the fname and lname.

This is what I currently have which is not working because I have incompatible types and comparison between pointer and integer errors, I'm sure there is a better way to do this instead of these if statements. I was thinking of sscanf but i'm not 100% sure how to use this, like I said I am new to C.

Here is my code its a very simple program (it should be):
main()
{

char buff[50];
char fname[25];
char lname[25];
int pipe = 0;
int k = 0;
memset(fname, '\0', sizeof(fname));
memset(lname, '\0', sizeof(lname));

while (gets(buff) != EOF)
{
gets(buff);
fname = ' ';
lname = ' ';

while (k <= length(buff))
{
if (buff[k] =='|')
pipe = 1;
if ((pipe = 0) && (buff[k] != '|'))
fname = fname + buff[k];
if ((pipe = 1) && (buff[k] != '|'))
lname = lname + buff[k];
k++;
}
}

printf("%s%s%s" , fname, lname, fname"."lname"@xxxxxxx.com");
}

PLEASE HELP
FAST HELP NEEDED!!!!!!

I am new to the C language....... This is my problem:
I need to write a C program to read in a persons name. The name will be passed to me in this formatt
FName | LName
If I was able to promt for first name and last name that would no problem but in this situation I can't. I need to read in the single string and and store the fname and lname.

This is what I currently have which is not working because I have incompatible types and comparison between pointer and integer errors, I'm sure there is a better way to do this instead of these if statements. I was thinking of sscanf but i'm not 100% sure how to use this, like I said I am new to C.

Here is my code its a very simple program (it sould be):
main()
{

char buff[50];
char fname[25];
char lname[25];
int pipe = 0;
int k = 0;
memset(fname, '\0', sizeof(fname));
memset(lname, '\0', sizeof(lname));

while (gets(buff) != EOF)
{
gets(buff);
fname = ' ';
lname = ' ';

while (k <= length(buff))
{
if (buff[k] =='|')
pipe = 1;
if ((pipe = 0) && (buff[k] != '|'))
fname = fname + buff[k];
if ((pipe = 1) && (buff[k] != '|'))
lname = lname + buff[k];
k++;
}
}

printf("%s%s%s" , fname, lname, fname"."lname"@xxxxxxx.com");
}

PLEASE HELP
 
Old 01-18-2005, 10:19 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Check out the strtok function from string.h in libstdc... You can tokenize the original string using | as a dilimeter. The first token is your first name, second token is your last name...

Also.. that printf statement doesn't look good to me. Are you trying to print out "first last first.last@xxxxx.com"? If so it would look like this: printf("%s %s %s.%s@xxxxx.com\n",first, last, first, last);

Last edited by jtshaw; 01-18-2005 at 10:22 PM.
 
Old 01-18-2005, 10:26 PM   #3
LittlesnowLinux
Member
 
Registered: Nov 2004
Location: China
Distribution: Debian
Posts: 30

Rep: Reputation: 15
if ((pipe = 0) && (buff[k] != '|'))
fname = fname + buff[k];
if ((pipe = 1) && (buff[k] != '|'))
lname = lname + buff[k];

Above is the bug of your program.You must use
fname[k]=buff[k];
to solve this problem.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Postfix: "smtpd_client_restrictions" takes no effect? Chowroc Linux - Networking 5 11-01-2005 09:42 AM
IPW2200 Make error macro "create_workqueue" passed 2 arguments, but takes just 1 jplev22 Linux - Wireless Networking 2 09-16-2005 11:34 AM
What is "using namespace std"? NCC-1701&NCC-1701-D Programming 1 06-29-2005 08:56 AM
Getting error: "ALSA device "default" is already in use by another program." brynjarh Debian 7 02-04-2005 11:45 AM
<input type="button" disabled="true" > does not work in ns4.7 or 4.9 cybercop12us Programming 2 11-29-2002 08:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration