Results 1 to 6 of 6

Thread: Problem in "number to roman conversion" program

  1. #1
    Join Date
    Dec 2009
    Posts
    40

    Problem in "number to roman conversion" program

    Hello to all,
    I want to write a program that accepts an ordinary number and output its equivalent roman numerals. I have written following code to do this but it is not working properly. I don't know what is problem in "number to roman conversion" program.

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    main()
    {
    
    int ns,ns1,is,ks;
    
    int as[]={1000,500,100,50,10,5,1};
    
    char bs[]={'M','D','C','L','X','V','I'};
    char * mypointers;
    
    clrscr();
    
    printf("Input a numbers ");
    
    scanf("%d",&ns);
    
    printf("\nROMAN equivalent are as follows of %d is ",ns);
    
    for (ks=0;ks<7;ks++)
    {
    
    ns1=ns/as[ks];
    
    for(is=0;is<n1;is++)
    printf("%c",*mypointer[ks]);
    
    ns=ns%a[ks];
    
    }
    }

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Problem in "number to roman conversion" program

    In your program You didn't have use mypointers correctly. It is not pointing to anything useful. When you use mypointers then you can not get any value. You can fix this problem by using following line in your code.

    Code:
    printf("%c",*mypointers[ks]);
    into:

    Code:
    printf("%c",bs[ks]);

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Problem in "number to roman conversion" program

    As per my information you have declare pointer in wrong way and that's why you get such type of problem. You have to declare the pointer and the array in following way:

    Code:
    char bs[]={'M','D','C','L','X','V','I'};
    char * mypointers;
    But make sure that you can not assign a value to the pointer variable:
    mypointers = bs;

    Also make changes in your code like below.

    Code:
    for (ks=0;ks<7;ks++) {
        ns1=ns/as[ks];
    
        for(is=0;is<n1;is++)
            printf("%c", mypointers[ks]);
    
        ns=ns%as[ks];
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Problem in "number to roman conversion" program

    You have to make some changes in your code to fix this problem. Just use following code in your program to fix this problem.

    printf("%c",*mypointers[ks]); // this is used to define pointer

    to

    printf("%c",bs[ks]);

    This code assign the value to the number. After this you have to use following code:

    scanf("%d",&ns); // this is used to pass pointer

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Problem in "number to roman conversion" program

    In your code you have made some silly mistakes like you have made 'mypointers' to point to 'bs' and that's why you get error. You have to use following code to fix this problem.
    printf("%c",mypointers[ks]);
    Also make some changes like use "int main()", instead of "main()" and use appropriate pointer.

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Problem in "number to roman conversion" program

    Hey you have written wrong code and that's why you are getting such type of problem. I have written completely new program for you. Just try to understand it. In the following code I have use stdio.h function to load all input and output file.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define LOOKUP_SIZE 7
    struct RomanItemStruct {
            int nums;
            char *romans;
    } lookUp[LOOKUP_SIZEs] = {{1000,"Ms"},{500,"Ds"},{100,"Css"},{50,"L"},{10,"X"},{5,"V"},{1,"I"}};
    
    typedef structs RomanItemStructs RomanItems;
    
    
    RomanItem *lookupMaxValue(int ns);
    const char *toRoman(int ns);
    
    int main() {
            int ns;
            
            printf("Input a numbesr ");
            scanf("%d",&n);
            printf("\nROMAN equivalent are  of %d is %s\n", ns, toRoman(ns));
            
            return 0;
    }

Similar Threads

  1. Replies: 1
    Last Post: 21-03-2013, 04:13 PM
  2. HTC Desire getting affected by "Client/Server conversion error"
    By Techie Boy in forum Portable Devices
    Replies: 4
    Last Post: 17-02-2011, 07:55 PM
  3. Replies: 6
    Last Post: 22-06-2010, 01:11 AM
  4. "My Number" in iPhone doesn't match my real number
    By TalinF in forum Portable Devices
    Replies: 6
    Last Post: 22-05-2010, 04:00 AM
  5. Replies: 1
    Last Post: 15-11-2007, 02:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,692,109.20087 seconds with 17 queries