Question Can anyone help me to convert c++ to c# ?

AliPak

New member
Joined
Feb 24, 2022
Messages
1
Programming Experience
1-3
C++:
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

typedef unsigned int my_counter;
typedef unsigned int my_sum;
const my_counter my_min_digit = 100;
const my_counter my_max_digit = 1000;

int main() {
    my_counter dd;
    std::cout << "Please enter your digit=";

    cin.clear();
    cin >> dd;

    cout << endl << "Your digit is=" << dd;
    cout << endl;

    my_counter i;
    my_counter i1;
    for (i = my_min_digit, i1 = 0; i != my_max_digit; ++i) {

        string str;
        stringstream sstr;
        sstr << i;
        sstr >> str;

        my_sum sum = 0;
        my_counter j;
        for (j = 0;; ++j) {

            if (str[j] == 0) break;

            my_sum val;
            switch (str[j]) {

                case '0':val = 0; break;
                case '1':val = 1; break;
                case '2':val = 2; break;
                case '3':val = 3; break;
                case '4':val = 4; break;
                case '5':val = 5; break;
                case '6':val = 6; break;
                case '7':val = 7; break;
                case '8':val = 8; break;
                case '9':val = 9; break;

                default: cout << endl << "Galaxy error"; return 1;
            }

            sum += val;
        }

        if (sum == dd) {
            cout << endl << "digit(" << i1 << ")=" << i;

            ++i1;
        }
    }

    cout << endl;

    if (i1 == 0) {
        cout << endl << "digits don't find";
    }
    else {
        cout << endl << "found " << i1 << " digits";
    }

    return 0;
}
 
This site is not a code conversion service. If you are having specific issues, show us what you have done and point out exactly where the issue is. In general, rather than trying to translate code form one language to another, you should determine exactly what functionality is implemented in the original code and then determine the best way to implement that functionality in the new language.
 
Hmmm... Interesting that the OP didn't say that it had to be well written C# code... This could be a fun challenge. How to rewrite this code the most obtuse way possible using C# -- so terrible that one would never try to submit it as homework/schoolwork?
 

Latest posts

Back
Top Bottom