Click here to Skip to main content
15,886,664 members

Title removed to prevent abuse

RV2020 asked:

Open original thread
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;
}
Tags: Java, C++14

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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