CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 20

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

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