Click here to Skip to main content
16,004,678 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I'm still fairly new to programming and I am in the middle of a home work question Though I bricked at this point. I was able to write out the base of the program, however it's functioning the way I want it to. What it needs to do is display the result color of entering two primaries and display an error if anything else has been entered, however it just jumps straight to the error.

5. Color Mixer
The colors red, blue, and yellow are known as the primary colors because they cannot be made by mixing other colors. When you mix two primary colors, you get a secondary color, as shown here:
● When you mix red and blue, you get purple.
● When you mix red and yellow, you get orange.
● When you mix blue and yellow, you get green.
Design a program that prompts the user to enter the names of two primary colors to mix. If the user enters anything other than “red,” “blue,” or “yellow,” the program should display an error message. Otherwise, the program should display the name of the secondary color that results.

What I have tried:

VB
Module Module1

    Sub Main()
        'Declare variables
        Dim Red As String
        Dim Blue As String
        Dim Yellow As String
        Dim Color1 As String
        Dim Color2 As String

        'User input
        Console.WriteLine("Input 2 colors Choices one at a time from Red, Blue, or Yellow.")
        Color1 = Console.ReadLine
        Color2 = Console.ReadLine
        If Color1 = Red And Color2 = Blue Then
            Console.WriteLine("Purple")
        Else If Color1 = Red And Color2 = Yellow Then
            Console.WriteLine("Orange")
        ElseIf Color1 = Yellow And Color2 = Blue Then
            Console.WriteLine("Green")
        Else
            Console.WriteLine("Error: Invalid input")
        End If
        Console.WriteLine("Press Enter to quit.")
        Console.ReadKey(ConsoleKey.Enter)

    End Sub

End Module


Side notes:
(*)I've also tried not making the primary colors variables, however it caused a syntax error.
(*)The language we use in class is VB.
Posted
Updated 7-Feb-16 9:07am
v2
Comments
Sergey Alexandrovich Kryukov 7-Feb-16 14:56pm    
As the purpose of some homework is to teach you something, you should talk to the person who gave you your assignment.
And you did not even explain what do you mean by "VB".
—SA
Member 12312671 7-Feb-16 16:04pm    
If you don't know what VB means then you couldn't help anyways. Our teacher doesn't have much time for us outside of class and his teaching style doesn't fit my learning style. It happens I can't have a teacher I see eye to eye with every quarter, but you know what? I'm an adult I deal with it and find the help I need. That aside if want to troll peoples posts go to 4Chan or something I don't have time to deal with Children.
Sergey Alexandrovich Kryukov 7-Feb-16 19:55pm    
No, this is you who don't know what is "VB". Let me try again: "VB"? Which one? If you don't know which one, you cannot get any help and do any development anyway.
—SA
Sergey Alexandrovich Kryukov 7-Feb-16 19:59pm    
The fragments of the "code" in your comment suggests that it's VB.NET. Here is my advice: first of all, never call it "VB", to avoid not only confusion, but also negative response from some people working with VB.NET... :-)
Also not that it's often important to indicate the version of the language and .NET framework.
—SA
Sergey Alexandrovich Kryukov 7-Feb-16 19:59pm    
The fragments of the "code" in your comment suggests that it's VB.NET. Here is my advice: first of all, never call it "VB", to avoid not only confusion, but also negative response from some people working with VB.NET... :-)
Also not that it's often important to indicate the version of the language and .NET framework.
—SA

Alright - as it's homework, I will give you good hints but not reveal everything.

Imagine I tell you this: "I have an integer variable named X. What is the result of X + 5 ?". What will be your response? Your code can't work because of an analogous problem.

When you've solved this, there's another problem, though it's more conceptual: How many color combinations can there be for two inputs with 3 options each? Your code only covers a small number of these combinations before jumping to the conclusion that the input was invalid.

If you got this working so far and would like a small challenge, leave a reply and I'll give you a small hint on how you could do all this in a more elegant way.

Good luck!
 
Share this answer
 
Comments
Member 12312671 7-Feb-16 15:41pm    
Ok just got out of the shower saw your post, so I fixed the conceptual issue and now I've noted that VB wont flip the combinations by itself. Still trying to wrap my head around the first part, can you give me more detail.


here's what i did to fix the other problem:
If Color1 = Red Or Blue And Color2 = Blue Or Red Then
Console.WriteLine("Purple")
ElseIf Color1 = Red Or Yellow And Color2 = Yellow Or Red Then
Console.WriteLine("Orange")
ElseIf Color1 = Yellow Or Blue And Color2 = Blue Or Yellow Then
Console.WriteLine("Green")
Else
Sascha Lefèvre 7-Feb-16 16:05pm    
Your idea is correct but it's not the right way to write it. Each side of the Or-operator has to be an expression that evaluates to Boolean (true/false). For "Color1 = Red Or Blue" the left side is evaluating to a Boolean but the right side is just a String.

And whenever you combine "Ands" and "Ors" in an expression (different operators in general) you have to pay attention to the operator precedences: An "And" has a higher priority than an "Or". So if you have an expression like "A Or B And C Or D" then the compiler will actually interprete this like "A Or (B And C) Or D". So if you don't want this you have to place brackets to make the expression evaluate in the order you want it to.

Do you still need more detail for the first part (since solution 2 more or less spoiled it ;-) )
Member 12312671 7-Feb-16 16:42pm    
According to the VB guide on how to assign a value to string it should look along the lines of this:

Dim Red As String = Microsoft.VisualBasic.Left(Red, 1)
Dim Blue As String = Microsoft.VisualBasic.Left(Blue, 2)
Dim Yellow As String = Microsoft.VisualBasic.Left(Yellow, 3)
I believed This to be correct, and it stops all the variables below those lines from having the "variable "x" is used before it has been assigned a value." warning But the Variable = (here, 1) in my code still has that error.

Another major concern is this warning I keep getting: "Conversion from string "blue to type 'Double' is not valid."

Also thank you very much I really appreciate your help so far
Sascha Lefèvre 7-Feb-16 17:10pm    
Assigning values to your string variables: You're thinking too complicated here. You're basing the assigned value on the current value which is not set. (And I don't know why you would want to use Left(..)?) You just need to assign a "straight" constant string value. Just write it down there :-)

"Conversion from string "blue to type 'Double' is not valid.": I assume that's because you haven't yet fixed that problem I wrote about in my last comment.
Member 12312671 7-Feb-16 17:30pm    
This is what I had done to fix the And/Or issue if that's what you meant just now, although I'm not sure if I used the proper type of bracket or placed them in the right place. I'll show you what I did:

If Color1 = (Red Or Blue) And Color2 = (Blue Or Red) Then

Also just want to point out so you don't think I'm just slacking while I'm waiting, I'm also writing the midterm extra credit assignment program between working on this one. Speaking of which would you mind having a look when its done? I feel its bulky and I could consolidate the code to make it a smaller program over all.
You haven't assigned a value to the variables Red, Blue, or Yellow so the input colour will never match and so you will drop through to your error message.

You need to learn how to debug your code - have a look at this CodeProject article:
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
 
Share this answer
 
v2
Comments
Member 12312671 7-Feb-16 15:44pm    
Thank you I will look into this, I don't really like our book. This guide seems more structured to my tastes.
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Feb-16 20:01pm    
Good point, a 5.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900