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
  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 09:30 PM.

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

    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 09: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
    Hanover Germany
    Posts
    20,395

    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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. 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)

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    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 02:36 PM.

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

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

    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.

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. 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)

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449

    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

  12. #12
    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
    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.
    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.
    You are right. It was an oversight on my part of where I posted and I should have posted on the Non-Visual C++ threads and for that I apologize.

    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?

  13. #13
    Join Date
    Apr 1999
    Posts
    27,449

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

    Quote Originally Posted by veryNew View Post
    You are right. It was an oversight on my part of where I posted and I should have posted on the Non-Visual C++ threads and for that I apologize.
    No problem.
    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?
    Basically, my advice is that you need very little if any documentation. Only when you start to really get involved with it do you need to read up on some of what the debugger does.

    Build a simple application (the one you're using now is an example). Then when it's built successfully, hit F10. You will see an arrow pointing to the first exectuable line of code. Then hit F10 again -- that arrow moves to the next line. Then hit F10 again -- the arrow moves again. Then keep hitting F10 until you get to the loop. You will see the arrow jump up to the top of the loop, jump over an if() statement that returns false, etc.

    Basically what you're doing is running your program in slow motion, under your control, instead of having it zoom by to completion.

    While hitting F10, you should see some other windows open up, where you see the variables you have declared and their current values. Note that they change when you execute a line that changes a variable.

    So that is a quick introduction. You then will learn how to set "breakpoints", where you run the program at full speed under the debugger until it hits the line where you've set the breakpoint. Then basically, things snowball from there, where you're picking up information on how to use the debugger more economically, smartly, etc. (it isn't just about hitting F10, but that is how I introduce anyone to the debugger).

    Regards,

    Paul McKenzie

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. 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)

  15. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. 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)

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
  •  





Click Here to Expand Forum to Full Width

Featured