Issue with Output of function to convert decimal to roman numeral

Apr 13, 2017 at 4:31am
My progranm compiles but does not output the correct roman numeral. If under 100 it ouptuts a blank and if over 100 it outputs MMMMMMMM. A push in the write direction would be appreciated. Thank you.


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
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>



using namespace std;

string DecimalToRoman(int);

int main() {
	cout << "Lab 7 - Convert Decimal Number to Roman Numeral" << endl;
	cout << "----------------------------------------" << endl;
	
	int origNum;
	
	cout << "Decimal Value Too Convert To Roman Numeral: ";
	cin >> origNum;
	if (origNum < 0 || origNum > 4999) {
		cout << "Number must be between 1-4999 or 0 to End. "; 
		cin >> origNum;
	}
	
	while (origNum != 0) {
		string result = DecimalToRoman(origNum);
		cout << "" << result << " is the Roman Numeral equivalent of " << origNum << "." << endl;
		cout << "Decimal Value Too Convert To Roman Numeral: ";
		cin >> origNum; 	
	}
}

string DecimalToRoman(int) {

	int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
    string numerals[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
    string result = "";
    int origNum;
    
	for (int x = 0; values[x] > 0; ++x) {
        while (origNum >= values[x]) {
        	origNum -= values[x];
        	result += numerals[x];
        }
  
    return result;
    }   
 
}
Last edited on Apr 13, 2017 at 4:33am
Apr 13, 2017 at 4:41am
My progranm compiles ...


Not really, used cpp.sh (the gear icon top right of the code) with all 3 warning levels on.

 In function 'std::string DecimalToRoman(int)': 
49:1: warning: control reaches end of non-void function [-Wreturn-type] 
42:30: warning: 'origNum' may be used uninitialized in this function [-Wmaybe-uninitialized] 


Warnings are your friend, set them to a high level. :+)

Edit:

Provide names for the function parameters, not just the type: that might help to see where you went wrong :+)

Make things const where ever you can.

Does it work for negative values? Consider making the type unsigned.

Last edited on Apr 13, 2017 at 4:45am
Apr 13, 2017 at 4:45am
Oh... Okay. It compiled for me but my compiler did not display these warnings. What do those warnings mean? Sorry I am kind of new to this.


Edit:
Well i switched things around and it works. Thank you so much. One last Question. How do I limit the numbers from being less than 0 and more than 4999? Everything else compiles
Last edited on Apr 13, 2017 at 4:55am
Apr 13, 2017 at 4:54am
What do those warnings mean?


'origNum' may be used uninitialized

I thought that would be pretty clear :+) Also, I hinted that wasn't really the problem

control reaches end of non-void function [-Wreturn-type]

That should be quite obvious too, but possibly a little tricky to see why in your code. You have this

1
2
3
4
5
6
7
string function(int /* put a name here */) {
   for (/* ... */) {

   }
   // no return statement here
   // control reaches end of non void function
}



this would be easier to see if you were to indent lines 46 and 47 properly.
Last edited on Apr 13, 2017 at 4:55am
Apr 13, 2017 at 4:59am
It compiled for me but my compiler did not display these warnings.


What compiler / IDE are you using?

Try to find out what the equivalent of these warnings in g++ / clang++ , in your compiler, are :

g++ -std=c++14 -Wall -Wextra -pedantic-errors *.cpp -o ExeFileName
Topic archived. No new replies allowed.