#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
usingnamespace 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.
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?
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;
}