+ 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>
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;
}
+ 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";
}
+ 3