Thread: string problem

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    18

    Exclamation string problem

    i have an assignment which basically takes whatever the users inputs, and outputs and translates it to Roman numerals. numbers----->Roman numerals

    here is the link that explains everything in more detail
    http://home.earthlink.net/%7Ecraie/1.../romannum.html

    i understand the pattern but i'm stuck on how to reduce the code

    this is what i got so far

    Code:
    [tag]#include <string>
    #include <iostream>
    using namespace std;
    int main()
    {
    	string rome;
    	string rome1="I";
    	string rome2=rome1+rome1;
    	string rome3=rome2+rome1;
    	string rome4="IV";
    	string rome5="V";
    	string rome6=rome5+rome1;
    	string rome7=rome6+rome1;
    	string rome8=rome7+rome3;
    	string rome9="IX";
    	string rome10="X";
    
    return 0;
    }
    [/tag]
    what i'm basically doing now is taking different Roman numerals and adding them together to get new ones. but, what i discovered that if i keep this kind of pace up, it will take me a LONG time to finish! no 0 in Roman numerals; and the limit is 3999. can anyone help me out to get started the right way to write out the code for the Roman numeral pattern? also, "Why will the program only work for values in the (integral) range [1..3999]? "thanks in advance

    btw, only strings,loops,and nest/branches are to be used in this program

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Can you describe in words your approach to say outputting the Roman version of 1984 ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    Quote Originally Posted by Salem View Post
    Can you describe in words your approach to say outputting the Roman version of 1984 ?
    1000 + 100*9 + (80+4)

    M +LLLLLLLLL +LXXXIV= MLLLLLLLLLLXXXIV? 1984
    or?
    |
    |
    \/
    M+IXC (900)+ XXXIV =MIXCLXXXIV? i think this is the correct form

    translated:
    separate each Roman numeral by the thousands,hundreds,etc.
    combine all the numerals to make a new numeral

    kinda
    Last edited by INeedSleep; 10-26-2007 at 02:16 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    But the correct answer is MCMLXXXIV

    The first step is isolate the powers of 10, this gives you 'M'(1000) 'C'(100) 'X'(10) and 'I'(1), which are repeated as necessary (eg XXX).

    Then the fives are used to prevent long runs of the 10's, namely 'D'(500), 'L'(50), 'V'(5).

    Finally, the fours and nines are written as either one-before-five or one-before-ten, so 4 for example is written as 'IV' (one before five), not as 'IIII', and 40 would be XL (ten before 50).

    In 1984, we have
    one thousand - M
    nine hundred - CM (in other words 100 before 1000)
    eighty - LXXX (50 and 30 added together)
    four - IV (one before five)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    Heres an update!
    I got the basic formulas to get the remainders from the thousands place, tens place etc.
    the program prints out a "I", "II" and a "III" with no problem.
    But i need help on what code to put for the condition of(the first if else statement), n==4 || n==9;and to repeat the same process for the tens place, hundreds place, etc. Also, for 5,6,7, and 8, individual "I" are printed out ;and it doesn't print out the correct roman numerals. Another problem i am having is getting the conversion of the D,M and C into 1,4 and 9. thanks in advance.

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    int main()
    
    {
    
    string roman;
    short i,n;
    short input; //user input
    cout<<" #: \n";
    cin>>input;
    
    //string rome;//conversion from user input
    
    //string names converted to roman numerals
    
    string rome1="I";
    string rome2="II";
    string rome3="III";
    string rome4="IV";
    string rome5="V";
    string rome6="VI";
    string rome7="VII";
    string rome8="VIII";
    string rome9="IX";
    string rome10="X";
    string rome50="L";
    string rome100="C";
    string rome500="D";
    string rome1000="M";
    
    //while(input> 0 && input < 4000)//while input is less than 0 and input
    //is less than 4000 
      
      
       i=1;
       n=input/1000;
       
       while(i<=n)
       {
       roman=roman+rome1000;
       i=i+1;
       }
         
       
       
       i=1;
       n=input%10;
       if (n == 4 || n == 9)
       {
       
       
       // handle 4 and 9 separately
         
       }
       else
       {
       // handle 5 ..and start 6,7,8
       while(i<=n)
       {
       roman=roman+rome1;
       i=i+1;
       }
       }
       
       
       
       
    	
          if((cin.peek() != '\n'))//if input is greater than or equal to 1000
          {
            
          }
         
       
    	
         //trying to find the thousands spot
    //the input cannot exceed 4000 and cannot be 4000 also input cannot be lower
    //1000
           else if( (input<4000 && input != 4000) && !(input < 1000) )
           {
           input=input/1000%10;//isolate the digit in the thousands place
           //cout<<input;
           }
          
           //trying to find the hundreds spot
    //the input cannot exceed 1000 and be less than 100
           else if(input<1000 && input>=100)
           {
           input=input%1000/100;
           //cout<<input;
           }
           
           else if(input<100 && input>=10)
           {
           input=input%100/10;
           //cout<<input;
           }
           
           else if(input<10 && input>0 && input>=1)
           {
           input=input%10;
           //cout<<input;
           }
           
           
           
           else
          {
           cout<<"nada";
          }
      
        
          
         
       cout<<roman;
     
    return 0;
    }

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by INeedSleep View Post

    Code:
    //string names converted to roman numerals
    
    string rome1="I";
    string rome2="II";
    string rome3="III";
    string rome4="IV";
    string rome5="V";
    string rome6="VI";
    string rome7="VII";
    string rome8="VIII";
    string rome9="IX";
    string rome10="X";
    string rome50="L";
    string rome100="C";
    string rome500="D";
    string rome1000="M";
    These should all be const, since you don't want to ever modify them. Also, it will help you to make an array of these so that you have similar numerals in an array together. So do something like this:
    Code:
    const char numerals[2][4]={
        {'I','X','C','M'},
        {'V','L','D','\0'} }

    Code:
       i=1;
       n=input&#37;10;
       if (n == 4 || n == 9)
       {
       
       
       // handle 4 and 9 separately
         
       }
       else
       {
       // handle 5 ..and start 6,7,8
       while(i<=n)
       {
       roman=roman+rome1;
       i=i+1;
       }
       }
    Instead of using rome1, put this in a loop. For each digit, print the corresponding roman numeral value. This is where having the above mentioned array helps.

    Code:
    	
          if((cin.peek() != '\n'))//if input is greater than or equal to 1000
          {
            
          }
    I don't know what This is supposed to do, but you already handled the 1000s place.



    Code:
    	
         //trying to find the thousands spot
    //the input cannot exceed 4000 and cannot be 4000 also input cannot be lower
    //1000
           else if( (input<4000 && input != 4000) && !(input < 1000) )
           {
           input=input/1000%10;//isolate the digit in the thousands place
           //cout<<input;
           }
          
           //trying to find the hundreds spot
    //the input cannot exceed 1000 and be less than 100
           else if(input<1000 && input>=100)
           {
           input=input%1000/100;
           //cout<<input;
           }
           
           else if(input<100 && input>=10)
           {
           input=input%100/10;
           //cout<<input;
           }
           
           else if(input<10 && input>0 && input>=1)
           {
           input=input%10;
           //cout<<input;
           }
           
           
           
           else
          {
           cout<<"nada";
          }
    You don't need all this. Handle each decimal place in a single loop.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    Quote Originally Posted by King Mir View Post
    These should all be const, since you don't want to ever modify them. Also, it will help you to make an array of these so that you have similar numerals in an array together.

    the professor wont let us use arrays for this program though

    Code:
       i=1;
       n=input%10;
       if (n == 4 || n == 9)
       {
       
       
       // handle 4 and 9 separately
         
       }
       else
       {
       // handle 5 ..and start 6,7,8
       while(i<=n)
       {
       roman=roman+rome1;
       i=i+1;
       }
       }
    Instead of using rome1, put this in a loop. For each digit, print the corresponding roman numeral value. This is where having the above mentioned array helps.

    how can i get the same results without using arrays?




    Code:
    	
         //trying to find the thousands spot
    //the input cannot exceed 4000 and cannot be 4000 also input cannot be lower
    //1000
           else if( (input<4000 && input != 4000) && !(input < 1000) )
           {
           input=input/1000%10;//isolate the digit in the thousands place
           //cout<<input;
           }
          
           //trying to find the hundreds spot
    //the input cannot exceed 1000 and be less than 100
           else if(input<1000 && input>=100)
           {
           input=input%1000/100;
           //cout<<input;
           }
           
           else if(input<100 && input>=10)
           {
           input=input%100/10;
           //cout<<input;
           }
           
           else if(input<10 && input>0 && input>=1)
           {
           input=input%10;
           //cout<<input;
           }
           
           
           
           else
          {
           cout<<"nada";
          }
    You don't need all this. Handle each decimal place in a single loop.
    i was going to put in a loop after i get the entire code down right

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    @INeedSleep
    Does he allow you to use vectors?
    If he does use that.

    If not, you can use switches. Possibly wrapped in a function call. But that's just emulating an array with a function, so in the normal case you'd still use an array.
    Last edited by King Mir; 11-05-2007 at 05:51 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    Quote Originally Posted by King Mir View Post
    @INeedSleep
    Does he allow you to use vectors?
    If he does use that.
    he's supposed go over vectors later in the semester(he's finally catching up with grading everything from along time ago)

    If not, you can use switches. Possibly wrapped in a function call. But that's just emulating an array with a function, so in the normal case you'd still use an array.
    we're going over switches maybe sometime in the next couple of weeks.
    all in all there is no avoidance of using vectors of arrays in this program?

    btw, the only problems i'm having now are

    1)*cant get "L"(50) or "D"(500) to print
    2)*cant print out anything that is supposed to stop at "III"
    ie, 5 prints out as IIIII
    3)*roman numerals appear after anything>=4000)
    4)*"nada" prints out for anything>=4000*/

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by INeedSleep View Post
    all in all there is no avoidance of using vectors of arrays in this program?
    In order to loop "for each digit" you need to store the characters that correspond to that digit in some sequential medium. Otherwise, you have to repeat the code for three times.

    I suppose It is also possible to use a macro, but that's a horrible practice for a situation that should properly be solved with a container.

    1)*cant get "L"(50) or "D"(500) to print
    It's the same as printing V. If you don't use a loop you should still have identical code for

    2)*cant print out anything that is supposed to stop at "III"
    ie, 5 prints out as IIIII

    there are several cases for each digit:
    0-3 print as a sequence of I
    4 prints as IV
    5-8 prints as V followed by several I's
    9 prints as IX

    For other digits, replace I, V, and X with there proper values for that digit (XLC and CDM).

    3)*roman numerals appear after anything>=4000)
    4)*"nada" prints out for anything>=4000*/

    You need to properly reprompt for when there is invalid input.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    Quote Originally Posted by King Mir View Post
    In order to loop "for each digit" you need to store the characters that correspond to that digit in some sequential medium. Otherwise, you have to repeat the code for three times.

    I suppose It is also possible to use a macro, but that's a horrible practice for a situation that should properly be solved with a container.

    1)*cant get "L"(50) or "D"(500) to print
    It's the same as printing V. If you don't use a loop you should still have identical code for

    2)*cant print out anything that is supposed to stop at "III"
    ie, 5 prints out as IIIII

    there are several cases for each digit:
    0-3 print as a sequence of I
    4 prints as IV
    5-8 prints as V followed by several I's
    9 prints as IX

    For other digits, replace I, V, and X with there proper values for that digit (XLC and CDM).

    3)*roman numerals appear after anything>=4000)
    4)*"nada" prints out for anything>=4000*/

    You need to properly reprompt for when there is invalid input.
    4)how should i properly reprompt?

    btw, i have another problem
    numbers such as 1999 and 2999 wont print out accordinly.
    how should i go to correct this?

    oh yea here is the updated code:

    Code:
    //updated 11/5/07
    
    #include <string>
    #include <iostream>
    using namespace std;
    int main()
    
    {
    
    string roman;
    short i,n;
    short input; //user input
    cout<<" #: \n";
    cin>>input;
    
    //string rome;//conversion from user input
    
    //string names converted to roman numerals
    
    const string rome1="I";
    const string rome2="II";
    const string rome3="III";
    const string rome4="IV";
    const string rome5="V";
    const string rome6="VI";
    const string rome7="VII";
    const string rome8="VIII";
    const string rome9="IX";
    const string rome10="X";
    const string rome50="L";
    const string rome100="C";
    const string rome500="D";
    const string rome1000="M";
    
    
    
    
       //thousands place to get remainder(1000-3000)
       
       n=input/1000;
       i=1;
       while(i<=n)
       {
       roman=roman+rome1000;
       i=i+1;
       }
        
       
       //hundreds place to get remainder(100-300)
       n=input%1000/100;
       if( n == 4 || n == 9)
       {
       if(n==4)
       roman=rome100+rome500;
       if(n==9)
       roman=rome100+rome1000;
       }
       else
       {
       if(n>=5)
       {
       roman=roman+rome500;
       n=n-5;
       }
       i=1;
       while(i<=n)
       {
       roman=roman+rome100;
       i=i+1;
       }
       
       }
       
       //tens place
       n=input%100/10;
       if( n == 4 || n == 9)
       {
       if(n==4)
       roman=rome10+rome50;
       if(n==9)
       roman=rome10+rome100;
       }
       else
       {
       if(n>=5)
       {
       roman=roman+rome50;
       n=n-5;
       }
       i=1;
       while(i<=n)
       {
       roman=roman+rome10;
       i=i+1;
       }
       
       }
       
       
       //ones place
       n=input%10;
       if (n == 4 || n == 9)
       {
       if(n==4)
       roman=roman+rome4;
       if(n==9)
       roman=roman+rome9;
       // handle 4 and 9 separately
       }
       else
       {
       // handle 5 ..and start 6,7,8
       if(n>=5)
       {
       roman=roman+rome5;
       n=n-5;
       }
       i=1;
       while(i<=n)
       {
       roman=roman+rome1;
       i=i+1;
       }
       
       }
       
       
       
       
    	
          if( (input<4000 && input != 4000) && !(input < 1000) )
          {
            input=input/1000%10;//isolate the digit in the thousands place
          }
         
      
         //trying to find the thousands spot
    //the input cannot exceed 4000 and cannot be 4000 also input cannot be lower
    //1000
           
          
           //trying to find the hundreds spot
    //the input cannot exceed 1000 and be less than 100
           else if(input<1000 && input>=100)
           {
           input=input%1000/100;
           //cout<<input;
           }
           
           else if(input<100 && input>=10)
           {
           input=input%100/10;
           //cout<<input;
           }
           
           else if(input<10 && input>0 && input>=1)
           {
           input=input%10;
           //cout<<input;
           }
           
           
           
           else
          {
           cout<<"nada";
          }
      
        
          
         
       cout<<roman;
     
    return 0;
    }
    i appreciate all of the help from everyone

  12. #12
    Registered User
    Join Date
    May 2007
    Posts
    77
    Well, it looks like you solved the "IIIII for 5" problem.
    However, it appears that you have that problem that no matter what the thousands and hundreds place is, neither one displays.

    I'm a little concerned that you just took the code that King Mir gave instead of adapting your existing code to use his code. At the moment, that entire code block is doing absolutely nothing, and is therefore only cluttering your program. Try to put what you have into that code structure.

    P.S. I'm working on it right now, but I think you don't need the definitions for "rome2", "rome3", "rome6", "rome7", and "rome8" because your code is already not using them.

    Edit: I found out that when you made the definitions for the tens and ones, you had the equation "roman=romeX+romeY" and the problem with that is that it overwrites what was in "roman" before, instead of adding to it. Go back and make sure you have "roman=roman +" plus whatever, whenever you have that equation, except for the first time.

    And, you really need to work on your indenting. I don't know about anyone else, but those place definitions are very difficult for me to read.
    Last edited by Molokai; 11-06-2007 at 01:32 PM.

  13. #13
    Registered User
    Join Date
    May 2007
    Posts
    77
    Actually, now that I've looked through it, unless you plan on doing multiple functions and calling functions and passing variables, you probably don't wan to use King Mir's code at all. It would be best if you did, because main would be a lot cleaner, but using my modified version of your code, it works just fine by itself.

    Edit: Not that that's impossible to do. It'll just take a little longer. I guess it depends on when your assignment is due.
    Last edited by Molokai; 11-06-2007 at 02:24 PM.

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    thanks Molo

    i should have the final code for the basic of turning positive integers into roman numerals

    but i want to do this:

    Add (Level 3) to use functions to break your program into more manageable pieces. I'd recommend the bounds checking code for the user's number and the display of the result. In fact, that results display itself could be broken down into a couple of functions: random pre-message and random post-message (with the result itself printed between these calls).

    You can add another (Level 1.5) to place the repetitious pattern of converting [each digit of] the user's number into A re-usable function.

    advice on how to get it started?

    thanks in advance

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    alright everything is good(except infinite loops)

    how does this look? is it more clear than the previous code?

    Code:
    //updated 11/6/07
    #include <cctype>
    #include <string>
    #include <iostream>
    using namespace std;
    int main()
    
    {
    
    char ans='y';
    string roman;
    short i,n;
    short input; //user input
    
    
    
      
    
    
    //string names converted to roman numerals
    
     string rome1="I";
     string rome4="IV";
     string rome5="V";
     string rome9="IX";
     string rome10="X";
     string rome50="L";
     string rome100="C";
     string rome500="D";
     string rome1000="M";
    
    
    
    
    
    
    while(ans=='y' || ans=='Y')
    {
    cout<<"what number would you like to convert?:\n";
    cin>>input;
    cin.ignore();
    
      if(input>0 && input<4000 )
      {
      
      roman="";
      
       //thousands place to get remainder(1000-3000)
       i=1;
       n=input/1000;
       while(i<=n)
        {
       roman=roman+rome1000;
       i=i+1;
        }
        
       
       //hundreds place to get remainder(100-300)
       n=input%1000/100;
       if( n == 4 || n == 9)
         {
       if(n==4)
       roman+=rome100+rome500;
       if(n==9)
       roman+=rome100+rome1000;
         }
       else
       {
       if(n>=5)
         {
       roman=roman+rome500;
       n=n-5;
         }
       i=1;
       while(i<=n)
       {
       roman=roman+rome100;
       i=i+1;
       }
       }
       
       //tens place
       n=input%100/10;
       if( n == 4 || n == 9)
       {
       if(n==4)
       roman+=rome10+rome50;
       if(n==9)
       roman+=rome10+rome100;
       }
       else
       {
       if(n>=5)
       {
       roman=roman+rome50;
       n=n-5;
       }
       i=1;
       while(i<=n)
       {
       roman=roman+rome10;
       i=i+1;
       }
       }
       
       
       //ones place
      n=input%10;
       if (n == 4 || n == 9)
       {
       if(n==4)
       roman=roman+rome4;
       if(n==9)
       roman=roman+rome9;
       // handle 4 and 9 separately
       }
       else
       {
       // handle 5 ..and start 6,7,8
       if(n>=5)
       {
       roman=roman+rome5;
       n=n-5;
       }
       i=1;
       while(i<=n)
       {
       roman=roman+rome1;
       i=i+1;
       }
       }
       
       
       cout << "that is converted from a" <<" " << input << " "
            <<"into" << " " << roman <<" " << "in roman numerals\n";
       cout << "would you like to try again?";
       cin >> ans;
       }
       
       else
       {
       ans='n';
       cout<<"then get the f*&k out then !";
       }
       
       if(ans !='y' || ans !='Y')
       {
       }
       else
       {
       cout<<"dont come the f@*k back !";
       }
      
    }
    
    return 0;
    }
    now i need help with what i previously posted(ie functions)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM