functions


hello, does anybody know why I get error on line 14, it is about the left operand or something.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

using namespace std;

int longi();

int main()
{
    string name;
    cout<<"Hello sir, what is your name?\n";
    cin>>name;
    cout<<"Your name is '"<< name << "'\n";
    longi()=name;          // I get the error here
    return 0;
}


int longi(){
    string name;
        cout<<"your name is "<<name.size()<<" letters long";
}
What are you trying to do with line 14?
to make the variable name from the function longi() equal to the variable name from the main function
It sounds like you'd want to make longi() take a string parameter so you can send the name entered in main to the function. If you're doing the output in the function, it doesn't need to return anything, so you can make the return type void.

1
2
3
4
void longi(string name)
{
cout << "your name is " << name.size() << " letters long";
}


Then you'd call longi(name); on line 14.
Last edited on
I've tried but it now said me that there are too many arguments

oh, also I am using code::blocks, which I think is the problem, because it works just fine in VisualStudio2013
Last edited on
Make sure the prototype at line 6 matches the function if you changed tyes, # of parameters.
like that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

using namespace std;

int longi(name);

int main()
{
    string name;
    cout<<"Hello sir, what is your name?\n";
    cin>>name;
    cout<<"Your name is '"<< name << "'\n";
    longi(name)=name;
    return 0;
}


void longi(string name){
    string name;
        cout<<"your name is "<<name.size()<<" letters long";
}
You don't need the variable name in the prototype, but you do need the data types to match. (edit - in both the return type and the types of the parameters)

int longi(name);
void longi(string name)


Just call the function. Don't try to make it equal to anything. It's not returning a valid lvalue.
longi(name)=name;

line 20 - you don't need to declare a new name variable in the function.
Last edited on
I did just like you told me, but I have now other errors:

error: 'void longi(std::string)' redeclared as different kind of symbol | line 6
error: previous declaration of 'int longi' | line 14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

using namespace std;

void longi(string name);

int main()
{
    string name;
    cout<<"Hello sir, what is your name?\n";
    cin>>name;
    cout<<"Your name is '"<< name << "'\n";
    longi(name);
    return 0;
}

void longi(string name)
{
    cout<<"your name is "<<name.size()<<" letters long";
}


Hello sir, what is your name? 
Mary 
Your name is 'Mary' 
your name is 4 letters long 
I see now. Thank you very much!!!
Topic archived. No new replies allowed.