+ 4

Please what's wrong with my code?

#include<iostream> #include<string> using namespace std; void swapmessage(string&str1,string,str2); void swapmessage(string&str1,string,str2) { void swapmessages: string temp=str1; str1=str2; str2=temp; } int main(){ string s1="hello "; string s2="good afternoon"; cout<<"Before swapping:"<<s1<<","<<s2<<"\n"; swapmessage(s1,s2); cout<<"After swapping:"<<s1<<","<<s2<<"\n"; return 0; }#include<iostream>

30th Dec 2023, 12:12 PM
Shadan oboodi
4 Respostas
+ 7
// here you can check where are differences: #include<iostream> #include<string> using namespace std; void swapmessage(string &str1, string &str2) { string temp=str1; str1=str2; str2=temp; } int main(){ string s1="hello "; string s2="good afternoon"; cout<<"Before swapping:"<<s1<<","<<s2<<"\n"; swapmessage(s1,s2); cout<<"After swapping:"<<s1<<","<<s2<<"\n"; return 0; }
30th Dec 2023, 1:08 PM
JaScript
JaScript - avatar
+ 4
do you really need to define a function for this? Why not just use swap? I would say the main thing that's wrong, other than obvious syntax errors, is that it is an unnecessary function... #include<iostream> using namespace std; int main(){ string s1="hello "; string s2="good afternoon"; cout<<"Before swapping:\ns1 = "<<s1<<"\ns2 = "<<s2<<"\n"; swap(s1,s2); cout<<"\nAfter swapping:\ns1 = "<<s1<<"\ns2 = "<<s2<<"\n"; }
30th Dec 2023, 12:35 PM
Bob_Li
Bob_Li - avatar
+ 3
Ok i got it thanks [Listen Once🌙] Bob_Li JaScript
2nd Jan 2024, 7:12 PM
Shadan oboodi