Concatenation: what's wrong with this program?

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
#include<iostream>
using namespace std;

char concat(char a, char b);

void main()
{
	char a,b;
	int c;
	cout<<"Enter employ's first name:\n";
	cin>>a;
	cout<<"Enter employ's second name:\n";
	cin>>b;

	cout<<"This program allows "<<concat(a,b)<<" to evaluate their salary\n\n:";

	cout<<"Enter your hourly salary:\n";
	cin>>c;

	cout<<"\nHourly: "<<c<<"\nDaily: "<<24*c<<"\nweekly: "<<24*7*c;
}

char concat(char a, char b);
{
	cout<<a<<" "<<b;
	return a<<" "<<b;
}


The following error occurs when I run the above program:
"error C2447: missing function header (old-style formal list?)"
and it points to line 24.

What's the problem?
there's a ; on line 23, so the line is a function declaration and not a definition, thus { ... } is not a function body.
OH I get it.. Still a problem in line 26!
Right.
There's a problem in your whole code. char is just one character. lines 11 and 13 will only allow lingle char names. Function concat will only return one char (if it was written correctly).
If you want to have strings either use char arrays and deal with a lot of problems that come with them, or use std::string.
Hamster bhai! I am just a beginner. I din't get much of what you said.
One more thing: i used char type with c++ for the first time. May be it's not there at all.
And i'm not familiar with strings either! But thanks for your concern.
closed account (jw6XoG1T)
are u desi by any chance? lol
closed account (jw6XoG1T)
variables of type char can only take one input such as "a" "x" '&' '?' '*' ect.....

u can use strings to take in whole words. to declare it use just like use char:

string x;
cin >> x;
As paki programmer said. Using string is much wiser in this situation. According to the comments in your code you want to concat the first and last name so it's impossible to fit these into a single char as they only take one letter.

Now if you want to use alot of string and combine them you should use the std::stringstream class, which you can add string with the << operator, then using the .str() to get the concatenated string.

You can read more about it here and how to use it -> http://www.cplusplus.com/reference/iostream/stringstream/
Yup! Frm Pak!! :)
closed account (jw6XoG1T)
me too lol =) except i live in america
I'm also a beginner. Try if this code work...
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
#include<iostream>
#include <string.h>
using namespace std;

string concat(string a, string b);

int main()
{
	string a,b;
	int c;
	cout<<"Enter employ's first name:\n";
	cin>>a;
	cout<<"Enter employ's second name:\n";
	cin>>b;

	cout<<"This program allows ";
	concat(a,b);
	cout<<" to evaluate their salary\n\n:";

	cout<<"Enter your hourly salary:\n";
	cin>>c;

	cout<<"\nHourly: "<<c<<"\nDaily: "<<24*c<<"\nweekly: "<<24*7*c;
}

string concat(string a, string b)
{
    cout<<a<<" "<<b;
    return (a,b);
}
Close try mainframe, but <string.h> is a C header for Cstring functions. To work with std::string lose the .h

Try this code:
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
#include <string>
#include <iostream>
using namespace std;

int main()
{
	string a,b;
	int c;
	cout << "Enter employ's first name:\n";
	cin >> a;
	cout << "Enter employ's second name:\n";
	cin >> b;

	cout << "This program allows " << concat(a, b) << " to evaluate their salary\n\n:";

	cout << "Enter your hourly salary:\n";
	cin >> c;

	cout << "\nHourly: " << c << "\nDaily: "<< 24 * c << "\nweekly: " << 24 * 7 * c;
}

string concat(string a, string b)
{
	string fusion = a + " " + b;
	return fusion;
}
Last edited on
1
2
3
4
5
string concat(string a, string b)
{
    cout<<a<<" "<<b;
    return (a,b);
}


In general try using the reference in your parameter to avoid unnecessary copying of the source, as the
the original method above will create a copy of both string a and b.

1
2
3
4
5
string concat(string & a, string & b)
{
    cout<<a<<" "<<b;
    return (a,b); // Can you actually write like this?  
}

I don't think you can. I thought that mainframe intended a std::pair here, but the function should return a string...
I believe the return (a, b); is undefined behavior as there is no overload for the comma in std::string
Thanks wolfgang :) I understood!
Moreover, my concatenation process was wrong. I got it now. Thanks again.
IIRC the comma operator is not overloadable at all, the default result value will be the last operation (so it will return b)
Last edited on
Topic archived. No new replies allowed.