Please help to correct my program

#include<iostream>
#include<cmath>
#include<cstdlib>

using namespace std;

int main()
{
int binary;
int rem;
int MyEnd;
int i;
int c;
int f;
string bin;
int num;

cout<<"Please enter the binary number"<<endl;
cin>>binary;
cout<<"Please re-enter the binary number"<<endl;
cin>>bin;

for (num=binary; num<=0; num--)
num/=10;
rem=num%10;
for (i=0; i>=0 && i<bin.size(); i++)
if (rem==1 || rem==0)
c=rem+(2^i);
c=c + 0;
cout<<"The binary that converted into decimal is"<<c<<endl;


cout<<"Press any integer number to continue"<<endl;
cin>>MyEnd;

return 0;}


The program can be executed but the fragments of code is wrong especially the calculation part. By the way, it is about to convert the binary to decimal. Please helpppp
First, you need to better format your code. That's all I've done here, I haven't changed anything. Read the comments.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cmath>
#include <cstdlib> // this should be #include <string>

using namespace std;

int main ( )
{
    // more descriptive variable names would be good. f is never used.
    int binary, rem, MyEnd, i, c, f, num;

    string bin;


    cout << "Please enter the binary number\n";

    cin >> binary;
    // It isn't necessary to enter the number twice.
    cout << "Please re-enter the binary number\n";
    // strings can be assigned int values
    // like this: bin = binary;
    cin >> bin;


    for ( num = binary; num <= 0; --num )
        num /= 10;

    rem = num % 10;


    // If you set i to 0, and never decrement it, it will never be negative.
    // There's no reason to check for that.
    for ( i = 0; i >= 0 && i < bin.size ( ); ++i )
        if ( !rem || rem == 1 )
            c = rem + ( 2^i ); // 2^i does not do what you think it does.
    // To take a number to a power, you have to use the pow function.
    // Like this: pow ( 2, i );


    c = c + 0; // this line does nothing


    cout << "The binary that converted into decimal is" << c << '\n';

    // There is a better way to do this without using a variable.
    cout << "Press any integer number to continue\n";
    // Use cin.ignore ( );
    cin >> MyEnd;
}


ps, use code tags: http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
but it says pow cannot be used as functionn
What does?
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<string>

using namespace std;

int main()
{
unsigned int binary;
unsigned int rem;
unsigned int MyEnd;
float i;
unsigned int c;
string bin;
unsigned int num;
double pow;

bin=binary;
cout<<"Please enter the binary number"<<endl;
cin>>binary;

for (num=binary; num<=0; num--)
num/=10;
rem=num%10;
for (i=0; i>=0 && i<bin.size(); i++)
if (rem==1 || rem==0)
c=rem + pow((double)2,i);
cout<<"The binary that converted into decimal is"<<c<<endl;


cout<<"Press any integer number to continue"<<endl;
cin>>MyEnd;

return 0;}


#include<iostream>
#include<cmath>
#include<cstdlib>
#include<string>

using namespace std;

int main()
{
unsigned int binary;
unsigned int rem;
unsigned int MyEnd;
float i;
unsigned int c;
string bin;
unsigned int num;
double pow;

bin=binary;
cout<<"Please enter the binary number"<<endl;
cin>>binary;

for (num=binary; num<=0; num--)
num/=10;
rem=num%10;
for (i=0; i>=0 && i<bin.size(); i++)
if (rem==1 || rem==0)
c=rem + pow((double)2,i);
cout<<"The binary that converted into decimal is"<<c<<endl;


cout<<"Press any integer number to continue"<<endl;
cin>>MyEnd;

return 0;}


It says, the function do not receive the function pow.
the function do not receive the function pow

That message makes no sense. Please quote the EXACT error message.
You're probably going to get a warning on the following line that you're trying to assign a double to an int.
c=rem + pow((double)2,i);

Pay attention to what C or C++ version you're using.
pow with a double and a a float is only supported in C++11, although C++98 should convert the float to a double. C does not support a double and an float as arguments.
http://www.cplusplus.com/reference/cmath/pow/

YOU HAVE BEEN ASKED TO USE CODE TAGS. PLEASE DO SO.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post and apply code tags to it.




Last edited on
@OP: when you say `pow' you are referring to the variable that you declared with double pow;
So `(the variable) pow (of type double) cannot be used as function'

To refer to the function use std::pow (or don't go colliding names and remove the variable declaration, that you are not using at all)
Therefore if I wanted to convert the binary to decimal, the symbol caret(^) cannot be used?
^ is the bitwise exclusive or (XOR) operator, not an exponentiation operator.
http://www.cplusplus.com/doc/tutorial/operators/

@ne555
Good catch on the variable pow. I missed that.
Topic archived. No new replies allowed.