my code wont do what I want it to do

Oh well :/
Last edited on
What is the purpose of these lines?
1
2
3
    letter1 = (r)||(g)||(b);
    letter2 = (r)||(g)||(b);
    letter3 = (r)||(g)||(b);


I believe you need to declare r g and b before using them
...
Last edited on
You have a fundamental misunderstanding of how the || operator works.

|| looks at what is on the left and the right of it. If either side is nonzero (true), it gives you back nonzero (true). If both sides are zero (false), it gives you zero (false).

What is happening is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//  assuming your variables are assigned to these values (which they aren't because you
//   haven't assigned them yet... but whatever):
// r == 0
// g == 1
// b == 2
letter1 = (r)||(g)||(b);

// after subbing in variable contents...
letter1 = 0 || 1 || 2;

// the first || is evaluated first.
//  "0 || 1".  Since the right side is nonzero, the || will evaluate to nonzero (1)
//  so   "0 || 1"  evaluates to "1".  Leaving us with:
letter1 = 1 || 2;

// now the next || is evaluated
// "1 || 2".  Since both sides are nonzero, the result is nonzero (1)
//  so "1 || 2" evaluates to "1".  Leaving us with:
letter1 = 1;

// so all you're doing with this line is assigning a value of 1 to your letter1 variable 


Note this is bad for a few reasons:

1) It's pointless because r, g, and b have no real meaning here. You can have the same effect by just assigning 1 directly.

2) Assigning any value to letter1 defeats the purpose of getting letter1 from the user. By reassigning like this you are erasing what the user input and replacing it with a value of 1. If you just throw away what the user gives you, you might as well not be asking them for anything.


The question now is... why do you think you need to do this? You are already checking the contents of letter1,2,and 3 below on line 27:

if (x==letter1&&y==letter2&&z==letter3)

What's more... that line is actually correct.

So if you're already properly getting user input for these letters on line 20. And properly checking to make sure they input the correct values on line 27 ... what do you need lines 21-26 for?
Last edited on
That code above is either going to return a 0 or a 1, you're going to get very unexpected results with it and the way it looks now is that you're not even going to need it. All you did was ask the user for input then immediately change it so more often then not you're going to be getting the else statement instead of the if statement. If no one else has helped you by the time I get back to my computer, I'll help you out.
Here is what I came up with. http://pastebin.com/YTVHXqWD

It's a little more complicated, but it allows you to do more than 3 answers a lot easier as well. It's very hard to keep code compact using different variable names so that is why I switched it to using arrays. I don't really know of an easier way to convert from int and char so that the code knows which is which.

If someone has a more efficient way of doing this, I'd like to know about it bc this is the only way that I know of with staying in the constraints of his wants anyways.
I really appreciate the information Disch. That makes complete sense, so now I know how to not write the program. Volatile Pulse, your program is much appreciated but unfortunately I can not do this program with arrays. I have to use integers, characters, if else statements, etc. It has to be the pretty basic programming knowledge. Like the REALLY basic programming material. That is how I was told how to write it, so no arrays :/. Another idea I had was trying to convert character values to integer values, but I had a dead end in that direction as well. My last idea was to have my program accept the actual characters 'r' 'g' 'b' and use some kind of logic to compare these characters to the value.
Any other ideas on how my code will work?
What exactly is is meant to do? The only clue in it is "This program will randomly generate 3 numbers between 0 and 2 each time you run it." Is it supposed to be a random number generator? What are you trying to do?
The program is like a test. First you get random numbers and that random number 0-2 is supposed to correspond to red, green, and blue respectively. Other than a lot of repeating if conditions, I don't know a simple way to check chars against ints. My only other idea was a for loop and check the answers each time someone enters a number to reduce the number of if statements.
Topic archived. No new replies allowed.