+ 1

Is this an efficient script? It works, just wondering if this was the best way to do this.

#include <iostream> using namespace std; int main() { int BXP = 1000; int XP; int lvl = 1; int XPN = (BXP+(lvl*100))-100; cin>>XP; while (XP >= XPN){ if (XP >= XPN) { XP -= XPN; lvl += 1; XPN += (lvl*100); } cout<< "lvl: "<< lvl <<endl; cout<< "XP: "<< XP <<endl; cout<< "XP Needed: "<< XPN; } cout<<endl; cout<< "Final Results"<<endl; cout<< "lvl: "<< lvl <<endl; cout<< "XP: "<< XP <<endl; cout<< "XP Needed: "<< XPN <<endl; return 0; } Cld I do better?

28th Feb 2017, 9:51 PM
Kai
7 Réponses
+ 13
Replace XPN += (lvl*100); with XPN += 100;
1st Mar 2017, 2:35 AM
Jafca
Jafca - avatar
+ 11
It looks efficient enough but I thought I'd tidy it for ya (I also got rid of "extra" things like BXP): #include <iostream> using namespace std; int main() { int XP; int lvl = 1; int XPN = 1000; cin>>XP; while (XP >= XPN){ XP -= XPN; lvl += 1; XPN += (lvl*100); cout << "lvl: " << lvl << endl << "XP: " << XP << endl << "XP Needed: " << XPN << endl; } cout << "Final Results" << endl << "lvl: " << lvl << endl << "XP: " << XP << endl << "XP Needed: " << XPN << endl; return 0; }
1st Mar 2017, 2:14 AM
Jafca
Jafca - avatar
+ 10
Change if (lvl = 50) to if (lvl == 50) = assigns the value 50 to lvl. == checks if lvl is equal to 50 and returns true if it is.
1st Mar 2017, 8:47 AM
Jafca
Jafca - avatar
+ 1
Thanks! that looks alot better haha but I've actually added a few things to the script and have also found out that the increment system has messed up... the XPN is supposed to go up by 100 every level :/ instead it's adding 100 to a pool and increasing the XPN by that. for example: lvl 1 XPN = 1200 lvl 2 XPN = 1300 lvl 3 XPN = 1500 lvl 4 XPN = 1800 lvl 5 XPN = 2200 but I need it to be lvl 1 XPN = 1000 lvl 2 XPN = 1100 lvl 3 XPN = 1200 lvl 4 XPN = 1300 ect... any ideas?
1st Mar 2017, 2:28 AM
Kai
+ 1
... that was so simple... haha thanks!
1st Mar 2017, 2:38 AM
Kai
+ 1
#include <iostream> using namespace std; int main() { int XP; int lvl = 1; int XPN = 1000; cin>>XP; while (XP >= XPN && lvl <= 49){ if (XP >= XPN) { XP -= XPN; lvl += 1; XPN += 100; } cout<< "Congrats! You Leveled Up!"<<endl<< "lvl: "<< lvl <<endl<< "XP: "<< XP <<endl<<"XP Needed: "<< XPN <<endl; } if (lvl = 50) { cout<<"Congrats! You've reached max level!"; } if (lvl < 50) { cout<<endl<<"Final Results"<<endl<< "lvl: "<< lvl <<endl<< "XP: "<< XP <<endl<< "XP Needed: "<< XPN <<endl; } return 0; } new problem... this script works. however, it always prints "Congrats! You've reached max level" whether the lvl = 50 or not. Could you tell me the error?
1st Mar 2017, 4:31 AM
Kai
+ 1
Oh I see.. so the single equal operation is for assigning to variables and the double operation is for testing conditions. thank you once again haha I apologize if I ask too many questions
1st Mar 2017, 2:02 PM
Kai