Program skipping crucial loop- cannot figure out why

I am currently writing a program that converts a base-10 number into base-2. Unfortunately, the program is only returning "0" when a number is set to be converted. I have established "checkpoints" to figure out where the program is functioning improperly, and it appears that I may have erred in line 9 or mistyped. Unfortunately, I cannot find my error. Could one of you guys see where I went wrong?

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
#include <iostream>
using namespace std;
int orig_number;
int digitcount;
int i;

void check_number() { //Figures the amount of digits needed to render orig_number in binary
	cout << "entering check_number... ";  //checkpoint for the calling of check_number
	cout << "You entered " << orig_number << "... ";
	for(i = 0; i++;) {
		cout << "Entering check_number for() loop... ";
		cout << "for the " << i << "time..."; //attempting to see how many times the for() loop runs, but it hasn't been returning the variable at all.
		while((orig_number / (2^i)) < 1) {
			digitcount = i;
			break;
		}
	}
}

void print_number() {  //supposed to print 0 if the number entered was 0, and otherwise print orig_number in base-2.
	int temp_number = orig_number;
	if(digitcount == 0) {
		cout << "just printing 0!... ";  //original checkpoint to see if the program was just entering the if() loop every time it ran
		cout << 0;
	}
	while(digitcount > 0) {
		cout << (temp_number / (2 ^ digitcount));
		temp_number = temp_number - (2 ^ digitcount);
		digitcount--;
	}
}

int main() {  //"Meat" of the program, where the two functions that solve the number are called.
	cout << "VERSION 1! \n";
	cout << "Enter a number: ";
	cin >> orig_number;
	check_number();
	print_number();
	system("PAUSE");
	return 0;
}
Found the problem: the format of the for() was improper in line 10.
 
for(i = 0; ; i++)

is the new format. Unfortunately, the loop appears to repeat forever. Could anyone guide me as to why the while() loop is not assigning digitcount a value and breaking the loop?
First whats with all the global variables?
int orig_number;
int digitcount;
int i;
I don't recommend using global variables unless completely necessary. Second you should look up the syntax for a for loop, but Im not completely convinced you need a for loop in this instance."For loop syntax below..".Also where you have a variable (orig_number) that is taking in a value outside of the scope of your function yet your not sending that variable in as a parameter. You have a large number of errors, not one or two, for instance every thing looks like it would print out as one line... Do you not want to separate the content? For example if you want to start the next statement on a second line you would do things like \n or endl; at the end of your previous cout statement. I mean no offense but you need to do some serious reading and practice. Im surprised your playing with functions if you don't know the syntax for input/output and simple loops like for loops you should step away from the functions until you know everything about cin and cout and some of the loops. Another recommendation is putting your function definitions after the main function and having function prototypes before the main function. Dude just read and read and read some more. Plus practice.

In general the for loop syntax:

for (initialize loop control variable(lcv); loop condition; (lcv) update){
statement.....
}
In other words:
for (int i = 0; i < 10 (the number of times you want it to iterate); i++){...}
Last edited on
I understand what you are saying, and I would like to state that the for() loop issue was a glimpsed over typing error. What I have changed so far:
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
#include <iostream>
using namespace std;
int digitcount;
void check_number(int number);
void print_number(int temp_number, int printed_digitcount);

int main() {  
	int orig_number;
	cout << "Enter a number: ";
	cin >> orig_number;
	check_number(orig_number);
	print_number(orig_number, digitcount);
	system("PAUSE");
	return 0;
}

void check_number(int number) {
	cout << "entering check_number ...\n ";
	cout << "You entered " << number << " ...\n ";
	for (int i = 0;; i++) {
		cout << "Entering check_number for() loop ...\n ";
		cout << "for the " << i << "time ...\n ";
		while ((number / (2 ^ i)) < 1) {
			digitcount = i;
			break;
		}
	}
}

void print_number(int temp_number, int printed_digitcount) {
	if (printed_digitcount == 0) {
		cout << "just printing 0! ...\n ";
		cout << 0;
	}
	while (printed_digitcount > 0) {
		cout << (temp_number / (2 ^ printed_digitcount));
		temp_number = temp_number - (2 ^ printed_digitcount);
		printed_digitcount--;
	}
}

Could you help me identify why the for() loop is not being exited once the conditions of the while() loop are met? Could you also help me find a way to write this in which digitcount does not have to be a global variable?
Last edited on
Just returned check_number() as an integer, treating it as the digitcount, as the whole purpose of the function was to define digitcount. One less issue with style, but the issue of functionality is still present.
Last edited on
You have no condition testing whether or not to continue.

remember:

for (int i = 0; i < 10 (the number of times you want it to loop); i++){...}

Note that where I have i < 10, that 10 could be a variable determined by the user or by something in the program such as length of string.

Without a loop condition you have an endless loop. The loop condition is like an if statement in the for loop. In the for loop: first the program reads to initialize a variable, what is known as the loop control variable. In your program its ( i ). Then it checks to see if the LCV is with in the condition or conditions of the loop condition, like I have here i < 10. Then the program will run and when its done running it comes back up to the LCV update and updates by how you have it set to up date. LCV + 1, will add 1 to the LCV. After the update it checks the loop condition and if it meets the requirements it continues. It will do this until the loop condition is met.
Also it looks much cleaner, good job.
Last edited on
Another thing in your first program I see your wanting to show the user the number of times the loop iterates (loops). Keep in mind in programming we start from zero not one so it will read 0, 1, 2, ....

Get rid of the while loop and put this, (number / (2 ^ i) < 1, in the loop condition for the for loop. You don't seem to need the second loop or you could get rid of the for loop all together and use a do while loop.
http://www.cplusplus.com/doc/tutorial/control/
Not completely sure that your arithmetic syntax is correct. Im some what new to C++ but I've never seen 2^i, I think your attempting to, say i was equal to 2 your trying to say 2 square? I don't know if that will work never used ^ before, but I might be wrong. Also I don't think your need the break, I don't understand why its there.
Also you can't take digitcount out of a void function it would be out of scope. You would need a value returning function.

http://www.cplusplus.com/doc/tutorial/functions/
The value return function would need to be in the same data type as the variable your returning.


You really need to read about loops, your using while loops all wrong. Your second function should be changed as well. Read about loops and see what you come up with and we'll go from there.

In general you have two problems effecting your program how your using loops and how your using functions.
The functions aren't bad but if you want to take a value out of a function it shouldn't be a void function.
Last edited on
Unfortunately, 2 ^ i doesn't do what you think it does. You seem to think it raises 2 to the i'th power. It actually is 2 XOR i.

The other big bug is at line 37. If the digit printed is zero, then the subtraction does the wrong thing.

For powers of two, you can actually use the shift operator (<<) since shifting a number left is the same as multiplying by 2.

Taking these together, check_number and print_number become:
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
void
check_number(int number)
{
    cout << "entering check_number ...\n ";
    cout << "You entered " << number << " ...\n ";
    for (int i = 0;; i++) {
	cout << "Entering check_number for() loop ...\n ";
	cout << "for the " << i << "time ...\n ";
	if ((number / (1 << i) < 1 )) {	// "if" instead of "while"
	    digitcount = i;
	    break;
	}
    }
}

void
print_number(int temp_number, int printed_digitcount)
{
    cout << "Print_number " << temp_number << ' ' << printed_digitcount << '\n';
    if (printed_digitcount == 0) {
	cout << "just printing 0! ...\n ";
	cout << 0;
    }
    while (--printed_digitcount >= 0) {
	int digit = (temp_number / (1<<printed_digitcount)); // get the digit
	cout << digit;		// print it
	temp_number = temp_number - (digit << printed_digitcount); // subtract it
    }
    cout << '\n';
}


The functions aren't bad but if you want to take a value out of a function it shouldn't be a void function.

I understand that, I changed the type of function when I gave it a return value.


The other big bug is at line 37. If the digit printed is zero, then the subtraction does the wrong thing.

For powers of two, you can actually use the shift operator (<<) since shifting a number left is the same as multiplying by 2.

Thanks for the help! I thought I had read about the 2 ^ i not working before but I couldn't remember where. But won't making the while loop condition
(--printed_digitcount >= 0)
make the program subtract one from printed_digitcount before the actions in the loop begin? Won't that cut off the first digit of the number?
also:

I have revised the structure of check_number so that it represents an int value and returns the value of digitcount instead of breaking.
1
2
3
4
5
6
7
8
9
10
11
int check_number(int number) {
	cout << "entering check_number ...\n ";
	cout << "You entered " << number << " ...\n ";
	for (int i = 0;; i++) {
		cout << "Entering check_number for() loop ...\n ";
		cout << "for the " << i+1 << "time ...\n ";
		if ((number/(1<<i)) < 1) {
			return i;
		}
	}
}


The project now works and I believe I learned a large amount from what you guys mentioned. Thanks.
Last edited on
Glad I could help.
Topic archived. No new replies allowed.