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'

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include<iostream>
#include<string>
#include<math.h>
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"<<endl;
                    cout<<endl;
                }
			}

			if(choice == 'd' || choice == 'B')
				decToBin();
		cont = getContinue();
	  }
	while(cont == 'y' || cont == 'Y');
}



void intro()
{ 
	cout << "This program coverts decimal numbers to binary and vice versa."<<endl;
}

bool isBinary( string bin )
{
   int i=0;
   bool binIsBinary = true;
   
   while (i < bin.length())
   {
      if( bin.at(i) != '1' && bin.at(i) != '0' )
          {
          binIsBinary = false;
          }
          i++;
   }

   return binIsBinary;
}

void decToBin()
{
   int dec;
   string bin;

   cout << endl << "Please enter a decimal number:"; 
   cin  >>  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<len; i++)
	   if (bin.at(i) == '1')
		   deci = deci + pow(2, len-i-1);

   cout << "The equivalent decimal number is: " << deci << endl    
        << endl;
}

char getChoice()
{
	char choice;
	
	cout << endl << "If you would like to convert a binary to a decimal then enter b."<<endl; 
	cout <<	"If you would like to convert a decimal to a binary then enter d. ";
	cin >> 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)
1
2
main: binToDec(bin);
function implementation: void binToDec(string bin)


SO change your prototype.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<string>

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).
1
2
3
4
5
6
7
8
9
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.