Loop Error

Write your question here.

The important part of the code is the loop all the way at the end.

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

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
	string product;
	int numattrib;
	int numoptions;
	int i = 1;
	int j = 1;
	int k; 
	int x = 1;
	int h = 1;
	string b[20][20];
	int n[20][20];

// insert explanation for product
// Asks the products you would like to analyze
	cout << "What would you like to analyse "; 
	getline(cin,product) ;


// tells user what product we are analysing and asks how many options of the product they have and inputs it as numoptions
	cout << "We are analyzing " << product << endl; 
	cout << "How many options do you have ";
	cin >> numoptions;


// while the numoptions is greater than i (the incrementar) the loop continues asking for the name of the option

	while ((numoptions + 1) > i)
	{

		cout << "What is option " << i  << " ";
		cin >> b[0][i];
		i++;
	}

// asks how many attributes the user would like to compare

	cout << "How many attributes do you have ";
	cin >> numattrib;

// asks the name of the attributes 

	while ((numattrib + 1) > j)
		{
		cout << "What is attribute " << j << " ";
		cin >> b[(j)][0];
		j++;
		}


// Resetting everything  ,will delete some later
		i = 1;
		j = 1;
		k = 1;
		x = 1;
		h = 1;

// doing option (number) attributes 
		while (numoptions > h)
		{
			while ((numattrib + 1) > i)
			{
				cout << "How much do you value attribute " << b[(i)][0];
				cout << " for option " << b[0][(k)];
				cin >> n[(j)][0];
				++j;
				++i;
			}
			h++;
			++k;
		}
	return 0;
}



I cannot figure out why the loop isn't looping correctly. I don't have any errors but the out side loop (while (mumoptions >h)) only loops once.
I also know the code might have to be edited and cleaned up. I'm coding after months of not doing it so I'm forgetting a lot of little things I used to know so it might just be something obvious that I cant see myself.
Last edited on
but the out side loop (while (mumoptions >h)) only loops once.
It loops almost exactly as much as it need to (one less time actually). See for yourself:
1
2
3
4
while (numoptions > h) {
    std::clog << "loop start\n"; //←Debug output
    while ((numattrib + 1) > i) {
//... 

Your problem lies in inner loop. Try to examine what happens step by step to see fault in your logic.
I've honestly been looking at this code now since I posted it and I still can't figure out what the issue is. I have a feeling what ever it is I'm not noticing because my brain is skipping over it. I even re-did it (the loop part) not looking at the original code and I still can't figure it out.
I usually don't ask for answers out right but can you please tell me whats wrong?
(this isn't for a class so I don't have a professor or other students I can ask)
Look at your code:
first you set i to 1.
Then you start outer loop, start inner loop and repeat it until i is less than numattrib + 1, incrementing i each iteration.
Then inner loop ends, outer loop repeats and you reach inner loop again. It starts with checking condition ((numattrib + 1) > i) . Question: What is value of i right now?
Last edited on
thank you it finally worked. As soon as I got past this problem my loop ended up looping indefinatly so I finally managed to fix it but do you see a way to do it differently without having to use the if (condition) break?

Its fine if you don't want to or cant help me further but I really appreciate you helping me with this it was starting to drive me crazy looking at the same part for 2 days.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
while (numoptions > h , i = 1)
		{
			while ((numattrib + 1) > i)
			{
				cout << "How much do you value attribute " << b[(i)][0];
				cout << " for option " << b[0][k];
				cin >> n[j][0];
				++j;
				++i;

				//
				//j = 1
				//k = 1
				// 
			} 
			if (numoptions < (k + 1)) break;
			h= h + 1;
			++k;
		}
If amount of iteration is known beforehand, use for loop:
1
2
3
4
5
6
for(int i = 1; i  <=numoptions; ++i)
    for(int j = 1; j <= numattrib; ++j) {
        cout << "How much do you value attribute " << b[j][0];
        cout << " for option " << b[0][i];
        cin >> n[j][0];
    }
(There is a strange MD array accesses, but I leave it here for now)
Last edited on
Topic archived. No new replies allowed.