Roman Numeral Converter to Decimal Using an Array of Strings - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20

Thread: Roman Numeral Converter to Decimal Using an Array of Strings

  1. #16
    Join Date
    Jun 2013
    Posts
    22

    Re: Roman Numeral Converter to Decimal Using an Array of Strings

    Quote Originally Posted by 2kaud View Post
    What book are you using?
    "Starting Out With C++ Early Objects" by Tony Gaddis

    Quote Originally Posted by 2kaud View Post
    You might like to look at these web sites

    http://www.learncpp.com/
    http://www.cplusplus.com/doc/tutorial/

    Welcome to the world of c++
    Thank you, I will definitely check them out.

  2. #17
    Join Date
    Jun 2013
    Posts
    22

    Re: Roman Numeral Converter to Decimal Using an Array of Strings

    Quote Originally Posted by GCDEF View Post

    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.

  3. #18
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,962

    Re: Roman Numeral Converter to Decimal Using an Array of Strings

    Quote Originally Posted by veryNew View Post
    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

  4. #19
    Join Date
    Jun 2013
    Posts
    22

    Question Re: Roman Numeral Converter to Decimal Using an Array of Strings

    Quote Originally Posted by 2kaud View Post
    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.

  5. #20
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,335

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center