0

Can your store if statements into a variable and test it?

Goal: 9 if statements test for what number multipleTwo is, 9 more overlaying if statements test for the output of multipleOne + Two, 5 more of statements test for the scale size of output of One and Two, 4 more operation category test for operation between One and Two. Question: Can I make things easier for myself by storing the 9 multiple 2 if statements into a variable, that will be tested by 1 through 9 One statments, ALL THE WHILE the correct if statement from Two PRINTS the output, not just checks if its correct? Script: https://code.sololearn.com/cofrv27a3p6W/?ref=app

17th Aug 2018, 6:51 PM
Slushie
Slushie - avatar
35 Antworten
+ 1
Ok now I've seen your code on a desktop it is easier to read. What you are essentially asking is a way to store the *commands* you want to execute when the time to print comes i.e. if multipleTwo == 2 {execute these when I want to print, after I know the other inputs} I'm not too familiar with C++, but I know Java and what I would do is make a class containing one method a bit like this: class MultipleTwo { void print(); } this would be like an interface in Java, but basically a class with a default or no implementation. Then you could have class MultipleTwoNumberThree extends/implements MultipleTwo { void print() { // your code to print } } This way you can create the class that you need, and at the end you can invoke print on your object. This is polymorphism. There may be an easier way to do something similar to functional programming in C++ which I think is what you want, to store the commands in an object to be executed at some later point When you said "I've resigned to copy and pasting" this just screams use polymorphism, or functions, or something like this. As a programmer, you have to spot the overarching problem you want to solve, and solve them all at once. Your code is going well, but it's going to be error prone, because one small typo will mean that your validation (check for 'division') will be invalid for the next check if this subsequent check is spelled wrong. If I write 'diViSion' the code will fail - could you instead convert the input to uppercase and compare on that? Using methods/functions abstracts this process instead of having the programmer write it out multiple times
18th Aug 2018, 9:44 AM
Dan Walker
Dan Walker - avatar
+ 1
So each if statement takes a Boolean statement, and it seems to be this that you want to store. You could have separate variables with descriptive names, like multOneIsValidRange = multipleOne > 0 && multipleOne < 10 and use the named boolean variable in the if statement. You can gradually build up more complex statements using these which will obviously include the previous checks. Alternatively you could define some methods that take an input and check some logic, so you can pass the arguments in and not repeat the logic
17th Aug 2018, 8:14 PM
Dan Walker
Dan Walker - avatar
+ 1
I kinda have just resigned to copying and pasting the script. My goal was just to have 9 if statements for multipleTwo, each with unique outputs, all scrunched up into 1 variable, so that when the multipleOne if statement is true, it will test for the multipleTwo if statement inside of the scrunched up variable.
18th Aug 2018, 2:28 AM
Slushie
Slushie - avatar
+ 1
Wow... 😂 That second option is exactly what I need. Thanks for your help I really appreciate it.
18th Aug 2018, 4:51 PM
Slushie
Slushie - avatar
0
That Penguin tried with below input and nothing is output... 2 3 3 Multiplication p.s. : checked this on sololearn application for Android
17th Aug 2018, 7:12 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
That Penguin check for input data type... For example, first input should be of int type only... cin.fail may help you for that...
17th Aug 2018, 7:14 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
I just finished the 2 as first input but still on 1st scale factor. Not third that's why 2 2 3 didn't work.
17th Aug 2018, 7:17 PM
Slushie
Slushie - avatar
0
That Penguin I checked your validation for wrong operation (line 106)... You have used nested if condition to validate the same...you can do it as below in single statement: if (operation == "Multiplication" || operation == "Division") // add all values to compare with || { //perfect input } else { // error messgae }
17th Aug 2018, 7:25 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Alright. But about the origonal question all I want to know is, can I store if statements with specific outputs into a storage variable of some kind that, by another if statement, will test for the right input inside of said storage variable.
17th Aug 2018, 7:37 PM
Slushie
Slushie - avatar
0
That Penguin sorry, but I am getting confused by your question...What benefit you will get it from that another so called storage variable is not making me sense...again in storage variable of some kind, you are going to test another if statement...so, rather directly go with perfect if statement....
17th Aug 2018, 7:43 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
😕 Hmm... I was thinking I could 1-9 multipleTwo outputs and put it into a variable, so that I can just say something like: if (multipleOne == 1) { cout << variableThatHolds9IfStatements; return 0; } And I was asking if I such a variable is possible, and if it would read every if statement. It seems alittle to complicated at this point so I think Ill just resine to copying and pasting the text.
17th Aug 2018, 7:48 PM
Slushie
Slushie - avatar
0
And predefine that variable before the script.
17th Aug 2018, 7:49 PM
Slushie
Slushie - avatar
0
That Penguin if you want to use multiple if statements and test a variable in different sections in your code you could try to use a function and pass that variable to the function test all the if statments and return true or false. and that way your if statments are only put in once in the function. unless i am not understanding what you want to do.
18th Aug 2018, 1:57 AM
Mooaholic
Mooaholic - avatar
0
That Penguin ok sorry i couldnt be more help, but it does sounds like a function is what you want.
18th Aug 2018, 2:40 AM
Mooaholic
Mooaholic - avatar
0
That's fine. Thanks for your help anyways.
18th Aug 2018, 2:46 AM
Slushie
Slushie - avatar
0
https://code.sololearn.com/cAr4WKQ6RbWe/?ref=app Alright so I tried to make a code based on what you said. I'm trying to have twoOne and twoTwo tested, by way of firstTwo. Not really correct as you can see. But its a better representation of what I am trying to do. Can someone explain to me how I would correctly write this? In the end there will be twoOne through twoNine so all 9 of statements needs to be correct.
18th Aug 2018, 3:22 PM
Slushie
Slushie - avatar
0
That Penguin giving the code you put with the class i made a version with a function and switch, there are 2 so you have to comment one out to run other. the first one just takes y and will just print y. second one takes both x and y and prints both but customization can be made. not sure if this is what you want or not though. https://code.sololearn.com/cjRi2R12V2mt/?ref=app
18th Aug 2018, 4:31 PM
Mooaholic
Mooaholic - avatar
0
That Penguin glad to help let me know if you have other problems
18th Aug 2018, 4:53 PM
Mooaholic
Mooaholic - avatar
0
Alright I found another issue. https://code.sololearn.com/cS2tWJ5iZxT0/?ref=app I'm trying to keep the multiplication sign in a void so that I dont have to continuously type it. Basiclly same story as multipleOne and Two expect, then sign shows up by the user inputting "Multiply" instead of 1 or 2, so on.
18th Aug 2018, 6:05 PM
Slushie
Slushie - avatar
0
That's a problem because you can't have words or letters for cases...
18th Aug 2018, 6:05 PM
Slushie
Slushie - avatar