Hi,
i have a problem,
i am writing the initials and the surname on one line of code, how do i copy just the initials and leave the surname, and put the initials on another line?
Thanks
Printable View
Hi,
i have a problem,
i am writing the initials and the surname on one line of code, how do i copy just the initials and leave the surname, and put the initials on another line?
Thanks
Do you have the initials and surname in a string? Do you want to split the string?
post the code you're having trouble with.
not got anything yet really. if i was to put it in a string how would i do that and then how would i spilt the string to do what i am wanting it do do?
>> not got anything yet really.
me neither.
>> if i was to put it in a string how would i do that and then how would i spilt the string to do what i am wanting it do do?
why don't you give it a try first and see what you come up with? after all programming *is* about problem solving, right?
> not got anything yet really
Apparently, you have the code which prints both on the same line.
How about you post that, then perhaps we can tell you how to change it.
hi,
this is what i have
it manages to copy the first 3 letter whiich i want it to do, but i also want it to copy the remiaining letters onto another line.Code:char szname[50];
cout << "Please enter your initials and surname: ";
char str2[3];
strncpy (str2,szname, 3);
str2[3] = '\0';
puts (str2);
cout <<endl;
cin.getline(szname, 20);
eg.
user enter - AN Other
Output
AN
Other
can any one help with that?
Not really.Quote:
Originally Posted by peckitt99
1) Move the cin.getline(szname, 20); immediately below the prompt and you have it.
2) Fix definition of str2 -- make it 4, not 3
3) Instead of puts(), use your cout
4) The rest of the string is at the address (&) of the 4th character ([3]) in szname. Put that together and output it.
[edit]And don't bump your threads -- it's not a good thing[/edit]
managed to do most of that but i dont understand 4.
4) The rest of the string is at the address (&) of the 4th character ([3]) in szname. Put that together and output it.
what do you mean?
Thanks for your help.
The first element of an array is [0]. The last element is [elements-1]. So if you have an array declared like this:Quote:
4) The rest of the string is at the address (&) of the 4th character ([3]) in szname. Put that together and output it.
what do you mean?
the addressable elements are [0] to [2]. [3] is out of bounds.Code:int array[3];
If you take the address of array[1], like this:
it's like you have another array that starts at array[1] (of course, it's the same array, occupying the same memory). So if you haveCode:&array[1]
the output will beCode:char s[] = "hello world";
cout << &s[5];
which is what you want (&szname[3]).Code:world
Hi,
Got it working thanks for all your help much appreciated!!
Thanks!!!