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

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

Hybrid View

  1. #1
    Join Date
    Jun 2013
    Posts
    22

    Question Roman Numeral Converter to Decimal Using an Array of Strings

    This program compiles, but has a bunch of logical errors. I know my problem is somewhere in the while loop that I have, but I can't figure out where.

    Here are some of the issues I am experiencing:

    1. At the beginning of the program it asks you to enter a number, and when you do it does nothing while proceeding to the while loop where I have it asking the same question

    Code:
    "Please enter a number between 1 and 20 (Enter 0 to stop)\n";
    	cin >> num;
    	cout << endl;
    I want to be able to eliminate that first statement but if I only run this in the loop without the above statement the program will display nothing on the screen and proceeds to stop.

    2. This code runs fine, except that if you make a mistake, it will prompt you to enter a valid number, however; it ignores your first response if the number you enter is valid and asks you to enter a valid number anyway. Once you enter it a second time, it will accept it and the program will continue on.

    Code:
    while(num != SENTINEL)
    	{
    		cout << "Please enter a number between 1 and 20 (Enter 0 to stop) ";
    		cin >> num;
    		cout << endl;
    Also if you type in 0 on your first response, it will prompt you that it is not a valid number and ask you to try again, instead of stopping the program like it is supposed to do. On your second response the program will accept your 0 and stop the program correctly.

    Here is my code:

    Code:
    // Write a program that displays the roman numeral equivalent
    // of any decimal number between 1 and 20 that the user enters.
    // The roman numerals should be stored in an array of strings
    // and the decimal number that the user enters should be used to 
    // locate the array element holding the roman numeral equivalent.
    // The program should have a loop that allows the user to continue
    // entering numbers until an end sentinel of 0 is entered.
    // Input validation: Do not accept scores less than 0 or greater than 20
    
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	// Declare constants and variables
    
    	
    	const int romanNum = 21;  // Size of the elements in the array
    
    	// 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"};
    	const int SENTINEL = 0;	 // variable stays 0 during entire program
    	int num = 0;			 // variable to store user input, intialized to 0														
    																		 
    	cout << "Please enter a number between 1 and 20 (Enter 0 to stop)\n";
    	cin >> num;
    	cout << endl;
    
    	// Loop comparing user input to roman numerals in the array. If user enters 0, the program stops.
    	while(num != SENTINEL)
    	{
    		cout << "Please enter a number between 1 and 20 (Enter 0 to stop) ";
    		cin >> num;
    		cout << endl;
    		
    		// Conditional statement comparing any number entered that is greater than 0 and less than or equal to 20
    		if(num > 0 && num <= 20)
    		{
    			// Displays user input
    			cout << num << " is equivalent to " << roman[num] << endl;   
    		}
    
    		else
    		{
    			// Input validation if a number entered is less than 0 or greater than 20
    			cout << "Invalid entry\n";
    			cout << "Please enter a number between 1 and 20 (Enter 0 to stop) ";
    			cin >> num;
    			cout << endl;
    		}
    	
    	}
    
    	system("pause");
    	return 0;
    }
    Last edited by veryNew; June 27th, 2013 at 10:30 PM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,444

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

    This program compiles, but has a bunch of logical errors. I know my problem is somewhere in the while loop that I have, but I can't figure out where.
    Then it's time to learn to use the debugger that comes with the compiler. Have you used the debugger?

    Also:
    Code:
    while(num != SENTINEL)
    	{
    		cout << "Please enter a number between 1 and 20 (Enter 0 to stop) ";
    		cin >> num;
    		cout << endl;
    		
    		// Conditional statement comparing any number entered that is greater than 0 and less than or equal to 20
    		if(num > 0 && num <= 20)
    		{
    			// Displays user input
    			cout << num << " is equivalent to " << roman[num] << endl;   
    		}
    
    		else
    		{
    			// Input validation if a number entered is less than 0 or greater than 20
    			cout << "Invalid entry\n";
    			cout << "Please enter a number between 1 and 20 (Enter 0 to stop) ";
    			cin >> num;
    			cout << endl;
    
    		}
    Why are you performing the same steps in red twice in the loop? This is the form that the code should take:
    Code:
    ask for number;
    while (number is not quit)
    {
       if number is good
           convert number to roman numeral;
      else
           output that number is bad;
      ask for number;  
    }
    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; June 27th, 2013 at 10:42 PM.

  3. #3
    Join Date
    Jun 2013
    Posts
    22

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

    Quote Originally Posted by Paul McKenzie View Post
    Then it's time to learn to use the debugger that comes with the compiler. Have you used the debugger?
    My compiler does not come with a debugger.

    Quote Originally Posted by Paul McKenzie View Post
    Also:
    Code:
    while(num != SENTINEL)
    	{
    		cout << "Please enter a number between 1 and 20 (Enter 0 to stop) ";
    		cin >> num;
    		cout << endl;
    		
    		// Conditional statement comparing any number entered that is greater than 0 and less than or equal to 20
    		if(num > 0 && num <= 20)
    		{
    			// Displays user input
    			cout << num << " is equivalent to " << roman[num] << endl;   
    		}
    
    		else
    		{
    			// Input validation if a number entered is less than 0 or greater than 20
    			cout << "Invalid entry\n";
    			cout << "Please enter a number between 1 and 20 (Enter 0 to stop) ";
    			cin >> num;
    			cout << endl;
    
    		}
    Why are you performing the same steps in red twice in the loop?

    The reason it is being repeated is because the "else" statement is the input validation if the user accidentally inputs an invalid number. The compiler prompts the user to try again and I just have it giving out the same instructions as before.




    Quote Originally Posted by Paul McKenzie View Post

    This is the form that the code should take:
    Code:
    ask for number;
    while (number is not quit)
    {
       if number is good
           convert number to roman numeral;
      else
           output that number is bad;
      ask for number;  
    }
    Regards,

    Paul McKenzie
    But, isn't that what I am doing here?

    Code:
    // Conditional statement comparing any number entered that is greater than 0 and less than or equal to 20
    		if(num > 0 && num <= 20)
    		{
    			// Displays user input
    			cout << num << " is equivalent to " << roman[num] << endl;   
    		}
    
    		else
    		{
    			// Input validation if a number entered is less than 0 or greater than 20
    			cout << "Invalid entry\n";
    			cout << "Please enter a number between 1 and 20 (Enter 0 to stop) ";
    			cin >> num;
    			cout << endl;
    		}

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,529

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

    Quote Originally Posted by veryNew View Post
    My compiler does not come with a debugger.

    What type of compiler are you using? What type of IDE?
    Victor Nijegorodov

  5. #5
    Join Date
    Jun 2013
    Posts
    22

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

    Quote Originally Posted by VictorN View Post

    What type of compiler are you using? What type of IDE?
    I am using a program called: "My Code Mate". It only has a compiler, but includes many projects that you can practice with, that's why I like to use it.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,444

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

    Quote Originally Posted by veryNew View Post
    My compiler does not come with a debugger.
    This is the Visual C++ forum, so given that, the compiler must come with a debugger. If not, then

    1) You're in the wrong forum and
    2) You need to get a better build system.

    For 1) you should post in the Non-Visual C++ forum.

    For 2) every compiler has to come with some sort of debugging module, else it will be difficult, if not impossible to write anything bigger than toy programs. Also, if you are a student and/or this is homework, it is considered cheating if you are asking an expert or professional programmer to debug programs instead of you doing so. So it is in your best interests to find and use a C++ build system, whether it's Visual C++, CodeBlocks using g++ or some other reputable C++ environment, that comes with a credible debugger.
    But, isn't that what I am doing here?
    No.

    Look at my pseudo-code carefully. The version I have initially asks for input outside the loop before it's entered. Then I enter the loop if the input is good and then process it. Then at the bottom of the loop, the question is asked again, thus taking you to the top of the loop with the check for quit. Then if the number if good, process it, else, output the number is bad and input again.

    So my version asks exactly once within the loop.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; June 28th, 2013 at 03:36 PM.

  7. #7
    Join Date
    Jun 2013
    Posts
    22

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

    Quote Originally Posted by Paul McKenzie View Post
    This is the Visual C++ forum, so given that, the compiler must come with a debugger. If not, then

    1) You're in the wrong forum and
    2) You need to get a better build system.
    I have Visual C++, but because the program that I use comes with its own compiler then I chose to use that. If that upsets you in any way, or any other moderator that posts here, then I apologize and will go back to using Visual, its' that simple.

    As it relates to Visual, I have never used the debugger before. I am very new, and I am learning this on my own, so I had no idea about it.

    Quote Originally Posted by Paul McKenzie View Post
    Also, if you are a student and/or this is homework, it is considered cheating if you are asking an expert or professional programmer to debug programs instead of you doing so.
    I am not a student, therefore your assumption that I am cheating is off base. I am learning on my own and I don't remember anywhere in my post where I specifically asked anyone reading it to debug the program for me. I simply asked a few questions...and I provided some code to go along with it as it relates to what I have learned so far.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,444

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

    Quote Originally Posted by veryNew View Post
    I have Visual C++, but because the program that I use comes with its own compiler then I chose to use that. If that upsets you in any way,
    No one is "getting upset".

    This forum is called "Visual C++ forum" for a reason. The posts here are geared toward the Microsoft Visual C++ compiler, not for any other compiler. Given that, every version of Visual C++ comes with a debugger, which is why it is impossible for the Visual C++ compiler to not come with a debugger. The other issue is that you may be using a compiler that isn't ANSI compliant, causing confusion when we give you a solution, but the solution either doesn't work or doesn't compile on your compiler that you are using that isn't Visual C++.

    That's why there is a Non_Visual C++ forum for such posts. In that forum, questions that do not concern the Visual C++ compiler are asked.
    As it relates to Visual, I have never used the debugger before. I am very new, and I am learning this on my own, so I had no idea about it.
    When you build a program, there is a "Debug" menu. If you hit F10 a few times after you get a successful build, you will see what the debugger does. You will be able to step through your code, watch variables, etc.
    I am not a student, therefore your assumption that I am cheating is off base.
    Did you see the if in my quote?

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,746

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

    The reason it is being repeated is because the "else" statement is the input validation if the user accidentally inputs an invalid number. The compiler prompts the user to try again and I just have it giving out the same instructions as before.
    You only need to have the statements

    Code:
    cout << "Please enter a number between 1 and 20 (Enter 0 to stop) ";
    cin >> num;
    cout << endl;
    once in the program - even dealing with invalid input and keep asking the user to input until 0 is entered. Look at how the loop can be changed to just have these statements once. Also, what happens if a user inputs say an x instead of a number?
    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.

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,204

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

    As Paul mentioned, the debugger will show you pretty quickly what the problem is. In your case it is here
    Code:
    	const int SENTINEL = 0;	 // variable stays 0 during entire program
    	int num = 0;			 // variable to store user input, intialized to 
    	while(num != SENTINEL)
    See what happens if you remove the first prompt. You have two choices. Either use a do/while loop, which is the better approach, or initialize num so that it doesn't equal SENTINEL.

    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.

  11. #11
    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.

  12. #12
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,746

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

    I am not a student, therefore your assumption that I am cheating is off base. I am learning on my own
    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;
    }
    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.

  13. #13
    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.

  14. #14
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,746

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

    To use the debugger, you need either to be logged on as an administrator or the user needs to be a member of the 'Debugger Users' group. Also, the solution needs to be configured to produce a debug build (which contains the debugging info) rather than a release build which doesn't.
    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.

  15. #15
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,746

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

    I am learning on my own
    What book are you using?

    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++
    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 1 of 2 12 LastLast

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center