0

I want help with this code

The problem is in the program itself. https://code.sololearn.com/c5LeZ4c0Lykz/?ref=app

19th Mar 2018, 7:48 AM
Shubh Agrawal
Shubh Agrawal - avatar
5 Respuestas
+ 12
#include <iostream> #include <string> using namespace std; int main() { string str; cout<<"Enter a string: "<<endl; getline(cin, str); for(int i=0;i<str.length();i++) { str.at(i)=(str.at(i)=='0'?'1':'0'); } cout<<str; return 0; } //Now U will be thinking U have seen it somewhere //well everything is near U , U just need to think how to use them smartly
19th Mar 2018, 12:22 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 14
how about taking that binary number in a String & then replacing 1 by 0 & 0 by 1 1 more thing 0%10 will be 0 , not infinity //for correcting your code ,just make a case for 0 also using if , as your loop will not work for 0 as input //hope it helps 👍
19th Mar 2018, 7:57 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 1
your code couldn't work for these two reasons: 1) you gave a condition of while (n!=0) which means when you enter zero as your value for n the loop wont execute as the condition evaluates to false 2) the other problem is a bit complex, because when you try to enter value for n as a binary number like 01101 or any other that begins with a 0, cin doesn't read in input in this way as an integer number doesn't begin with a zero unless it's only zero itself so when your binary number is 011 it will only read eleven. I couldn't get around that but I tried this : #include <iostream> using namespace std; int main() { long long n; cout<<"Enter a binary number: "<<endl; cin>>n; int rem=0; string num=""; do { rem = n%10; n=n/10; if(rem==1) { num=num+"0"; } if(rem==0) { num=num+"1"; } } while(n!=0) ; cout<<num; return 0; }
19th Mar 2018, 9:26 AM
MÎDÅŞ
MÎDÅŞ - avatar
+ 1
Gaurav can you please post the code. I tried but i am not getting it correct.
19th Mar 2018, 11:56 AM
Shubh Agrawal
Shubh Agrawal - avatar
+ 1
yeah thanks man👍
19th Mar 2018, 12:23 PM
Shubh Agrawal
Shubh Agrawal - avatar