Beginner help with loop validation

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!

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

int leafChoice, veinChoice, edgeChoice, barkChoice;

int main()
{
    bool status = true;
    while (status)
    {
        cout << "Welcome to Tree ID!" << endl;
        cout << "Please choose the type of leaf to get started." << endl << endl;
        cout << "1. Hand Shaped Leaves" << endl;
        cout << "2. Spear Shaped Leaves" << endl;
        cout << "3. Heart Shaped Leaves" << endl;
        cout << "4. Round Leaves" << endl;
        cout << "5. Needles" << endl;
        cout << "6. Quit" << endl << endl;
        cout << "Enter choice: ";
        cin >> leafChoice;

        if ((leafChoice < 1) || (leafChoice > 6))

            {
                cout << endl;
                cout << "Please enter a valid option" << endl << endl;
                status = true;
            }

        else ((leafChoice > 0) && (leafChoice < 6));
            {
                status = false;
                cout << "Now let's take a look at the veins, are they:" << endl << endl;
                cout << "1. Netlike?" << endl;
                cout << "2. Parallel?" << endl;
                cout << "3. Restart" << endl << endl;
                cout << "Enter choice: ";
                cin >> veinChoice;
            }

    }


        cout << leafChoice << veinChoice << edgeChoice << barkChoice;


  return 0;
}
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
        cout << "Welcome to Tree ID!" << endl;
	cout << "Please choose the type of leaf to get started." << endl << endl;
	cout << "1. Hand Shaped Leaves" << endl;
	cout << "2. Spear Shaped Leaves" << endl;
	cout << "3. Heart Shaped Leaves" << endl;
	cout << "4. Round Leaves" << endl;
	cout << "5. Needles" << endl;
	cout << "6. Quit" << endl << endl;
	cout << "Enter choice: ";
	cin >> leafChoice;

	while (leafChoice < 1 || leafChoice >6) {
		cout << "Invalid input" << endl;
		cout << "Please choose the type of leaf to get started." << endl << endl;
		cout << "1. Hand Shaped Leaves" << endl;
		cout << "2. Spear Shaped Leaves" << endl;
		cout << "3. Heart Shaped Leaves" << endl;
		cout << "4. Round Leaves" << endl;
		cout << "5. Needles" << endl;
		cout << "6. Quit" << endl << endl;
		cout << "Enter choice: ";
		cin >> leafChoice;
	}


This will guarantee you get a good response from 1-6, then you can ask for the next input and test it the same way.
Last edited on
Oh, okay, thanks for the tip about global variables. This looks much better and will work well with what I'm going to do. Thanks again!
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?

Thanks

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

int main()
{
    int leafChoice=0;
    int veinChoice=0;
    int edgeChoice=0;
    int barkChoice=0;

        std::cout << "Welcome to Tree ID!" << std::endl;
        std::cout << "Please choose the type of leaf to get started." << std::endl << std::endl;
        std::cout << "1. Hand Shaped Leaves" << std::endl;
        std::cout << "2. Spear Shaped Leaves" << std::endl;
        std::cout << "3. Heart Shaped Leaves" << std::endl;
        std::cout << "4. Round Leaves" << std::endl;
        std::cout << "5. Needles" << std::endl << std::endl;
        std::cout << "Enter choice: ";
        std::cin >> leafChoice;
        std::cout << std::endl;

	while ((leafChoice < 1) || (leafChoice >5)) {
		std::cout << "Invalid input" << std::endl << std::endl;
		std::cout << "Please choose the type of leaf to get started." << std::endl << std::endl;
		std::cout << "1. Hand Shaped Leaves" << std::endl;
		std::cout << "2. Spear Shaped Leaves" << std::endl;
		std::cout << "3. Heart Shaped Leaves" << std::endl;
		std::cout << "4. Round Leaves" << std::endl;
		std::cout << "5. Needles" << std::endl << std::endl;
		std::cout << "Enter choice: ";
		std::cin >> leafChoice;
        std::cout << std::endl;
	}
        std::cout << "Now let's take a look at the veins, are they:" << std::endl << std::endl;
        std::cout << "1. Netlike?" << std::endl;
        std::cout << "2. Parallel?" << std::endl << std::endl;
        std::cout << "Enter choice: ";
        std::cin >> veinChoice;
        std::cout << std::endl;

    while ((veinChoice < 1) || (veinChoice >2)) {
        std::cout << "Invalid input" << std::endl << std::endl;
        std::cout << "Now let's take a look at the veins, are they:" << std::endl << std::endl;
        std::cout << "1. Netlike?" << std::endl;
        std::cout << "2. Parallel?" << std::endl << std::endl;
        std::cout << "Enter choice: ";
        std::cin >> veinChoice;
        std::cout << std::endl;
    }
        std::cout << "How about the edges of the leaves, are they?" << std::endl << std::endl;
        std::cout << "1. Smooth edges" << std::endl;
        std::cout << "2. Toothed edges" << std::endl;
        std::cout << "3. Lobed edges" << std::endl << std::endl;
        std::cout << "Enter choice: ";
        std::cin >> edgeChoice;
        std::cout << std::endl;

    while ((edgeChoice < 1) || (edgeChoice > 3)){
        std::cout << "Invalid input" << std::endl << std::endl;
        std::cout << "How about the edges of the leaves, are they?" << std::endl << std::endl;
        std::cout << "1. Smooth edges" << std::endl;
        std::cout << "2. Toothed edges" << std::endl;
        std::cout << "3. Lobed edges" << std::endl << std::endl;
        std::cout << "Enter choice: ";
        std::cin >> edgeChoice;
        std::cout << std::endl;
    }
        std::cout << "What does the bark look like?" << std::endl << std::endl;
        std::cout << "1. Smooth" << std::endl;
        std::cout << "2. Scales" << std::endl;
        std::cout << "3. Plates" << std::endl;
        std::cout << "4. Peeling strips" << std::endl;
        std::cout << "5. Shed Ribbon" << std::endl;
        std::cout << "6. Fibrous" << std::endl;
        std::cout << "7. Curved Ridges" << std::endl << std::endl;
        std::cout << "Enter choice: ";
        std::cin >> barkChoice;
        std::cout << std::endl;

    while ((barkChoice < 1) || (barkChoice > 7)){
		std::cout << "Invalid input" << std::endl << std::endl;
        std::cout << "What does the bark look like?" << std::endl << std::endl;
        std::cout << "1. Smooth" << std::endl;
        std::cout << "2. Scales" << std::endl;
        std::cout << "3. Plates" << std::endl;
        std::cout << "4. Peeling strips" << std::endl;
        std::cout << "5. Shed Ribbon" << std::endl;
        std::cout << "6. Fibrous" << std::endl;
        std::cout << "7. Curved Ridges" << std::endl << std::endl;
        std::cout << "Enter choice: ";
        std::cin >> barkChoice;
    }
        std::cout << leafChoice << veinChoice << edgeChoice << barkChoice;


  return 0;
}
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main() {

	int num1 = 1;
	int num2 = 2;
	int num3 = 3;
	int num4 = 4;

	int info = num1 * 1000 + num2 * 100 + num3 * 10 + num4;

	cout <<"Full number: "<< info << endl;

	cout << "num1 = " <<num1%1000 << endl;
	cout << "num2 = " << num2 % 1000 << endl;
	cout << "num3 = " << num3 % 1000 << endl;
	cout << "num4 = " << num4 % 1000 << endl;

	return 0;
}
Oh wow, that's such a good idea. Thanks Joe! Now I've got a lot to work with.
Topic archived. No new replies allowed.