Error message: invalid types 'bool...' (array problem)


 
Thread Tools Search this Thread
Top Forums Programming Error message: invalid types 'bool...' (array problem)
# 1  
Old 10-08-2011
Data Error message: invalid types 'bool...' (array problem)

Hello everyone.

I'm stuck with an error message that neither I nor any of my computer science peeps can understand. The program I wrote is meant to be a simple decimal to binary converter, but with this message it's more complicated than I thought.

Here's the code:
Code:
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	int num = 0;
	double k = 0;
	int max = 0;
	bool binary[100];
	cout << "Enter a decimal number: " ; 
		cin >> num;
		
	while (pow(2,k) <= num)
		k++;
		
	k--;				// Decrements the power after the highest power of 2 is found.	  
	max = k;
	
	while (k >= 0 && num > 0)
	{
		if (pow(2,k) > num)
			binary[k] = 0;
		else
		{
			binary[k] = 1;
			num -= pow(2,k);
		}
		k--;
	}
	
	while (max >= 0)
	{
		cout << binary[max];
		max++;
	}
	
return 0;
}

The error exactly says:
...\dectobin.cpp:27: error: invalid types 'bool [100][double]' for array subscript
...\dectobin.cpp:30: error: invalid types 'bool [100][double]' for array subscript

If it helps, I'm using Quincy... but only because my lecturer is using Quincy.

Any help is appreciated greatly.

PS, is there any reason why the variable k can't be of type int?
# 2  
Old 10-08-2011
There's no such thing as array[3.14159]. It has to be a whole integer. So floating point numbers as array indexes make no sense.

You could make k an integer, and it'd spew warnings, but it'd work. Using pow() is severe overkill, though -- your numbers are already binary. Just use binary operators to get at the bits in num instead of converting to float, doing exponents, and converting back.

Code:
#include <iostream>
#include <cmath>
using namespace std;

int main(void)
{
	unsigned int num, q;
	cout << "Enter a decimal number: " ; 
		cin >> num;

        for(q=31; q >= 0; q--)
        {
                // Shift the number q bits to the right, then check if bit 1 is set.
                if((num >> q) & 1) cout << "1";
                else                     cout << "0";
        }

        return(0);
}

# 3  
Old 10-08-2011
Quote:
Originally Posted by Corona688
There's no such thing as array[3.14159]. It has to be a whole integer. So floating point numbers as array indexes make no sense.

You could make k an integer, and it'd spew warnings, but it'd work. Using pow() is severe overkill, though -- your numbers are already binary. Just use binary operators to get at the bits in num instead of converting to float, doing exponents, and converting back.

Code:
#include <iostream>
#include <cmath>
using namespace std;

int main(void)
{
	unsigned int num, q;
	cout << "Enter a decimal number: " ; 
		cin >> num;

        for(q=31; q >= 0; q--)
        {
                // Shift the number q bits to the right, then check if bit 1 is set.
                if((num >> q) & 1) cout << "1";
                else                     cout << "0";
        }

        return(0);
}

Although I would very much like to do this, the binary equivalent has to be stored within an array... =[

Also, I actually did declare k as type int before, but that produced even more problems for me... it said on lines 14, 22 and 27:
error: call of overloaded 'pow(int, int&); is ambiguous
note: candidates are: double pow(double, double)
# 4  
Old 10-08-2011
Quote:
Originally Posted by qf_woodfox
Although I would very much like to do this, the binary equivalent has to be stored within an array...
This is suddenly sounding like homework. We have a homework forum and special homework rules to be followed, please use them.

You can easily store in an array instead of printing them while using my method. There's still no reason to use pow().
Quote:
Also, I actually did declare k as type int before, but that produced even more problems for me... it said on lines 14, 22 and 27:
error: call of overloaded 'pow(int, int&); is ambiguous
note: candidates are: double pow(double, double)
You might need to typecast things to double to do so. C++ is a little stricter about this than C.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Bool vs char * conversion

I have a problem at make step to install a downloaded package consisted of different programs. In file included from kcdbext.cc:16:0: kcdbext.h: In member function �char* kyotocabinet::IndexDB::get(const char*, size_t, size_t*)’: kcdbext.h:1281:14: error: cannot convert �bool’ to... (3 Replies)
Discussion started by: yifangt
3 Replies

2. Programming

SQL: Alter existing bool after printing

I'm writing a DB to manage books & dvd's for the library. So after they added more books/dvd's, they press the print button and all newly added entries are printed. That is, as it prints all 'printed = false' entries, which (false) is the default value for that field for each new entry. ... (3 Replies)
Discussion started by: sea
3 Replies

3. Shell Programming and Scripting

Invalid null command error

Hi, I have this script which gives me output as Invalid null command set recent_file=`grep '^-.*xlsx$' $FTP_LOG |\ sed -e 's/Jan/1/g' \ -e 's/Feb/2/g' \ -e 's/Mar/3/g' \ -e... (6 Replies)
Discussion started by: juzz4fun
6 Replies

4. Shell Programming and Scripting

Shell Scripting Problem - Invalid Back Reference

Here is the question... Create a new script, sub2, taking three parameters... 1.) the string to be replaced 2.) the string with which to replace it 3.) the name of the file in which to make the substitution ...that treats the string to be replaced as plain text instead of as a regular... (1 Reply)
Discussion started by: johnhisenburg
1 Replies

5. Programming

Error message: invalid types 'bool...' (array problem)

Hello everyone. I'm stuck with an error message that neither I nor any of my computer science peeps can understand. The program I wrote is meant to be a simple decimal to binary converter, but with this message it's more complicated than I thought. Here's the code: #include <iostream>... (2 Replies)
Discussion started by: qf_woodfox
2 Replies

6. Shell Programming and Scripting

useradd: ERROR: invalid syntax

Hello - I have the below script syncToken=None;\ forceDeleteUserHome=true;\ nisPwdDir=/etc;\ mirrorFilesLocation=/etc/connector_mirror_files;\ removeHomeDirContents=true;\ shadow=false;\ connectorPrompt=#;\ nisBuildDirectory=/var/yp;\ PGROUP=nogroup;\ COMMENTS='Comments\\ with\\... (2 Replies)
Discussion started by: manju--
2 Replies

7. Shell Programming and Scripting

01.30 Invalid shell error

Hi, I am getting the error 01.30 Invalid shell error I am running the bash shell script in the korn login shell. I have mentioned the #!/bin/bash statement in the my script but not sure why it is giving this error to me.. (4 Replies)
Discussion started by: mr_harish80
4 Replies

8. Shell Programming and Scripting

Perl script with rsh gets stty invalid message

I have a Perl script, that does a system call with rsh to a remote machine. #!/usr/bin/env perl system ("rsh remote-machine echo 99"); And I get the following: stty: standard input: Invalid argument 99 I've tried replacing the system call with below, but I still get the same stty... (1 Reply)
Discussion started by: slchin
1 Replies

9. Shell Programming and Scripting

Problem with shell script...ORA-00904:invalid identifier

Guys, Please suggest me what's wrong with this script? #!/usr/bin/sh ############################################################################## # Author : Bhagat Singh # # # Date : Nov 13,2006 #... (12 Replies)
Discussion started by: bhagat.singh-j
12 Replies

10. Shell Programming and Scripting

problem with script and syntax error message

Hi I have the following script and have problem debugging the problems. The function of this script is to make sure the entire file is being received (the filesize of a data is not changing after 20 seconds) and start moving the file to another directory. This script should be started every 30mins.... (5 Replies)
Discussion started by: ReV
5 Replies
Login or Register to Ask a Question