NEWS Forum Wiki XDA TV

# C++ loop coding help

Tip us?
 TickleMeHomo69 21st September 2013, 05:23 AM Member - OP Thanks Meter 5 Posts: 45 Join Date: Dec 2012 C++ loop coding help I have a program that writes out numbers in roman numerals. I need help writing a loop that repeats the program unless I enter -1 it will stop the program from repeating Code: ```#include using namespace std; int main() { double num; int intnum, m, d, c, l, x, v, i, n; cout << "Enter a year (1000-3000) enter -1 to exit: "; cin >> num; intnum = (int)num; m = intnum / 1000; d = ((intnum%1000) / 500); c = ((intnum%500) / 100); l = ((intnum%100) / 50); x = ((intnum%50) / 10); v = ((intnum%10) / 5); i = (intnum%5); n = m + d + c + l + x + v + i; while (n > 0) { cout << ""; { for (m; m>0; m--) cout << "M"; } { for (d; d>0; d--) cout << "D"; } { for (c; c>0; c--) cout << "C"; } { for (l; l>0; l--) cout << "L"; } { for (x; x>0; x--) cout << "X"; } { for (v; v>0; v--) cout << "V"; } { for (i; i>0; i--) cout << "I"; } n--; } return 0; }``` REPLY
 cycad 21st September 2013, 05:40 AM Junior Member Thanks Meter 10 Posts: 12 Join Date: Aug 2013 Location: East Coast More Info > I think you want to put your code into another loop, like this: while (1) { cin >> num; if (num == -1) break; // process num here } REPLY

TickleMeHomo69
Member - OP
Thanks Meter 5
Posts: 45
Join Date: Dec 2012
Quote:
 Originally Posted by cycad I think you want to put your code into another loop, like this: while (1) { cin >> num; if (num == -1) break; // process num here }
It doesn't allow me to quit unless I input -1 which is good BUT it does not repeat the process. After it gives me a roman numeral it just stays there and does nothing until I press -1

I need it to ask me again to enter a year and it will output a roman numeral.
 cycad 21st September 2013, 06:34 AM Junior Member Thanks Meter 10 Posts: 12 Join Date: Aug 2013 Location: East Coast More Info > You need to break this down into functions so it's more readable. If something is confusing keep breaking it down. Try including and then using this prototype: string ConvertToRomanNumeral(int n); Also use this prototype: int GetInput(); Once you create those functions, then you can do something like this in your main function: while (1) {num = GetInput();if (num == -1) break;string roman_numerals = ConvertToRomanNumeral(num);cout << roman_numerals << endl;} It's a lot easier to read this way. REPLY
 daniel-s 21st September 2013, 07:22 AM (Last edited by daniel-s; 21st September 2013 at 07:32 AM.) Reason: typo Junior Member Thanks Meter 4 Posts: 18 Join Date: Sep 2013 As a side note, you can input values 1 to 3999 for your algorithm to handle them correctly. Twice as much at no extra cost! REPLY

Junior Member
Thanks Meter 10
Posts: 12
Join Date: Aug 2013
Location: East Coast
Quote:
 Originally Posted by daniel-s As a side note, you can input values up to 3999 for you algorithm to handle them correctly. 33% bonus at no extra cost!
Actually this really only works for values up to 3. 4 comes out as IIII which is only right for clocks.
 The Following User Says Thank You to cycad For This Useful Post: [ Click to Expand ]

TickleMeHomo69
Member - OP
Thanks Meter 5
Posts: 45
Join Date: Dec 2012
Quote:
 Originally Posted by cycad You need to break this down into functions so it's more readable. If something is confusing keep breaking it down. Try including and then using this prototype: string ConvertToRomanNumeral(int n); Also use this prototype: int GetInput(); Once you create those functions, then you can do something like this in your main function: while (1) {num = GetInput();if (num == -1) break;string roman_numerals = ConvertToRomanNumeral(num);cout << roman_numerals << endl;} It's a lot easier to read this way.
Thanks got it to work.
 TickleMeHomo69 2nd October 2013, 01:04 AM Member - OP Thanks Meter 5 Posts: 45 Join Date: Dec 2012 I have another question about another code. When I run it is suppose to give me the answer to the quadratic equation but it doesn't it gives me "No roots available. How can I fix to make it work properly so when I type in 3 numbers it gives me the answer to the quadratic equation and if there is any roots. Code: ```#include #include using namespace std; double discriminant(double n1, double n2, double n3); double pos(double n1, double n2, double n3); double neg(double n1, double n2, double n3); int main() { double a; double b; double c; double discrimi; double sqr1; double sqr2; cout << "Enter the first number in the quadratic equation: "; cin >> a; cout << "Enter the second number in the quadratic equation: "; cin >> b; cout << "Enter the third number in the quadratic equation: "; cin >> c; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); discrimi = discriminant (a,b,c); if(discrimi > 0 && a != 0) { sqr1 = pos(a,b,c); sqr2 = neg(a,b,c); cout << "In the equation " <
 daniel-s 2nd October 2013, 09:03 PM (Last edited by daniel-s; 3rd October 2013 at 04:10 AM.) Reason: typo Junior Member Thanks Meter 4 Posts: 18 Join Date: Sep 2013 what is this If these are parts of an exercise or a homework assignment, then the whole point is for you to debug these issues on your own. You're asking for some very basic debugging. Let me assure you, it's bad practice to run to the forum the first minute your code breaks. Debugging code that you have authored yourself is something that you must master if you want to become any good. And if you're not interested in becoming good, why are you doing this in the first place? REPLY

### XDA PORTAL POSTS

#### T-Mobile Galaxy S III LTE Finally Gets Android 4.3, Verizon S4 Gets Minor Bugfix OTA

New firmware updates have begun rolling out for two … more

#### Source Code Commits in Android 4.4.2 KOT49H Reveal Flash SMS Attack Fix and App Ops Removal

Just a few hours ago, we talked about how the … more

#### Prepare for Ubuntu Touch Smart Phones in 2014

There was much hype around the unveiling of Ubuntu Touch, Canonical’s take on a … more