tips on this programming job......

Page 3 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

notfred

Lifer
Feb 12, 2001
38,241
4
0
different language, but here's my solution, the logic should be the same.

#!/usr/bin/perl

$nums{M} = 1000;
$nums{D} = 500;
$nums{C} = 100;
$nums{L} = 50;
$nums{X} = 10;
$nums{V} = 5;
$nums{I} = 1;

print "Enter Numeral: ";
$input = <>;
chomp $input;
$input = uc $input;
unless ($input =~ m/[MDCLXVI]+/){print "invalid entry";exit}
@numeral = split //, $input;
for ($i=0;$i<$#numeral;$i++) {
if ($nums{$numeral[$i]}<$nums{$numeral[$i+1]}){$output -= $nums{$numeral[$i]}}
else {$output += $nums{$numeral[$i]}}
}
$output += $nums{$numeral[$#numeral]};

print $output;

and here's the executable, if anyone wants to test it: Link to compiled exe
 

JetBlack69

Diamond Member
Sep 16, 2001
4,580
1
0
you shouldn't have a ; in this line"for (index = 0;index < str.length(); index++);{" in the for loop.
 

BigJohnKC

Platinum Member
Aug 15, 2001
2,448
1
0
A for loop works this way:

for(variable declaration, i.e. index = 0; condition on variable; variable increment)

so when you have
for (index = 0;index < str.length(); index++);{

you don't need
index++;

later on in the loop contents. This may be why your program is not looping.


Secondly, how does your InputBox get the string? does that line inputBox.getString("Enter a String") wait for your input? If not, it may be pulling in no string at all. I think you need to review the syntax on how InputBox and OutputBox get and give strings to the user.
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
hmmm.........that little ; fixed it......but now, any letter I put will = 0.........any ideas on that??
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
actually, I tired it. I ran it and it asked me to input a Roman numeral....I entered X, and pressed enter....then the MS-Dos ran away............no output......dang, my pc is messed up......

btw, I"m realy curious as to how you made an executable?? People say that Java can't make executables?? I dont' know, Im really interested in knowing how to make one.........
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: Semidevil
actually, I tired it. I ran it and it asked me to input a Roman numeral....I entered X, and pressed enter....then the MS-Dos ran away............no output......dang, my pc is messed up......
Run it from a command prompt. If you just run it by double clicking, it runs, and as soon as it prints the answer, the program exits, and the window closes. If you run it from a command prompt, then the window stays open after it finishes, and you have time to read the answer.

on windows 2k/XP:
start > run > cmd

on windows 9x:
start > run > command

You can jsut drag the roman.exe file to the command prompt window, then press enter.

btw, I"m realy curious as to how you made an executable?? People say that Java can't make executables?? I dont' know, Im really interested in knowing how to make one.........

It's not Java.
 

BigJohnKC

Platinum Member
Aug 15, 2001
2,448
1
0
Originally posted by: Semidevil
hmmm.........that little ; fixed it......but now, any letter I put will = 0.........any ideas on that??

Did you take that extra index++; out of there?
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
actually, I tired it. I ran it and it asked me to input a Roman numeral....I entered X, and pressed enter....then the MS-Dos ran away............no output......dang, my pc is messed up......

That's because it's a CUI (console user interface) application. After it's printed the output, it destroys the window. Try running it from a console...

btw, I"m realy curious as to how you made an executable?? People say that Java can't make executables?? I dont' know, Im really interested in knowing how to make one.........

It's Perl, not Java. You can create standard PE (protable executable) files in either language, though, as notfred has shown.

[edit]Ignore me, I replied too late :)...[/edit]
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
I tink I"m just gonna give up on this program........now I"ve got a newer interest......how do you create portable executable in java??
 

BigJohnKC

Platinum Member
Aug 15, 2001
2,448
1
0
Originally posted by: Semidevil
I tink I"m just gonna give up on this program........now I"ve got a newer interest......how do you create portable executable in java??

It's not generally done very much since java isn't really used in that way. I think that some IDE's can create executables (I'm pretty sure Symantec's VisualCafe can, but I haven't checked in a while) out of java. You shouldn't give up, but oh well, I guess some people find it easier to quit than to learn new things that may be hard.
 

Semidevil

Diamond Member
Apr 26, 2002
3,017
0
76
I figured I should read up more on the basics first then rather tackle this program........*sigh*

I"m a quitter.....:('

 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: Semidevil
I figured I should read up more on the basics first then rather tackle this program........*sigh*

I"m a quitter.....:('

You're program really isn't that tough, logically. It only took me about 10 minutes to write in perl (did you ever figure out if it was accurate, btw?). You've just got to understand the syntax bettter.