Re: Roman Numeral Converter to Decimal Using an Array of Strings
Quote:
Originally Posted by
2kaud
What book are you using?
"Starting Out With C++ Early Objects" by Tony Gaddis
Quote:
Originally Posted by
2kaud
Thank you, I will definitely check them out.
Re: Roman Numeral Converter to Decimal Using an Array of Strings
Quote:
Originally Posted by
GCDEF
This forum is only for people using Visual C++. If you're using a different environment, you should post in the generic C++ forum, but any development environment that doesn't include a debugger is completely useless. If that's really the case, you should stop using it immediately.
Yes, Paul was right. It was an oversight on my part, I should have posted on the other generic thread. I apologize to anyone reading here on that misjudgment.
Re: Roman Numeral Converter to Decimal Using an Array of Strings
Quote:
Originally Posted by
veryNew
I have never used the debugger before, but I would like to learn how. Are there any good tutorials online that explain how to use the debugger?
http://www.cprogramming.com/debuggers.html
Re: Roman Numeral Converter to Decimal Using an Array of Strings
:confused:
Quote:
Originally Posted by
2kaud
Have a look at this
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
const int romanNum = 21; // Size of the elements in the array
const int SENTINEL = 0; // variable stays 0 during entire program
int num = 1; // variable to store user input, initialized to non-zero for error test
// Roman numerals stored in an array
string roman[romanNum] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X",
"XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XIII", "XIX", "XX"};
do {
cout << "Please enter a number between 1 and " << romanNum - 1 << " (Enter 0 to stop): ";
if (!(cin >> num)) {
cout << "Entry is not a number" << endl;
cin.clear();
cin.ignore(100, '\n');
} else
if (num > 0 && num < romanNum)
cout << num << " is equivalent to " << roman[num] << endl;
else
if (num != SENTINEL)
cout << "Invalid number\n";
} while (num != SENTINEL);
return 0;
}
This code was really good, and everything made sense to me. I especially liked where you integrated the input statement in your if conditional statement:
Code:
if (!(cin >> num)) {
I have never seen that before and I have always used it as a separate statement after the cout instruction. That just really caught my attention that's all.
The only part that was confusing for me is where you initialized num to 1 here:
Code:
int num = 1; // variable to store user input, initialized to non-zero for error test
Shouldn't this be initialized to zero? I debugged the program and everything works fine in what you did, I am just simply asking why you initialized num to 1.
Re: Roman Numeral Converter to Decimal Using an Array of Strings
If for the first input a non-numeric value is entered, then (cin >> num) is false so it prints 'Entry is not a number'. But num has not been set so has the last value (the one to which it was first initialised). If num was initialised to 0 (ie the sentinel value) then the do while condition would be false and the program ends! Initialising num to any value other than the sentinel value ensures that the loop doesn't terminate first time round if a non-numeric value is entered.:cool: Perhaps num should be initialised as SENTINEL + 1.
Code:
int num = SENTINAL + 1;
Happy coding:)