Find longest name, display it and say how many letters it have

Hi I am trying to make a program into which you writre for example ten Names or words or etc. and it would find the longest name/word write it and the numbers of characters too but I have problem with displaying that name.
Here is me sourcecode, the problem is here "vitaz=szInput;" i don't know how to save that longest name/word.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdlib>
#include <iostream>
#include <string.h>

using namespace std;

int main ()
{
  char a;
  char szInput[256],vitaz[256];
  for(int i=1; i<=10; i++)
  {
cout<<"Zadaj meno"<<endl;
  cin>>szInput;
  if (strlen(szInput)>a) 
    {
    a=strlen(szInput);
    vitaz=szInput; 
    } 
  }    
cout<<"Najdlsie meno "<<vitaz<<" ma znakov: "<<a<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}
Last edited on
?
Kak3n wrote:
i don't know how to save that longest name/word
reference wrote:
Copies the first num characters of source to destination
That is the function you want to use to actually copy word to vitaz
tnx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <cstdlib>
#include <iostream>
//#include <string.h>
#include <cstring>
#include <iomanip> // setw

using namespace std;

int main ()
{
    //char a;
    int a = 0 ; // int, not char. initialized to zero

    const int SZ = 256 ;
    // char szInput[256],vitaz[256];
    char szInput[SZ],vitaz[SZ]; // avoid magic numbers

    for(int i=1; i<=10; i++)
    {
        cout << "Zadaj meno: " ;

        //cin>>szInput;
        cin >> setw(SZ) >> szInput ; // limit to max SZ-1 characters

        const int len = strlen(szInput) ;
        if ( len > a )
        {
            // a=strlen(szInput);
            a = len ;

            //vitaz=szInput;

            // std::strcpy() http://www.cplusplus.com/reference/cstring/strcpy/
            // this is the function that you should use to copy c-style strings
            strcpy( vitaz, szInput ) ;
        }
    }

    cout << "Najdlsie meno " << vitaz << " ma znakov: " << a << /*endl*/ '\n' ;

    // system("PAUSE");
    // return EXIT_SUCCESS;
}
Topic archived. No new replies allowed.