-
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
-
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
-
Love to help, but I don't like receiving emails with lots of files with lots of code.
-
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
-
Nonstandard. Read FAQ!
Code:
scanf( "%s", romanchar );
Unsafe! Read scanf format specifiers better or consider using fgets.
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).
All these can be written as sum += (number);
Can be written as sum++.
Consider using proper indentation as well.
But code in itself, however, is not faulty. It compiles fine.
-
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
-
Use strlen to get the length of the string so you don't process "rubbish" either.