I'm trying to create a program that asks a set of questions to determine what kind of tree a person is looking at. I'm new to programming and I'm having a hard time getting started. Could someone help me figure out a good way of nesting these loops to validate the data?
I've only included the first loop but I'm not sure how to get it working correctly. It doesn't loop back to the beginning of the while loop, it just continues the program.
Is it bad programming practice to keep nesting the loops? Should I make void functions that ask the questions and retrieve the data instead or is this format okay?
I'm planning on validating the trees by taking the numbers collected and comparing it to a tree class file. Any help would be appreciated!
First don't use global variables, its bad practice and can cause problems later on. So move your variables into your main function. Next else doesn't test anything I think you meant to use else if. For your loop problem the simplest way to check for good input would be to do something like:
I had another question regarding this code. I want to put all 4 variables into the same variable. So if the entries are 1, 2, 3, 4, the next variable will take all 4 variables into one without adding them together. So just a final variable that is 1234 and I'll use that to compare the tree ID numbers and then pull that info. What would be a good way to combine these numbers together? I tried to use a for loop but I feel like there has to be a simpler way to do this?
I've got 2 ideas for you. Note these are ways of doing it without any more advanced knowledge, yes there are better ways of doing this.
Option 1: You could use a string where you append each number on to the string and then when you need to test the numbers you look at each position in the string to get your number.
Option 2: Create an int to hold all the numbers. Then you'll do something like: int info = num1*1000 + num2*100 + num3*10 + num4; Then to get the numbers back out you will use modulo (%). Which to get the first number you would do num1 = info%1000 and num2 = info%100 and so on.
Personally, I would prefer option 2 but both work and it's up to you. Demonstration of option 2 would look like: