Apr 25, 2014 at 10:17pm UTC
So I'm trying to make this code work, and so far the code I have, no matter what I enter I receive my debug error code: Error: Invalid characters encountered
This happens even when it is an m, d, x, v, i, l, etc. What am I doing wrong?
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 171 172
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
char num[10];
int sum = 0, i = 0;
cout << "Enter a Roman numeral: " ; //decide on a limit? Or an equation that should work it all out?
cin >> num[5];
for (int i = 0; i < 10; i++)//Sum
{
num[i] = tolower(num[i]);
switch (num[i])
{
case 'm' : sum += 1000; break ;
case 'd' : sum += 500; break ;
case 'c' : sum += 100; break ;
case 'l' : sum += 50; break ;
case 'x' : sum += 10; break ;
case 'v' : sum += 5; break ;
case 'i' :
if (num[i + 1] == 'v' )
{
sum -= 1;
}
else if (num[i + 1] == 'x' )
{
sum -= 1;
}
else if (num[i + 1] == 'l' )
{
sum -= 1;
}
else if (num[i + 1] == 'd' )
{
sum -= 1;
}
else if (num[i + 1] == 'c' )
{
sum -= 1;
}
else if (num[i + 1] == 'm' )
{
sum -= 1;
}
else if (num[i + 1] == 'i' )
{
sum += 1;
}
else if (num[i - 1] == 'i' )
{
sum += 1;
}
else if (num[i - 1] == 'v' )
{
sum += 1;
}
else if (num[i - 1] == 'x' )
{
sum += 1;
}
else if (num[i - 1] == 'c' )
{
sum += 1;
}
else if (num[i - 1] == 'm' )
{
sum += 1;
}
else if (num[i - 1] == 'd' )
{
sum += 1;
}
else if (num[i - 1] == 'l' )
{
sum += 1;
}
break ;
default :
cout << "Error: Invalid characters encountered." << endl;
int error = 1;
sum = 0;
while (error != 0)
{
cout << "Enter a roman numeral:\t" ;
cin >> num;
for (int i = 0; i < 5; i++)//Sum
{
num[i] = tolower(num[i]);
switch (num[i])
{
case 'm' : sum += 1000; break ;
case 'd' : sum += 500; break ;
case 'c' : sum += 100; break ;
case 'l' : sum += 50; break ;
case 'x' : sum += 10; break ;
case 'v' : sum += 5; break ;
case 'i' :
if (num[i + 1] == 'v' )
{
sum -= 1;
}
else if (num[i + 1] == 'x' )
{
sum -= 1;
}
else if (num[i + 1] == 'l' )
{
sum -= 1;
}
else if (num[i + 1] == 'd' )
{
sum -= 1;
}
else if (num[i + 1] == 'c' )
{
sum -= 1;
}
else if (num[i + 1] == 'm' )
{
sum -= 1;
}
else if (num[i + 1] == 'i' )
{
sum += 1;
}
else if (num[i - 1] == 'i' )
{
sum += 1;
}
else if (num[i - 1] == 'v' )
{
sum += 1;
}
else if (num[i - 1] == 'x' )
{
sum += 1;
}
else if (num[i - 1] == 'c' )
{
sum += 1;
}
else if (num[i - 1] == 'm' )
{
sum += 1;
}
else if (num[i - 1] == 'd' )
{
sum += 1;
}
else if (num[i - 1] == 'l' )
{
sum += 1;
}
break ;
default :
cout << "Error: Invalid characters encountered." << endl;
int error = 1;
sum = 0;
}//end switch
}//end for
}//end while
}//end for
}//end while
cout << endl;
cout << "The Roman numeral, " << num << " translates to Arabic number, " << sum << "." << endl;
return 0;
}
Last edited on Apr 25, 2014 at 10:18pm UTC
Apr 25, 2014 at 10:58pm UTC
Oops! I changed the number of characters from 5 to 10. Forgot to change that one...I'm not actually even sure if I can just say
cin >> num;
of if it has to be num[10];
Apr 25, 2014 at 11:18pm UTC
Since num is an array of char, num[index] is a single char. So, when you do cin >> num[index] regardless of what index is, you are asking for a single character to be extracted from cin .
Valid indexes for an array if size x are 0 to x -1. If you were to write cin >> num[10]; and num only contained 10 elements, you would be accessing an element of num that did not exist.
I would recommend the use of cin.getline here.
Last edited on Apr 25, 2014 at 11:19pm UTC
Apr 26, 2014 at 12:31am UTC
1 2 3 4 5
const unsigned num_size = 10 ;
char num[num_size];
cout << "Enter a Roman numeral: " ;
cin.getline(num, num_size);
...
Last edited on Apr 26, 2014 at 12:31am UTC