C ++中的十进制到十六进制转换:没有任何回报

时间:2018-02-19 15:32:52

标签: c++ visual-studio hex decimal

我试图让程序从十进制 - 十六进制转换,反之亦然,具体取决于用户选择的内容。但是,一旦用户输入他们想要转换的号码,它只要求按任意键继续,然后程序关闭。甚至没有回到其他选择。

case 3:
    {
        cout << "Type D2H for Decimal - Hexadecimal conversion\n" 
            << "Type H2D for Hexadecimal - Decimal conversion: " << endl;
        cin >> convert;

        char input, hex[11];
        int hexx, number, num, remainder, i, j, digit, len, n, ct;
        bool error, found;
        char digitt[16] = { '0','1','2','3','4','5','6','7','8','9',
            'A','B','C','D','E','F' };

        /*
            String compare. Either converting decimal to hexadecimal, or vice versa.
        */
        if (strcmp(convert, "D2H") == 0)
        {
            do
            {
                error = false;
                cout << "Enter U for upper case letters\n"
                    << "L for lower case letters. ";
                cin >> input;
                if (toupper(input) == 'U')
                    hexx = 55;
                else if (toupper(input) == 'L')
                    hexx = 87;
                else
                {
                    cout << "Invalid entry\n";
                    error = true;
                }
            } while (error);

            cout << "Enter number to convert: ";
            cin >> num;
            number = num;

            for (i = 0; i < 10; i++)
                hex[i] = ' ';
            hex[11] = '\0';
            i = 10;

            //This is where the conversion takes place
            while (number > 0)
            {
                digit = number % 16;
                if (digit < 10)
                    hex[i] = digit + 48;
                else
                    hex[i] = digit + hexx;
                number /= 16;
                i--;
            }

            if (hexx == 55)
                hex[i] = 'X';
            else
                hex[i] = 'x';
            hex[i - 1] = '0';
            cout << num << " Base 10 in hex is: " << hex << "\n";
        }

        else if (strcmp(convert, "H2D") == 0)
        {
            cout << "Enter number to convert: ";
            cin >> hex;
            num = 0;
            ct = 1;
            len = strlen(hex);
            for (i = len - 1; i >= 0; i--)
            {
                j = 0;
                found = false;
                do
                {
                    if (toupper(hex[i]) == digitt[j])
                    {
                        n = j * ct;
                        num += n;
                        ct *= 16;
                        found = true;
                    }
                    j++;
                } while (!found);
            }
            cout << hex << " Base 16 in decimal is: " << num << "\n";
        }
        else
            cout << "Invalid entry\n";
    }

我在代码中遗漏了什么。以下是我现在拥有的代码的输出结果:

Type D2H for Decimal - Hexadecimal conversion
Type H2D for Hexadecimal - Decimal conversion:
D2H
Enter U for upper case letters
L for lower case letters. L
Enter number to convert: 93
Press any key to continue . . .

在用户键入要转换的数字之前,它可以正常工作。关于代码的任何遗漏或缺失?我一直在改变一些事情,但我还没有找到问题。

2 个答案:

答案 0 :(得分:0)

您正在使用数组越界,您已定义

char input, hex[11];

以后你正在设置

hex[11] = '\0';

数组中的最后一项是hex [10]

答案 1 :(得分:0)

为什么我觉得这是一项任务,您不能使用package com.mohamed.locationdemo; import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class MainActivity extends AppCompatActivity { // declarations LocationManager locationManager; LocationListener locationListener; TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // initiations textView = (TextView) findViewById(R.id.textViewID); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationListener = new LocationListener() { @Override // when the user change his location public void onLocationChanged(Location location) { Log.i("Location is: ", location.toString()); // var location here represents the cords of the phone location textView.setText(location.toString()); } @Override // called when the location service is either enabled or disabled public void onStatusChanged(String s, int i, Bundle bundle) { textView.setText(s); Log.i("on status changed", ""); } @Override // only when the location service enabled public void onProviderEnabled(String s) { textView.setText(s); Log.i("on provider enabled", ""); } @Override // only when the location service is disabled public void onProviderDisabled(String s) { textView.setText(s); Log.i("on provider disabled", ""); } }; if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); } else { // if we have permission then we will get the location directly locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 10, locationListener); Log.i("in the else condition", ""); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); // now we will check if the user answered with yes, then we will get the phone location using LocationManager class if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {Log.i("on request permission", ""); if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 10, locationListener); Log.i("getting loca in methode", ""); } } } } 或任何内置函数(如std::string)来执行此操作?由于您没有在原始问题中提及这些约束,我将继续假设你可以使用它们来回答。

尽可能忽略无效输入(如果您知道输入需要是整数类型,则可以继续询问用户有效输入,直到std::stol实际捕获整数)。输入字符串不能像这样保护,但内置的std::cin只会转换有效的输入。您可以在std::stol循环中调用这些转换函数,就像在我的示例中一样,或者您选择。

do while