help please!!

This is a discussion on help please!! within the C++ Programming forums, part of the General Programming Boards category; i am doing a roman numeral program and i easily got it to convert from regular #s to roman numerals ...

  1. #1
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45

    help please!!

    i am doing a roman numeral program and i easily got it to convert from regular #s to roman numerals but im having a problem w/ making roman numerals to regular numbers could some one help me please
    <code>
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    int rtoa( char value[]){
    static char number[40];
    unsigned int newnum=value;
    //string[0]=(char)0;
    // int newnum; const int M=1000;
    // static char number[]=value;
    for(int i=0;i<3000;i++){
    while(number == "M"){
    newnum+=1000;
    }
    if(number=="CM"){
    newnum+=900;
    }
    if(number == "D"){
    newnum+=500;
    }
    if(number == "CD"){
    newnum+=400;
    }
    while(number == "C"){
    newnum+=100;
    }
    if(number == "XC"){
    newnum+=90;
    }
    if(number == "L"){
    newnum+=50;
    }
    if(number == "XL"){
    newnum+=40;
    }
    while(number == "X"){
    newnum+=10;
    }
    if(number == "IX"){
    newnum=+9;
    }
    if(number == "V"){
    newnum+=5;
    }
    if(number == "IV"){
    newnum+=4;
    }
    while(number=="I"){
    newnum+=1;
    }}
    cout<<newnum;
    return newnum;}
    char * ator(const unsigned int value){
    static char string[40];
    unsigned int number=value;
    string[0]=(char)0;
    while(number >= 1000){
    strcat(string,"M");
    number-=1000;
    }
    if(number >= 900){
    strcat(string,"CM");
    number-=900;
    }
    if(number >= 500){
    strcat(string,"D");
    number-=500;
    }
    if(number >= 400){
    strcat(string,"CD");
    number-=400;
    }
    while(number >= 100){
    strcat(string,"C");
    number-=100;
    }
    if(number >= 90){
    strcat(string,"XC");
    number-=90;
    }
    if(number >= 50){
    strcat(string,"L");
    number-=50;
    }
    if(number >= 40){
    strcat(string,"XL");
    number-=40;
    }
    while(number >= 10){
    strcat(string,"X");
    number-=10;
    }
    if(number >= 9){
    strcat(string,"IX");
    number-=9;
    }
    if(number >= 5){
    strcat(string,"V");
    number-=5;
    }
    if(number >= 4){
    strcat(string,"IV");
    number-=4;
    }
    while(number>0){
    strcat(string,"I");
    number-=1;
    }
    cout<<string<<endl;
    return string;
    }



    void main(){
    unsigned int val; int pick=0;
    char valu[40];
    while (pick==1||2){
    cout<<"Would you like to either: "<<endl;
    cout<<" (1) Convert Regular Numbers to Roman Numerals\n";
    cout<<" (2) Convert Roman Numerals to Regular Numbers\n";
    cout<<" (3) Exit\n";
    cin>>pick;
    if (pick==1){
    cout<<"Enter a regular number: ";
    cin>>val;
    ator(val);

    }
    else if(pick==2){
    cout<<"Enter a roman numeral: ";
    cin>>valu;
    rtoa(valu);
    }
    else{
    exit(0);}}


    } </code>

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    The quality of message topics has really gone downhill recently

    Some points to ponder
    <code>
    Code tags use [], not <>

    > for(int i=0;i<3000;i++){
    What are you trying to do here?
    I would have thought that a single pass of the roman numeral string would be enough

    > while(number == "M"){
    1. number isn't initialised
    2. number isn't changed by this loop, so if it gets into the loop, it will never get out
    3. use strcmp for comparing strings

    > void main(){
    main returns an int, as in
    int main ( ) {

    > while (pick==1||2){
    This should be
    while ( pick==1 || pick == 2 ){

  3. #3
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    PS: dont use lots of if statements.. use switch.. and use CODE tags to align ur code in here
    what does signature stand for?

  4. #4
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45
    I tried to do what Salem told me and i have one error that says that
    D:\Microsoft Visual Studio\Work\roman\roman.cpp(9) : error C2440: 'initializing' : cannot convert from 'char' to 'char [40]'
    There are no conversions to array types, although there are conversions to references or pointers to arrays
    Error executing cl.exe.

    i know it should be easy to fix but im stumped
    Code:
    int rtoa(  char value[], const int size){
      static char number[40];
     char newnum[40]=value[size];
       number[0]=(char)0;
    		int regnum=0;  
       while(newnum == "M"){
    	       strcmp(number, "M");
    		   regnum+=1000;
    	   }
           if(newnum=="CM"){
    	       strcmp(number, "CM");
    		   regnum+=900;
    	   }
           if(newnum == "D"){  
    	       strcmp(number, "D");
    		   regnum+=500;
    	   }
           if(newnum == "CD"){
    	       strcmp(number, "CD");
    		   regnum+=400;
    	   }
           while(newnum == "C"){
              strcmp(number, "C");
    		   regnum+=100;
    	   }
           if(newnum == "XC"){
    	      strcmp(number, "XC");
    		   regnum+=90;
    	   }
           if(newnum == "L"){
    	      strcmp(number, "L");
    		   regnum+=50;
    	   }
           if(newnum == "XL"){
    	      strcmp(number, "XL");
    		   regnum+=40;
    	   }
           while(newnum == "X"){
              strcmp(number, "X");
    		   regnum+=10;
    	   }
           if(newnum == "IX"){
    		   strcmp(number, "IX");
    		   regnum=+9;
    	   }
           if(newnum == "V"){
    	      strcmp(number, "V");
    		   regnum+=5;  
    	   }
           if(newnum == "IV"){
    	      strcmp(number, "IV");
    		   regnum+=4;
    	   }
           while(newnum=="I"){
              strcmp(number, "I");
    		   regnum+=1;
       }//}
    cout<<regnum;
    return regnum;}

  5. #5
    elad
    Guest
    char newnum[40]=value[size];

    not sure this is the error call listed, but suspect it might be, and is an error even if it isn't the currently referenced one. You cannot assign one char array to another. You must use strcpy(), strcat() or one of their variants to accomplish this or use objects of the string class of STL or some other appropriately implemented string class.

    Also, if you use zero as a char it is interpretted as the NULL char via the ASCII character set conversion. There is no need to cast it to a char. You could be more explicit by doing this if you wish, so you realize you aren't trying to assign capital oh to the first element of whatever it was.

    char ch = NULL; //is same as
    char ch = 0; //is same as
    char ch = '\0';

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >D:\Microsoft Visual Studio\Work\roman\roman.cpp(9) : error
    >C2440: 'initializing' : cannot convert from 'char' to 'char [40]'
    Are you trying to copy the contents of value to newnum? If so then using the assignment operator will not work, you must copy a C string cell by cell either by hand or with strncpy.
    Code:
    char newnum[40];
    strncpy ( newnum, value, 40 );
    -Prelude
    My best code is written with the delete key.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Forgive me if I am digressing here, but I noticed the long code snippet you posted. Perhaps you could simplify with arrays? I tried doing so with encoding from standard to roman and found the result to be satisfying. So I have an example here, but to keep it simple, I demonstrate the clearer, beginner numerization that was probably taught to young Romans before moving on to the more advanced method I chose this since the code body is quite small and thus illustrative. If you would like to see the function that does a 'real' conversion, just let me know. Again, this demonstrates standard to Roman, not the other way around. I figure since you're probably in school I'd better not give away the answer to your original question, and anyway you probably would rather I didn't anyhow. So here goes:







    Code:
    
    
    char* ToSimpleRoman(char *string)
     {
      int number = atoi(string);  //...convert the input to an integer.
      int position;               //...holds the current array position.
      int multiples;
      int loops;
    
      char *s = string;
      const char symbol[] = "MDCLXVI";
      const int  value[] = { 1000, 500, 100, 50, 10, 5, 1 };
    
    
      for(position = 0; position < 7; position++)
       {
    
          multiples = number / value[position];  //...how many 'M's, etc.
    
    
         if(multiples)
          {
            loops = 0;
    
             while(loops < multiples)
              {
               *(s++) = symbol[position];  //...copy the symbol..
                ++loops;
              }
    
             number -= ( multiples * value[position] ); //...subtract what we've done.
          }
    
       }
    
      *s = 0;    //...null terminate the string.
    
      return string;
     }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Yes, what Sebastiani said is true. Using arrays would be much better to do, especially if you later added new segments to the array later on.

  9. #9
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    What's the best C++ book i can find??
    what does signature stand for?

  10. #10
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    The one with words.

  11. #11
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Yea.. well ... everybody knows that. I mean a title
    what does signature stand for?

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What's the best C++ book i can find??
    The C++ Programming Language by Bjarne Stroustrup.

    -Prelude
    My best code is written with the delete key.

  13. #13
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    Originally posted by Prelude
    >What's the best C++ book i can find??
    The C++ Programming Language by Bjarne Stroustrup.

    -Prelude
    I think its not for beginners..
    what does signature stand for?

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I think its not for beginners..
    Definitely not, but that wasn't the question. The question was "What's the best C++ book i can find??".

    -Prelude
    My best code is written with the delete key.

  15. #15
    Intranasal Heroin User Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,035
    you knew what he meant yet you gave him a smart answer
    shame on you. really. what if he went out and actually bought it?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21