function
Mar 1, 2015 at 4:21pm UTC
i can't get this function working
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
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
using namespace std;
int convert_digit(string roman);
int main()
{
string roman;
char goAgain;
do
{
cout << "Please enter the Roman Numeral to convert : " ;
cin >> roman;
convert_digit (string roman);
cout << "Roman Number " << roman << " equals " << sum <<endl;
cout << "Want to convert Roman Numeral again(Y or N)" ;
cin >> goAgain;
while (toupper(goAgain) !='Y' && toupper(goAgain) !='N' )
{
cout << "please enter Y or N: " ;
cin >> goAgain;
}
}while (toupper(goAgain) =='Y' );
return 0;
}
int convert_digit(string roman)
{
int length = roman.length();
int previous = 0;
bool error = false ;
int nIndex = 0;
int sum = 0;
while ( (error == false ) && (nIndex < length) )
{
switch (roman[nIndex])
{
case 'M' :
sum += 1000;
if (previous < 1000)
{
sum -= 2 * previous;
}
previous = 1000;
break ;
case 'D' :
sum += 500;
if (previous < 500)
{
sum -= 2 * previous;
}
previous = 500;
break ;
case 'C' :
sum += 100;
if (previous < 100)
{
sum -= 2 * previous;
}
previous = 100;
break ;
case 'L' :
sum += 50;
if (previous < 50)
{
sum -= 2 * previous;
}
previous = 50;
break ;
case 'X' :
sum += 10;
if (previous < 10)
{
sum -= 2 * previous;
}
previous = 10;
break ;
case 'V' :
sum += 5;
if (previous < 5)
{
sum -= 2 * previous;
}
previous = 5;
break ;
case 'I' :
sum += 1;
if (previous < 1)
{
sum -= 2 * previous;
}
previous = 1;
break ;
default :
cout << roman[nIndex] << " is not a Roman Numeral!" << endl;
error = true ;
sum = 0;
} // switch
nIndex++;
} // while
return sum;
}
Mar 1, 2015 at 4:30pm UTC
Please explain the "not working" in detail.
Mar 1, 2015 at 4:37pm UTC
int convert_digit is not converting roman numeral to numbers
Mar 1, 2015 at 5:57pm UTC
How can you know that? The code that you show does not even compile due to errors on lines 23 and 26.
Mar 1, 2015 at 10:06pm UTC
got convert_digit to work however i added a new function called check_format and an error: expected primary-expression before 'roman' on line 21. any advice
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
using namespace std;
int convert_digit(string roman);
bool check_format( string []);
int main()
{
string roman;
char goAgain;
do
{
cout << "Please enter the Roman Numeral to convert : " ;
cin >> roman;
if (check_format(string roman))
{
cout << "Roman Number " << roman << " equals " << convert_digit(roman) <<endl;
}
else
{
cout << "That is not the proper format for Roman Numerals" ;
}
cout << "Want to convert Roman Numeral again(Y or N)" ;
cin >> goAgain;
while (toupper(goAgain) !='Y' && toupper(goAgain) !='N' )
{
cout << "please enter Y or N: " ;
cin >> goAgain;
}
}while (toupper(goAgain) =='Y' );
return 0;
}
int convert_digit(string roman)
{
int length = roman.length();
int previous = 0;
bool error = false ;
int nIndex = 0;
int sum = 0;
while ( (error == false ) && (nIndex < length) )
{
switch (roman[nIndex])
{
case 'M' :
sum += 1000;
if (previous < 1000)
{
sum -= 2 * previous;
}
previous = 1000;
break ;
case 'D' :
sum += 500;
if (previous < 500)
{
sum -= 2 * previous;
}
previous = 500;
break ;
case 'C' :
sum += 100;
if (previous < 100)
{
sum -= 2 * previous;
}
previous = 100;
break ;
case 'L' :
sum += 50;
if (previous < 50)
{
sum -= 2 * previous;
}
previous = 50;
break ;
case 'X' :
sum += 10;
if (previous < 10)
{
sum -= 2 * previous;
}
previous = 10;
break ;
case 'V' :
sum += 5;
if (previous < 5)
{
sum -= 2 * previous;
}
previous = 5;
break ;
case 'I' :
sum += 1;
if (previous < 1)
{
sum -= 2 * previous;
}
previous = 1;
break ;
default :
cout << roman[nIndex] << " is not a Roman Numeral!" << endl;
error = true ;
sum = 0;
} // switch
nIndex++;
} // while
return sum;
}
bool check_format( string roman)
{
if (roman.find_first_of("IIII" ) !=string::npos)
{
return false ;
}
else if (roman.find_first_of("XXXX" ) !=string::npos)
{
return false ;
}
else if (roman.find_first_of("LLLL" ) !=string::npos)
{
return false ;
}
else if (roman.find_first_of("VVVV" ) !=string::npos)
{
return false ;
}
else if (roman.find_first_of("CCCC" ) !=string::npos)
{
return false ;
}
else if (roman.find_first_of("DDDD" ) !=string::npos)
{
return false ;
}
return true ;
}
Mar 1, 2015 at 10:32pm UTC
What do you have on line 21 before the word "roman" and why?
Compare also lines 8 and 127. That mismatch will be your next error.
Topic archived. No new replies allowed.