Please help with this probleme

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)

The answers just keep stacking...

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <string>
#include <windows.h>
#include <cmath>
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.