Little problems with functions

Hello, guys. I have little problem with this code, can you help me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <iostream>
using namespace std;
void Balts(string x){
    x="balts";
}
void Melns(string  y){
    y="melns";
}
int main(){
    string x, y;
   int i;
   for(i=0; i<=10; i++){
   cout<<Balts(string x)<<" un "<<Melns(string y);
}
   return 0;
}
it is customary to tell us what the problem is.
but I will take a guess: perhaps you forgot to make the parameters reference, so the functions do not seem to change the strings? Also, string x in the function call is not necessary (and I don't know if its correct or not, actually, havent tried it).

put that together and you get:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

  #include <iostream>
using namespace std;
void Balts(string &x){
    x="balts";
}
void Melns(string  &y){
    y="melns";
}
int main(){
    string x, y;
   int i;
   for(i=0; i<=10; i++){
       cout<<Balts(x)<<" un "<<Melns(y);
}
   return 0;
}
In addition to what jonnin mentioned, in your line 13, you're trying to cout a void (twice). Balts and MeIns are both void functions. i.e. They return nothing.
What are you expecting line 13 to display?

Try this:
 
  cout << x << "un " << y;


A couple of other notes:
- i never gets used in your loop. You're going to display the same values of x and y 11 times.
- Do you realize your loop is going to execute 11 times, not 10 due to the <= condition.
Last edited on
Good finds. lets try that again, with those fixes too.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23


#include <iostream>
using namespace std;
void Balts(string &x)
{
    x="balts";
}
void Melns(string  &y)
{
    y="melns";
}
int main()
{
   string x, y;   
   for(int i=0; i<10; i++) //10 times? 11 times? 
   {
       Balts(x);
       Melns(y);
       cout<<x<<" un "<<y;
   }
   return 0;
}
Thank you guys, I understood where was the biggest problem, i was trying cout void. And there was 10 times, i don't know why I put <=. So, thank you, guys.
It's hard to know how to fix it because it's hard to know what exactly it's supposed to do.

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

string White()
{
    return "white";
}

string Black()
{
    return "black";
}

int main()
{
   string x, y;
   for (int i = 0; i < 5; ++i)
   {
       x += White();
       y += Black();
       cout << x << " and " << y << '\n';
   }
}

white and black
whitewhite and blackblack
whitewhitewhite and blackblackblack
whitewhitewhitewhite and blackblackblackblack
whitewhitewhitewhitewhite and blackblackblackblackblack
Topic archived. No new replies allowed.