Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey...
Can I please have some help converting the following Java code into C++?

SQL
var a, b, c, d, s : int
var nikkySteps, nikkyDistance : int
var byronSteps, byronDistance : int
var next, sgn : int

get a, b, c, d, s

nikkySteps := 0
nikkyDistance := 0
next := a
sgn := 1
loop
    exit when nikkySteps + next >= s
    nikkySteps := nikkySteps + next
    nikkyDistance := nikkyDistance + sgn * next
    if sgn = 1 then
	next := b
    else
	next := a
    end if
    sgn := sgn * -1
end loop
nikkyDistance := nikkyDistance + sgn * (s - nikkySteps)

byronSteps := 0
byronDistance := 0
next := c
sgn := 1
loop
    exit when byronSteps + next >= s
    byronSteps := byronSteps + next
    byronDistance := byronDistance + sgn * next
    if sgn = 1 then
	next := d
    else
	next := c
    end if
    sgn := sgn * -1
end loop
byronDistance := byronDistance + sgn * (s - byronSteps)

if nikkyDistance > byronDistance then
    put "Nikky"
elsif nikkyDistance < byronDistance then
    put "Byron"
else
    put "Tied"
end if




Thanks

What I have tried:

C++
#include <iostream>
using namespace std;

int main() {
	int a,b,c,d,s
        int nikkySteps, NikkyDistance
        int byronSteps, byrinDistance
        int next, sgn
        cin >> a;
        cin >> b;
        cin >> c;
        cin >> s;
        cin >> d;
        int nikkySteps = 0;
        int nikkyDistance = 0;
        int next = a;
        int sgn = 1;
*confused*
        nikkySteps = nikkySteps + next;
        nikkyDistance = nikkyDistance + sgn * next;
        if (sgn = 1 ){
           next = b
        } else {
           next = a
        }
        sgn = sgn * -1
*confused*
nikkyDistance = nikkyDistance + sgn * (s = nikkySteps);

int byronSteps = 0
int byronDistance = 0
int next = c
int sgn = 1
*confused"
        byronSteps = byronSteps + next;
       byronDistance = byronDistance + sgn * next;
        if (sgn = 1 ){
           next = d
        } else {
           next = c
        }
        sgn = sgn * -1
byronDistance = byronDistance + sgn * (s - byronSteps)
if (nikkyDistance > byronDistance ) { 
        cout << "Nicky" << endl;
} else if (nikkyDistance < byronDistance ) {
        cout << "Byron" << endl;
} else {
        cout << "Tied" << endl;
}
	return 0;
}
Posted
Updated 4-Nov-20 3:19am
v3
Comments
CPallini 12-Feb-18 9:23am    
That's PL/SLQ, not Java, isn't it?
David_Wimbley 12-Feb-18 11:44am    
The := gives me delphi flashbacks
Maciej Los 12-Feb-18 16:04pm    
Agree!
RV2020 13-Feb-18 4:48am    
sorry!

1 solution

You have to understand what above code does. And you have to rewrite it from scratch!

In case you're lazy, you can use online converter. Note: it's not Java, it's Delphi. A list of available converters, you'll find here: .NET Code Conversion - Convert Your Code[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900