New and confused

Pages: 12
I have posted this code multiple times and haven't gotten an answer that works with my code usually I am told to just remake it in a completely different way. I was wondering if anyone knew how to fix my code as it is to work in the way it is supposed to. The two main problems I am having is that I can't get "bozz" to output no matter what I do. Also It Bizz and Buzz will output correctly based on their rules... However, Bizz will also Output on numbers like 60 that include a 6 and are also divisible by 5. I am brand new to coding with C++ and have no idea what I am doing but I would really appreciate if you were to work with the way I am coding the project and don't ask to remake the code completely as I have done so multiple times with the same result. Thanks for any help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
int main()
{
	int i;
	cout << "Enter a number\nNumber: ";
	cin >> i;
	scanf_s("%d", &i);
	{
		if ((i % 5 == 0) && ((i / 100) != 6) || (i % 5 == 0) && ((i / 10) % 10 != 6) || (i % 5 == 0) && ((i % 10) != 6))
			cout << "Output: bizz" << endl;			 // bizz is divisible by 5 and doesn't have a 6
		else if ((i % 5 != 0) && ((i / 100) == 6) || (i % 5 != 0) && ((i / 10) % 10 == 6) || (i % 5 != 0) && ((i % 10) == 6))
			cout << "Output: buzz" << endl;		 // buzz is not divisible by 5 however it does have a 6
		else if ((i % 5 == 0) && ((i / 100) == 6) || (i % 5 == 0) && ((i / 10) % 10 == 6) || (i % 5 == 0) && ((i % 10) == 6))
			cout << "Output: bozz" << endl;  // bozz is divisible by 5 and has a 6
		else cout << i << endl;				 // i is neither divisible by 5 nor does it have a 6
	}
	return 0;
}

Ok so I added those changes you mentioned, however, now when I get to the debug it makes me enter a number 2ce before it gives me an output and it still won't follow my 4 rules err
Last edited on
The first thing I noticed is the first thing Shell pointed out - your use of && and || without parentheses is definitely confusing, and I'm not entirely certain what is your INTENDED result.

Also, I'm most unfamiliar with the calls you're using. I recognize printf() as an old C version of cout, but I don't know where you're given a chance for input, since I just see some function with no return type that takes what appears to be a string and reference to an unknown type i, so now I'm also confused and would like to see how this is resolved.
Well scanf lets me enter in an input being i to the debug and then the cout is what is given as an output as a result to the if statements while printf is just the the prompt to the person inputting a number to the debug as to what they are supposed to do. (Again I have no idea what I am doing)
Basically. I just need to make a program that allow the user to input any number to the debug while following these 4 basic rules.

bizz is divisible by 5 and doesn't have a 6
buzz is not divisible by 5 however it does have a 6
bozz is divisible by 5 and has a 6
i is neither divisible by 5 nor does it have a 6

So when a rule is met the output should be of that rule. For example. 60 has a 6 in it and is divisible by 5 so this number when input to the debug should display an output of "Bozz". But I am too stupid to understand how to make this simple process work sorry. =(
Well scanf lets me enter in an input being i to the debug and then the cout is what is given as an output as a result to the if statements while printf is just the the prompt to the person inputting a number to the debug as to what they are supposed to do. (Again I have no idea what I am doing)

Don't mix C and c++. No point at all of using printf there instead of simply using cout like you do in the rest of your program. As for the input part, use cin, which is for c++.

1
2
3
4
int i;
cout << "Enter a number\nNumber: ";
cin >> i;
// http://www.cplusplus.com/doc/tutorial/basic_io/ 
Ok so I added those changes you mentioned, however, now when I get to the debug it makes me enter a number 2ce before it gives me an output and it still won't follow my 4 rules err
Ah! Thank you! I'm learning from this as well. C is a strange and confusing language.

Well, I'm trying to work this out, and thus far I have what I believe is:

if ((i % 5 == 0 && i != 600) || (i % 5 != 0 && i != 66) || (i % 5 == 0 && i != 60))

Or to put it another way, the call to bizz is:

(i ends in 5 or 0 and isn't 600) OR (i DOESN'T end in 5 or zero and isn't 66) or (i DOES end in 5 or 0 and isn't 60)

Does that sound about right? Because if so, the second line is effectively discarding any numbers that end in 5 or 0 automatically, and is only satisfied by the number 66, and the third line... give me a second...

It appears the third line is only satisfied by the numbers 600 and 60.*

*disclaimer: there's every chance I could be wrong.
Last edited on
For bizz the rule is:
divisible by 5 and doesn't have a 6

So there is no point of checking if it is not divisible by 5 - i % 5 != 0
You're also checking if it's not 600, 66 or 60. But what if its 26? or 36? or 67? Those all contain the number 6.
Well Basically.
For "Bizz" you want the number you input to end in a 5 or 0; however, you don't want there to be a 6 in the number at all. You don't want a 6 in the hundreds, tens, or ones places. (For this project only getting tested on 1-999 so don't worry about past that)
For "Buzz" you want the input to NOT end in 5 or 0; however if there is a 6 anywhere in it you need to cout << "Output: Buzz" endl;
If the number ends with a 5 or 0 AND it contains a 6 anywhere in it then cout << "Output: Bozz" endl; finally if none of these rules work. Output the number. For this cout << i << endl; seems to work fine.
Well 660 would also fit into Bozz Idk if there is more than those 3 numbers. While also you Have to state the it is not divisible because then Bizz would count 60, 660 and 600 I figured
At the risk of introducing something new, just use a stringstream object to effectively convert your number into a string, then search the string for sixes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <sstream>

int main() {

	int number = 60;

	std::stringstream stream;

	stream << number;

	std::string string = stream.str();

	bool is_divisible = (number % 5 == 0);
	bool has_six = (string.find_first_of("6") != std::string::npos);
	//... 


But I like I said, this would look suspicious to your instructor if he hasn't covered them yet.
Last edited on
Well the way I see it. I will break down my thought on my code.
if ((i % 5 == 0) && ((i / 100) != 6) || (i % 5 == 0) && ((i / 10) % 10 != 6) || (i % 5 == 0) && ((i % 10) != 6)) for the i%5==0, this checks the input number "i" for divisibility by 5. then && means that I also wanna check for another rule being the checking for a 6. ((i / 100) != 6) checks the Hundreds place. When I do || i am then doing this to check divisibility of the number again while this time doing ((i / 10) % 10 != 6) to check the tens place. Finally I do || again but this time I do ((i % 10) != 6) to check the ones place.
Yeah it is funny because I litterally have NEVER done programming before and all that we were given... was this info. I will post the hints he gave us...
Though we are using C++ he gave us the C commands of printf, scanf, and told us this. hund=n/100; ten=n/10%10; one=n%10; (hund==6) || (ten==6)||(one==6)
number % 5 ==0

This is all we were given.
I've actually reformatted it so it doesn't give compiler warnings for parentheses:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
int main()
{
	int i;
	cout << "Enter a number\nNumber: ";
	cin >> i;
	if (((i % 5 == 0) && ((i / 100) != 6)) || ((i % 5 == 0) && ((i / 10) % 10 != 6)) || ((i % 5 == 0) && ((i % 10) != 6)))
	cout << "Output: bizz" << endl;			 // bizz is divisible by 5 and doesn't have a 6
    else if (((i % 5 != 0) && ((i / 100) == 6)) || ((i % 5 != 0) && ((i / 10) % 10 == 6)) || ((i % 5 != 0) && ((i % 10) == 6)))
	cout << "Output: buzz" << endl;		 // buzz is not divisible by 5 however it does have a 6
	else if (((i % 5 == 0) && ((i / 100) == 6)) || ((i % 5 == 0) && ((i / 10) % 10 == 6)) || ((i % 5 == 0) && ((i % 10) == 6)))
	cout << "Output: bozz" << endl;  // bozz is divisible by 5 and has a 6
	else cout << i << endl;				 // i is neither divisible by 5 nor does it have a 6
	return 0;
}


And here's the thing - I think you're taking too many commands in your initial "bizz" call, and the call also contradicts itself. Now please remember, this is based on MY UNDERSTANDING of what you have in the first if statement, and I could very well be wrong, but it looks like:

i divisible by 5 or 0 and not 600, or i divisible by 5 and 0 and not 606, or i divisible by 5 or 0 and not 66.

And to be perfectly honest, this is really confusing code. We both agree on that much, right? But anyway, the problem is you're wanting to remove, say, 600 from the first statement, but it's perfectly acceptable because of the other calls. That's to say, it's finding the first call invalid, but it has two more options that are perfectly valid (if I'm understanding this right... the i / 10 % 10 != 6 I'm shaky on, but the last function is perfectly acceptable for 600), so you're saying, "I'm not going to allow 600 in the first if statement... except I kind of am..." and it's strange.

EDIT: And in the case of your code - ESPECIALLY this code - I would probably be very explicit in the first line to remove values not ending in divisible by 5 or ending in 6 from the very start with:

 
if (i % 5 != 0 || i % 2 != 0) { cout << i << endl; }


And just be done with it right then and there, since it would be an odd number that isn't 5 right from the start. Normally I'd reserve that for the end, but in the case of this very confusing code I think it prudent to just terminate the program ASAP for values we know we don't want.
Last edited on
well i'm sure I don't have has much of a clue about this then you do but when I say i % 5 == 0 I am saying "i" as in the number input (Be it any number from 1-999) will take the place of "i" so if i input 60, it will take the place of "i" in the if statement. so 60%5 == 0 this is true so it is divisible by 5. Now I check the hundreds place, tens place, and ones place for a 6. This is done through 60/100 == 6 for hundreds which is false. 60/10%10 ==6 so 60/10 = 6%10 = 6 so this is true. Finally 60%10 == 6 this is false. So after checking the hundreds, tens, and ones place. the tens place is true so it is divisible by 5 and contains a 6. This is a number that SHOULD output Bozz.
No, I'm certain I know what a variable is ;)

Try this. I haven't tested it in GREAT detail, but I gave it a few inputs and it seems to work well enough. You might find otherwise. It's not elegant by any stretch of the imagination, and it can be fooled by just about anything (give it a letter) but it SEEMS to work.

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
#include <iostream>

using namespace std;

int main()
{
	int i;
	while (cin)
	{
		cout << "Enter a number: ";
		cin >> i;
		// bizz is divisible by 5 and doesn't have a 6
		if (i % 5 == 0 && (i / 10 != 6 &&i % 10 != 6 && i / 100 % 10 != 6 &&
			i / 100 != 6 && i != 61 && i != 62 && i != 63 && i != 64 &&
			i != 65 && i != 67 && i != 68 && i != 69)) cout << "Output: Bizz\n";
		// buzz is not divisible by 5 however it does have a 6
		else if (i % 5 != 0 && (i % 10 == 6 || i / 100 % 10 == 6 || 
			i / 100 == 6 || i == 61 || i == 62 || i == 63 || i == 64 ||
			i == 65 || i == 67 || i == 68 || i == 69)) cout << "Output: Buzz\n";
		// bozz is divisible by 5 and has a 6
		else if (i % 5 == 0 && (i % 10 == 6 || i / 10 == 6 || i / 100 % 10 == 6 ||
			i / 100 == 6 || i == 61 || i == 62 || i == 63 || i == 64 ||
			i == 65 || i == 67 || i == 68 || i == 69)) cout << "Output: Bozz\n";
		else cout << i << endl;
	}
}
Last edited on
Omg It works exactly how it is supposed to and in the same sort of format that I was talking about Nice man! I have a question though. How did you set it so that you can set an input in debug without it closing. Like usually before I would put 1 input and get an output and it would say press any key to continue and it would close debug. But with your program it works so that you can get multiple inputs / outputs without closing it.

EDIT: Is it because you have the function in a while? so you can enter multiple inputs to the Debug?
Last edited on
That was implemented (probably quite poorly - ask someone on the forums for a more elegant way) with the code:

1
2
3
4
5
while (cin)
{
cin >> i
//code here
}


Which, so long as it continues receiving integer input, it will continue reading the while as true. You could also use a do while version:

1
2
3
4
5
6
7
8
char answer;
do
{
//all the code above
//code is finished and it returned the answer you needed
cout << "Continue? 'y' for yes: ";
cin >> answer;
} while (answer == 'y' || answer == 'Y');


The reason I didn't give it to you in that manner is because you PROBABLY want to rapid-fire numbers in to check, and aren't interested in typing 'y' EVERY TIME you want to test a number.
Yeah, and again this is LITTERALLY the first code that I have been assigned with 0 programming knowledge so I am not looking for the smoothest way to cut down the code. Plus I personally like it all written out the way you did it. What you showed me is what I have been trying to ask people how to do for the past like 3 days haha. Thanks a lot I just gotta make sure of understanding everything like I get how you implemented the rules. I just gotta understand why you only had to mark 61-69 a being == "i". That part confuses me a bit.
I just gotta understand why you only had to mark 61-69 a being == "i".


Well, that was simply because I couldn't think of any "nice" way to find all values between 60-69 without explicitly including them in a single operation, else I certainly would have. It may exist, and if there is a better way I'd love to know it, but as I was testing it, those values kept eluding the checks, so I just directly excluded them.

Again, that code is FAR from elegant, and I'm slightly ashamed of it because it's so ugly, and I can't help but think there's a better way to do it (and again, I would LOVE to know if there is), but it REASONABLY accomplishes what it's supposed to do.
Pages: 12