Hello, I am quite new to C++ and have stumbled on a peculior probleme. When I run my code it gives the answer for the submitted arabic number in roman numerals, however if I click 'y' to run again something like this happens.

Enter number: 45
Roman numeral: XLV
Go again? (y/n) y
Enter number: 2
Roman numeral: XLVII
go again? (y/n)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132`` ``````#include #include #include #include using namespace std ; using namespace std; int main() { string roman; int integer; int piece; char again = 'Y'; while (again == 'y' || again == 'Y') { cout << "Entre un nombre entier: "; cin >> integer; if ((integer >= 4000) || (integer <=0)) { cout << endl << "Nombre d'entrĂ©e invalad" << endl; } else { if (integer >= 1000) { piece = (integer / 1000); for (int i = 0; i < piece; i++) { roman += 'M'; } integer %= 1000; } if (integer >= 100) { piece = (integer / 100); if (piece == 9) { roman += "CM"; } else if (piece >= 5) { roman += 'D'; for (int i = 0; i < piece -5; i++) { roman += 'C'; } } else if ( piece == 4) { roman += "CD"; } else if (piece >= 1) { for (int i = 0; i < piece; i++) { roman += 'C'; } } integer %= 100; } if (integer >= 10) { piece = (integer / 10); if (piece == 9) { roman += "XC"; } else if (piece >= 5) { roman += 'L'; for (int i = 0; i < piece - 5; i ++) { roman += 'X'; } } else if (piece == 4) { roman += "XL"; } else if (piece >= 1) { for (int i = 0; i < piece; i++) { roman += 'X'; } } integer %=10; } if (integer >= 1) { piece = integer; if (piece == 9) { roman += "IX"; } else if (piece >= 5) { roman += "V"; for (int i = 0; i < piece - 5; i++) { roman += 'I'; } } else if (piece == 4) { roman += " IV"; } else if (piece >= 1) { for (int i = 0; i < piece; i++) { roman += 'I'; } } } cout << "Chiffres Romain " << roman << endl; } cout << "Go again? (y/n) " ; cin >> again; } cout << "The end." << endl; return 0; } ``````
Last edited on
You never clear your `roman` string you just keep appending to it.

http://www.cplusplus.com/reference/string/string/clear/
Last edited on
I have tried multiple times to make it work thanks to the code that the link contains, however I cant seem to get it to work. Any ideas on where the code will go?
Last edited on
`roman.clear();` Line 115
Last edited on
I know that I probably sound like a idiot but...if I put roman.clear(); in line 115 i get:

1>------ Build started: Project: Travaux de chiffres Romain, Configuration: Debug Win32 ------
1> Main file.cpp
1>Testing loops.obj : error LNK2005: _main already defined in Main file.obj
1>C:\Users\Daniel\Documents\Visual Studio 2010\Projects\Travaux de chiffre Romain\Debug\Travaux de chiffres Romain.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
 1>Testing loops.obj : error LNK2005: _main already defined in Main file.obj

This would seem to say you already have main defined maybe in another .c or .cpp file? Do you have multiple files in this project?

Last edited on
My good lord!!! I feel so stupid!! I had earlier created a new .cpp file to try and test that link of code you send me! Problem is now solved thank you so much for bearing with a noob such as myself. Have a good life and thank you once again.
Topic archived. No new replies allowed.