Skip to content

Converting Code

When you switch programming languages, you need to learn how that language works. What we have learnt about C#, converts quite easily to C++. Let’s have a quick look at the main differences.

Illustration of converting our code to C++

Example

The following code is the C++ version of the menu code from the control flow chapter.

#include "splashkit.h"
int main()
{
string line;
int choice;
do
{
write_line("1: Addition");
write_line("2: Subtraction");
write_line("3: Multiplication");
write_line("4: Division");
write_line("5: Quit");
write("Enter your choice: ");
line = read_line();
while (!is_integer(line))
{
write_line("Please enter a whiole number");
write("Enter your choice: ");
line = read_line();
}
choice = stoi(line);
switch (choice)
{
case 1:
write_line("Addition");
break;
case 2:
write_line("Subtraction");
break;
case 3:
write_line("Multiplication");
break;
case 4:
write_line("Division");
break;
case 5:
write_line("Quit");
break;
default:
write_line("Invalid choice");
break;
}
} while (choice != 5);
}