function fun

Basically I wanted it to:
create a new function
then take 2 strings of text
then add them together

where did I go wrong?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// default values in functions
#include <iostream>
using namespace std;

void add (char a="", char b="")
{
    cout << "a + b = ";
    cout << a+b ;
    cout << " Success";
  return (0);
}

int main ()
{
  add("the","1");
  cin.get();
  return 0;
}
Last edited on
A char is only one character, as in "a" or "t". What you need is to use string instead.

Try this:

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

using namespace std;

void add (string a, string b)
{
    cout << "a + b = ";
    cout << a+b ;
    cout << " Success";
  return (0);
}

int main ()
{
  add("the","1");
  cin.get();
  return 0;
}


Thank you for answering my question declaring the function through "void" didn't work but I changed it to "int" and it worked with you string modification thank you

just for fun:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>
#include <iostream>

using namespace std;

int add (string a, string b)
{
    cout << "a + b = ";
    cout << a+b ;
    cout << " Success";
  return (0);
}

int main ()
{
  add("the","1");
  cin.get();
  return 0;
}
and here was my intention from the start:
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 <string>
#include <iostream>

using namespace std;

int emailFriend (string buddy)
{
    cout << "Hello, ";
    cout << buddy ;
    cout << " hows it going today?" << endl;
  return (0);
}

int main ()
{
  emailFriend("Bob");
  emailFriend("Lisa");
  emailFriend("Carl");
  emailFriend("Phil");
  emailFriend("... you");
  cin.get();
  return 0;
}
If you don't need functions to return anything, make their return type void:
1
2
3
4
5
6
7
8
9
void emailFriend (string buddy)
{
    cout << "Hello, ";
    cout << buddy ;
    cout << " hows it going today?" << endl;
    //Note: For a void function, there's no need for return just before the closing brace.
    //It doesn't hurt, though.
    return;
}

This doesn't apply to main(), which should always return int.
Topic archived. No new replies allowed.