-6

I'm trying to pass two local variables to another block, but when compiling my code, this error shows:

(error 93: ) expected)

I am using Borland Turbo C++ 3.0. I can't figure out where I am going wrong; could someone point me in the right direction?

char create_username(char forename,surname)
{      
     strcpy(forename);
     strcpy(surname);

     strcat(forename,surname);

     return username;

}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Moto28
  • 45
  • 9

4 Answers4

1

Even Turbo C++ implemented this in a correct way decades ago.

Function parameters need to be declared giving their types explicitly:

char create_username(char forename, char surname)
                                 // ^^^^ This is needed!

in contrast to declaring a bunch of local variables, where the multiple definition syntax can be used:

void foo() {
    char forename, surname;
}

Also note that char only stores a single character, and you're not able to store a forename/surname.

The type rather should be std::string.


As you're clarifying your requirements in this comment

I am trying to make a username by joining using the first letter of the forename and the full surname, ...

your function should rather look like:

std::string create_username(const std::string& forename, const std::string& surname) {      
     return std::string(forename[0],1) + surname;
}
Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • am trying to make a username by joining using the first letter of the forename and the full surname, thanks so much for you're help – Moto28 Oct 29 '15 at 18:47
0

Literally everything is wrong.

char create_username(char forename,surname)

This returns a character? And forename and surname are each characters? That can't be right.

 strcpy(forename);

Since forename is a character, not a string, it can't be passed to strcpy. But also -- copy to where?

 strcat(forename,surname);

Why did you make a copy of forename and surname if you were just going to pass the original ones to some other function? And what do you think this does? Do you think it concatenates both forename and surname onto ... what?

 return username;

Where did username come from?

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
-2

Here you are.

#include <iostream>
#include <cstring>

char * create_username( const char *forename, const char *surname )
{
    size_t n = std::strlen( forename ) + std::strlen( surname ) + 1;
    char *username = new char [ n + 1]; // one more for the blank between names

    std::strcpy( username, forename );
    std::strcat( username, " " );
    std::strcat( username, surname );

    return username;
}

int main()
{
    const char *forename = "Craig";
    const char *surname = "Cheney";

    char *username = create_username( forename, surname );

    std::cout << username << std::endl;

    delete [] username;
}

The output is

Craig Cheney

If your compiler does not support namespaces and new C++ headers then you can write something like the following

#include <iostream.h>
#include <string.h>

char * create_username( const char *forename, const char *surname )
{
    size_t n = strlen( forename ) + strlen( surname ) + 1;
    char *username = new char [ n + 1]; // one more for the blank between names

    strcpy( username, forename );
    strcat( username, " " );
    strcat( username, surname );

    return username;
}

int main()
{
    const char *forename = "Craig";
    const char *surname = "Cheney";

    char *username = create_username( forename, surname );

    cout << username << endl;

    delete [] username;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
-2

First a bit of a friendly advice -- you should check out the introduction to understand how this website works. Your question is poorly worded, and it will most likely be closed and deleted as unconstructive unless you edit and fix it to comply with site rules. If you can't bother to write proper questions in proper English you will have a hard time finding help.

Second, you have many errors in your code which show that you don't understand the basic concepts of the C/C++ language, and also that you didn't bother to check the C/C++ language syntax. Just doing a Google search for function names you want to use would have shown you how to invoke them properly -- here is an example for strcpy().

If you want to be a good programmer you need to put in a bit more effort into learning new things.

Community
  • 1
  • 1
Igor Levicki
  • 1,017
  • 10
  • 17