-
June 28th, 2013, 06:02 PM
#16
Re: Roman Numeral Converter to Decimal Using an Array of Strings
Originally Posted by 2kaud
What book are you using?
"Starting Out With C++ Early Objects" by Tony Gaddis
Originally Posted by 2kaud
Thank you, I will definitely check them out.
-
June 28th, 2013, 06:06 PM
#17
Re: Roman Numeral Converter to Decimal Using an Array of Strings
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.
-
June 29th, 2013, 02:00 AM
#18
Re: Roman Numeral Converter to Decimal Using an Array of Strings
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
Best regards,
Igor
-
June 30th, 2013, 07:52 PM
#19
Re: Roman Numeral Converter to Decimal Using an Array of Strings
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.
-
July 1st, 2013, 05:32 AM
#20
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. Perhaps num should be initialised as SENTINEL + 1.
Code:
int num = SENTINAL + 1;
Happy coding
Last edited by 2kaud; July 1st, 2013 at 05:39 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|