functions

Nov 30, 2014 at 8:27pm

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";
}
Nov 30, 2014 at 8:29pm
What are you trying to do with line 14?
Nov 30, 2014 at 8:30pm
to make the variable name from the function longi() equal to the variable name from the main function
Nov 30, 2014 at 8:34pm
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 Nov 30, 2014 at 8:35pm
Nov 30, 2014 at 8:43pm
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 Nov 30, 2014 at 8:44pm
Nov 30, 2014 at 8:47pm
Make sure the prototype at line 6 matches the function if you changed tyes, # of parameters.
Nov 30, 2014 at 8:49pm
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";
}
Nov 30, 2014 at 8:52pm
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 Nov 30, 2014 at 8:54pm
Nov 30, 2014 at 8:57pm
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
Nov 30, 2014 at 9:00pm
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 
Nov 30, 2014 at 9:03pm
I see now. Thank you very much!!!
Topic archived. No new replies allowed.