Thread: Minor Problem

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

    Minor Problem

    Hi

    I just wanted to know if anyone can help me with a slight problem I have. I created a C program at college using visual studio 2005, and it worked fine. I sent the program code via email so I could work on it at home. I downloaded the trial version of visual studio 2005, and copied and pasted my program code to a new project file. But when I ran the program I started giving out the wrong answers. When I removed one aspect (else if statement) it worked, prob is I need that statement.

    Would it be possible for me to email one of the helpers as I'm worried other people at my college use forum and may steal my code.

    Thanks

    Stewie

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why not post just the section you had to change, and a bit above/below it, so that we get a feeling for the changes you made and what the context is.

    It is likely that you had a problem already, but you weren't seeing it [luck or some such], or the new compiler is exposing some latent problem in your code. It's very rare that "correct code" shows up incorrect on a newer compiler.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Love to help, but I don't like receiving emails with lots of files with lots of code.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    17
    Hi matsp, thanks for replying
    here is the code

    Code:
    #include<stdio.h>
    
    void main()
    
    {
    
       char romanchar[16];
       int counter;
       int sum; 
    
       printf("Please enter a Roman Numeral");
       scanf( "%s", romanchar );
    
       counter = 0;
       sum = 0;
    
       for ( counter = 0; counter < 16; counter++ )
     {
    
       if ( romanchar[counter] == 'I' && romanchar[counter+1] == 'V')
       {
          sum = sum + 4; 
          counter++;
       }
       else if ( romanchar[counter] == 'I' && romanchar[counter+1] == 'X')
       {
    	  sum = sum + 9; 
          counter++;
       }
       else if ( romanchar[counter] == 'X' && romanchar[counter+1] == 'L')
       {
          sum = sum + 40; 
          counter++;
       }
       else if ( romanchar[counter] == 'X' && romanchar[counter+1] == 'C')
       {
    	  sum = sum + 90; 
    	  counter++;
       }
       else if ( romanchar[counter] == 'C' && romanchar[counter+1] == 'D')
       {
    	  sum = sum + 400; 
    	  counter++;
       }
       else if ( romanchar[counter] == 'C' && romanchar[counter+1] == 'M')
       {
    	  sum = sum + 900; 
    	  counter++;
       }
       else if ( romanchar[counter] == 'I' )
       {
    	  sum = sum + 1; 
       }
       else if ( romanchar[counter] == 'V' )
       {
    	  sum = sum + 5; 
       }
       else if ( romanchar[counter] == 'X' )
       {
    	  sum = sum + 10; 
       }
       else if ( romanchar[counter] == 'L' )
       {
    	  sum = sum + 50; 
       }
       else if ( romanchar[counter] == 'C' )
       {
    	  sum = sum + 100; 
       }
        else if ( romanchar[counter] == 'D' )
       {
    	  sum = sum + 500; 
       }
        else if ( romanchar[counter] == 'M' )
       {
    	  sum = sum + 1000; 
       }
    
    
    
     }
    
    printf("Arabic Number = %d\n", sum);
    
    }

    When the bottom three else if statements are removed the program outputs the correct answer

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    void main()
    Nonstandard. Read FAQ!

    Code:
    scanf( "&#37;s", romanchar );
    Unsafe! Read scanf format specifiers better or consider using fgets.

    Code:
    counter = 0;
    Not necessary since you're initializing it to 0 in the loop anyway (though the compiler will probably remove it when compiling a release anyway).

    Code:
    sum = sum + 4;
    All these can be written as sum += (number);

    Code:
    sum = sum + 1;
    Can be written as sum++.

    Consider using proper indentation as well.
    But code in itself, however, is not faulty. It compiles fine.
    Last edited by Elysia; 11-30-2007 at 08:32 AM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    for ( counter = 0; counter < 16; counter++ )
    If you have less than 16 characters in your input, you will process "rubbish". That probably accounts for your incorrect answer. What that rubbish is will very much depend on the actual compiler, runtime and a variety of other things.

    Along with Elysia's comments, of course.
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Use strlen to get the length of the string so you don't process "rubbish" either.

Popular pages Recent additions subscribe to a feed