Teen Programmers Unite  
 

 

Return to forum top

C++ simple program

Posted by Maida [send private reply] at March 28, 2002, 06:49:34 PM

Does anybody out there know how I can get the program that I am trying to do function as what it is supposed to do, the program is for the user to input name and address, each element separated by '*' ex. Tim Henson*London*UK, the end result should be
Tim Henson
London
UK

Please I am going mad!
Tim

Posted by AREM [send private reply] at March 28, 2002, 10:19:26 PM

Use the function strcpy() and strcat() they can be found if you include string.h

Posted by buzgub [send private reply] at March 28, 2002, 11:53:15 PM

how about a 3-element array of char pointers? assign the first char* to the start of the string, go through the string until you find the first asterisk, set it to '\0', set the second char* to be equal to the location of the first asterisk (now a null character) + 1, go through until you get to the second asterisk. Repeat.

Posted by gian [send private reply] at March 29, 2002, 03:26:04 AM

Or, you could use the strtok function, found in string.h, which would be quite well suited to this.

Posted by Psion [send private reply] at March 29, 2002, 07:04:21 AM

I can do it in C using nothing but ANSI C standard library calls, with no control constructs or operators....

Posted by manoj [send private reply] at March 29, 2002, 10:45:09 AM

Why don't u take the input char by char(getchar(),getch() etc) and store them in 3 structs(arrays,linked list whatever)? If u just want to display the result like that, just put an endline when u get to '*' in a for loop. Ofcourse, strcpy() etc r very handy if u r comfertable with them.

Posted by RedX [send private reply] at March 29, 2002, 11:59:34 AM

Instead of an char array you could use the string class. Using those standard classes is a good habit (makes programmes more readable and more stable).
That is if you don't have the same teacher as Codered, because then you 'll be yelled at. &;0)

RedX

Posted by Psion [send private reply] at March 29, 2002, 12:31:12 PM

Am I the only one reading this thread who knows how to do this in ANSI C using no program logic but two standard library calls? =)

Posted by gian [send private reply] at March 29, 2002, 01:58:58 PM

Psion, if I was as masterful as you, then I wouldn't be replying to this empty-handed.

Posted by manoj [send private reply] at March 29, 2002, 03:12:32 PM

Well Psion, library calls r all powerful but a guy at this stage should learn 2 use logic in such simple progs to sharpen programming skills.

Posted by Psion [send private reply] at March 29, 2002, 05:00:09 PM

Might as well give it away!

#include <stdio.h>

main()
{
char a[SIZE], b[SIZE], c[SIZE];

scanf("%[^*]*%[^*]*%[^\n]", a, b, c);
printf("%s\n%s\n%s\n", a, b, c);
}

This code is vulnerable to buffer overflow attacks, but that is also easy to fix. =)

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.