Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Beginner
#include<iostream>
#include<cctype>
using namespace std;
char C[56] , cF[16], cM[16], cL[24];
void combine (char a[] ,char b[] ,char c[]);
 
void main ()
{
    int initial=0, final, j, index=0;
    cout<<"Enter your complete name seperated by spaces \n";
    cin.get(C , 55);
    final= initial;
    while (C[final]!= ' ')
    {
        final++;
    }
    for (j=initial; j<final ; j++)
    {
        cF[index] = C[j];
        index++;
    }
    initial= final + 1;
    index=0;
 
    final= initial;
    while (C[final]!= ' ')
    {
        final++;
    }
    for (j=initial; j<final ; j++)
    {
        cM[index] = C[j];
        index++;
    }
    initial= final + 1;
    index=0;
 
    final= initial;
    while (C[final]!= ' ')
    {
        final++;
    }
    for (j=initial; j<final ; j++)
    {
        cL[index] = C[j];
        index++;
    }
    combine (cF,cM ,cL);
 

}
 
void combine (char a[] , char b[] , char c[])
{
    int i;
    a[0]= toupper (a[0]);
    b[0]= toupper (b[0]);
    c[0]= toupper (c[0]);
    cout<<a[0]<<". "<<b[0]<<". ";
    for (i=0 ; i<strlen(c); i++)
        cout<<c[i];
    cout<<endl<<endl;
}
Posted 1-May-10 14:36pm
7mesho428
Edited 1-May-10 22:58pm
Moak11.4K
v4

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

First of all a few problems:
1. For strlen to work, you should include string.h or cstring depending on your compiler.
2. Your method declaration for combine should match at both places (return type of function should be void at last).
 
Now, the actually error, we call it Copy-Paste Error. In the last final loop you are iterating your array till you find a space, which is not necessary in each case. Rather you should look for a End of String '\0'. So change your last while loop to:
while (C[final]!= '\0')
{
    final++;
}
 
It works for me after doing this.
If input is: Manoj Kumar Singh
Output is: M. K. Singh
 
I hope this is what you expected.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Afzaal Ahmad Zeeshan 256
1 OriginalGriff 251
2 BillWoodruff 240
3 Maciej Los 230
4 Sergey Alexandrovich Kryukov 195
0 OriginalGriff 6,419
1 Sergey Alexandrovich Kryukov 6,008
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,070


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 2 May 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100