### Checking for Invalid Binary Inputs

I'm having a problem checking for invalid binary inputs from the user. Here is the program. Can anyone tell me how to approach this?

These are the errors I'm getting

error C2660: 'binToDec' : function does not take 1 arguments
warning C4018: '<' : signed/unsigned mismatch
error C2082: redefinition of formal parameter 'bin'
error C2275: 'std::string' : illegal use of this type as an expression
see declaration of 'std::string'
error C2082: redefinition of formal parameter 'bin'

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135`` ``````#include #include #include using namespace std; void intro(); bool isBinary(string); void decToBin(); string getBin(); void binToDec(); char getChoice(); char getContinue(); int main() { char choice, cont; string bin; intro(); do{ choice = getChoice(); if(choice == 'b' || choice == 'B') { bin = getBin(); bool binIsBinary = isBinary(bin); if(binIsBinary) binToDec(bin); else { cout<<"Error!!! Your Number is Not Binary"<> dec; bin = ""; while (dec != 0) { if (dec % 2 == 0) bin.insert(0, "0"); else bin.insert(0, "1"); dec = dec / 2; } cout << "The equivalent binary number is: " << bin << endl << endl; } string getBin(string bin) { string bin; cout << endl << "Enter a binary number: "; cin >> bin; return string; } void binToDec(string bin) { double deci; string bin; double len; len = bin.length(); deci = 0; for (int i=0; i> choice; return choice; } char getContinue() { char cont; cout << "Would you like to convert another number(Y/N)? "; cin >> cont; return cont; }``````
Last edited on
error C2660: 'binToDec' : function does not take 1 arguments

in the function prototype you declared function binToDec as
`void binToDec();` without any parameters

but in main and function implementation you declared it with paramter(string type)
 ``12`` ``````main: binToDec(bin); function implementation: void binToDec(string bin)``````

 ``1234567891011121314151617181920`` ``````#include #include using namespace std; void binToDec(); // A. declaration of function prototype int main() { string bin; binToDec(bin); // B. call the function return 0; } void binToDec(string bin) // C. definition of function { // The actual code for the function goes here }``````

Both A and C must have the same return type (void in this case), all three of A, B and C must have the same number and type of parameters. Here B and C agree, but declaration A specifies the function takes no parameters at all.

Below, the function string getBin() is receiving a string as an input parameter, and then immediately attempts to define another, different string with the same name. In this case the answer is to remove the input parameter. The return statement should give the name of the variable (bin) but instead gives its type (string).
 ``123456789`` ``````getBin(string bin) { string bin; cout << endl << "Enter a binary number: "; cin >> bin; return string; }``````

Function binToDec() has a similar problem with a parameter passed to the function followed by a different variable with the same name. In this case the answer is to keep the input parameter, but remove the extra variable.
Last edited on
Topic archived. No new replies allowed.