locked
Can Some one please convert this c++ code to c# thank you!! RRS feed

  • Question

  • /* 

    It's a course requirement, they will not allow my to use c++ rather c#, I cannot stand the inputs, and most online compilers are hardly worth using with c#, the Microsoft learning site in regards to c# is not interesting compared to other programming languages.

    */

    #include <iostream>
    #include <cmath>
    #include <iomanip>
    #include <string>

    using namespace std;


    int main()
    {
      std::cout<<"Currency converter"<<std::endl;
    // Declared variables
    double Au = 0.8, Us = 1.0;
    double output1, input1, input2,  output2;
    std::cout << "--------------------- "<<std::endl;
    std::cout <<"Au to Us: $"<<std::flush;
    cin >> input1;
    output1 = Au / Us * input1;
    //output1 = Us / Au * input1;

    std::cout << "--------------------- "<<std::endl;
    std::cout <<"$1 Aud to Us is $"<<output1<<std::endl;
    std::cout << "--------------------- "<<std::endl;
    std::cout <<"Us to Au: $"<<std::flush;
    cin >> input2;
    output2 = Us / Au * input2;
    //output2 = Au / Us * input2;  
    std::cout << "--------------------- "<<std::endl;
    std::cout << "$1 Us to Au is $"<<output2<<std::endl;
    std::cout << "--------------------- "<<std::endl;

    return 0;
    }

           

    Saturday, February 10, 2018 2:05 AM

All replies

  • Have you tried a converter? Here is a translation via Tangible software converter yet what is even better is to know what the C++ code does so you can write it yourself in C# without doing a conversion at all.

    using System;
    
    static int Main()
    {
      Console.Write("Currency converter");
      Console.Write("\n");
    // Declared variables
    double Au = 0.8;
    double Us = 1.0;
    double output1;
    double input1;
    double input2;
    double output2;
    Console.Write("--------------------- ");
    Console.Write("\n");
    //C++ TO C# CONVERTER TODO TASK: The cout 'flush' manipulator is not converted by C++ to C# Converter:
    //ORIGINAL LINE: std::cout <<"Au to Us: $"<<std::flush;
    Console.Write("Au to Us: $");
    input1 = double.Parse(ConsoleInput.ReadToWhiteSpace(true));
    output1 = Au / Us * input1;
    //output1 = Us / Au * input1;
    
    Console.Write("--------------------- ");
    Console.Write("\n");
    Console.Write("$1 Aud to Us is $");
    Console.Write(output1);
    Console.Write("\n");
    Console.Write("--------------------- ");
    Console.Write("\n");
    //C++ TO C# CONVERTER TODO TASK: The cout 'flush' manipulator is not converted by C++ to C# Converter:
    //ORIGINAL LINE: std::cout <<"Us to Au: $"<<std::flush;
    Console.Write("Us to Au: $");
    input2 = double.Parse(ConsoleInput.ReadToWhiteSpace(true));
    output2 = Us / Au * input2;
    //output2 = Au / Us * input2;  
    Console.Write("--------------------- ");
    Console.Write("\n");
    Console.Write("$1 Us to Au is $");
    Console.Write(output2);
    Console.Write("\n");
    Console.Write("--------------------- ");
    Console.Write("\n");
    return 0;
    }
    
    //----------------------------------------------------------------------------------------
    //	Copyright © 2006 - 2018 Tangible Software Solutions Inc.
    //	This class can be used by anyone provided that the copyright notice remains intact.
    //
    //	This class provides the ability to convert basic C++ 'cin' and C 'scanf' behavior.
    //----------------------------------------------------------------------------------------
    internal static class ConsoleInput
    {
    	private static bool goodLastRead = false;
    	internal static bool LastReadWasGood
    	{
    		get
    		{
    			return goodLastRead;
    		}
    	}
    
    	internal static string ReadToWhiteSpace(bool skipLeadingWhiteSpace)
    	{
    		string input = "";
    
    		char nextChar;
    		while (char.IsWhiteSpace(nextChar = (char)System.Console.Read()))
    		{
    			//accumulate leading white space if skipLeadingWhiteSpace is false:
    			if (!skipLeadingWhiteSpace)
    				input += nextChar;
    		}
    		//the first non white space character:
    		input += nextChar;
    
    		//accumulate characters until white space is reached:
    		while (!char.IsWhiteSpace(nextChar = (char)System.Console.Read()))
    		{
    			input += nextChar;
    		}
    
    		goodLastRead = input.Length > 0;
    		return input;
    	}
    
    	internal static string ScanfRead(string unwantedSequence = null, int maxFieldLength = -1)
    	{
    		string input = "";
    
    		char nextChar;
    		if (unwantedSequence != null)
    		{
    			nextChar = '\0';
    			for (int charIndex = 0; charIndex < unwantedSequence.Length; charIndex++)
    			{
    				if (char.IsWhiteSpace(unwantedSequence[charIndex]))
    				{
    					//ignore all subsequent white space:
    					while (char.IsWhiteSpace(nextChar = (char)System.Console.Read()))
    					{
    					}
    				}
    				else
    				{
    					//ensure each character matches the expected character in the sequence:
    					nextChar = (char)System.Console.Read();
    					if (nextChar != unwantedSequence[charIndex])
    						return null;
    				}
    			}
    
    			input = nextChar.ToString();
    			if (maxFieldLength == 1)
    				return input;
    		}
    
    		while (!char.IsWhiteSpace(nextChar = (char)System.Console.Read()))
    		{
    			input += nextChar;
    			if (maxFieldLength == input.Length)
    				return input;
    		}
    
    		return input;
    	}
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, February 10, 2018 3:05 AM
  • Ah Karen, I may have explained my self poorly, sorry but I'm not really proficient in c# and I'm not not sure what this converter does?

    Is it like cmake? Do I run it in a IDE, or a online terminal?

    Saturday, February 10, 2018 9:51 AM
  • Ah Karen, I may have explained my self poorly, sorry but I'm not really proficient in c# and I'm not not sure what this converter does?

    Is it like cmake? Do I run it in a IDE, or a online terminal?

    Best to read the documentation

    https://www.tangiblesoftwaresolutions.com/product_details/cplusplus_to_csharp_converter_details.html


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, February 10, 2018 12:29 PM
  • Hello lord_daggeroth,

    For your code the convert process will be easy because it just contains cout and cin method. The "cout" function of C++ corresponds to C# "Console.Writeline" method; The "cin" function of C++ corresponds to C# "Console.Readline" method. The convert result like below.

    using System;
    namespace Test_Project
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Currency converter");
                double Au = 0.8, Us = 1.0;
                double output1, input1, input2, output2;
                Console.WriteLine("--------------------- ");
                Console.Write("Au to Us: $");
    
                input1 = double.Parse(Console.ReadLine());
                output1 = Au / Us * input1;
    
                Console.WriteLine("-------------------- - ");
                Console.WriteLine("$1 Aud to Us is $" + output1);
                Console.WriteLine("--------------------- ");
    
                Console.Write("Us to Au: $");
                input2 = double.Parse(Console.ReadLine());
    
                output2 = Us / Au * input2;
    
                Console.WriteLine("----------------");
                Console.WriteLine("$1 Us to Au is $" + output2);
                Console.WriteLine("----------------");
    
            }
        }
    }

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 12, 2018 2:47 AM
  • Hello,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 27, 2018 8:49 AM