保存命令行参数并对其进行类型转换

Saving command line parameters and typecasting them

本文关键字:类型转换 命令行 参数 保存      更新时间:2023-10-16

我正在制作一个 c++ 程序,该程序将两个 ASCII 字符作为命令行中的输入。然后,它显示这些字符之间的范围以及所述字符的相应十进制、八进制和十六进制代码。

我遇到的问题是对命令行参数进行类型转换。

命令行字符保存在 char* argv[] 中,如果我将它们直接键入到 int(对于十进制、八进制、十六进制),我会得到一些古怪的输出

如果我尝试将它们保存在 char 变量中,然后将该单独的变量类型转换为 int,则不允许我将 argv[1] 保存到 char 中。我收到此错误:

错误:无法使用类型为"char *"的值初始化类型为"char"的实体

注意:显示它的逻辑不完整。由于我在命令行方面遇到问题,并希望首先解决这个问题

#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[])
{
    //If there are no command line parameters, display this info
    if (argc == 1)
    {
        cout << 
        "This program takes two printable ASCII characters as input, with t"
        "he firstncharacter preceding the second in the ASCII character se"
        "quence. The programnthen displays all characters in the range det"
        "ermined by those two characters,nalong with their corresponding d"
        "ecimal, octal and hexadecimal codes, four pernline, with a suitab"
        "le header and a pause if the display consumes more than ansingle "
        "screen of output. The two input character values must be entered a"
        "sntwo separate command-line parameters, and there is no error che"
        "cking.nn"
        "The printable ASCII characters extend from the blank space charact"
        "er (' ',nwith code 32 decimal) to the tilde character ('~', with "
        "code 126 decimal).nThe characters with codes in the range 0 to 31"
        " and also code 127 are non-nprintable "control characters".nn"
        "When entering characters at the command line to determine the char"
        "acter rangenwe want in the output, we need to be very careful how"
        " we enter some characters.nThese include the the blank space char"
        "acter and some others that are treatednas "meta characters" by "
        "the operating system, and are thus not passed to thenprogram for "
        "processing.nn";
        cout << setw(75) << "Screen 1 of 2" << endl;
        cout << "Press Enter to continue ... ";
        cin.ignore (80, 'n');
        cout << endl;
        cout << "Such characters need to be enclosed in double quotes, except ("
        "of course) fornthe double-quote character itself ("), which can be "
        ""escaped" by placing anbackslash character (\) in front of it. Her"
        "e is a list of such characters,nand how they should be entered on the"
        " command line:nn"
        "" " the blank spacen"
        ""&" the ampersandn"
        ""<" the less-than operator, which redirects inputn"
        "">" the greater-than operator, which redirects outputn"
        ""^" the hat symboln"
        ""|" the vertical bar, or pipe symboln"
        "\"  the double-quote symbolnn"
        "All other characters can be entered as themselves.nnnnnnnnn";
        cout << setw(75) << "Screen 2 of 2" << endl;
        cout << "Press Enter to continue ... ";
        cin.ignore (80, 'n');
    }
    //Else if there are command line parameters, display decimal, octal and
    //hexadecimal
    else
    {
        char cFirst = argv[1];
        int first = (int)argv[1];
        int last = (int)argv[2];
        int range = last - first;
        if (range == 0)
        {
            cout << "     Dec Oct Hex" << endl;
            cout << setw(4) << argv[1] << setw(4) << dec << first 
                 << setw(4) << oct << first << setw(4) << hex << first << endl;
            cout << "Press Enter to continue ... ";
            cin.ignore (80, 'n');   
        }
        else if (range == 1)
        {
            cout << "     Dec Oct Hex     Dec Oct Hex" << endl;
            cout << setw(4) << argv[1] << setw(4) << dec << first 
                 << setw(4) << oct << first << setw(4) << hex << first;
            cout << setw(4) << argv[2] << setw(4) << dec << last
                 << setw(4) << oct << last << setw(4) << hex << last << endl;
            cout << "Press Enter to continue ... ";
            cin.ignore (80, 'n'); 
        }
        else if (range == 2)
        {
            int middle = first + 1;
            cout << "     Dec Oct Hex     Dec Oct Hex     Dec Oct Hex" << endl;
            cout << setw(4) << argv[1] << setw(4) << dec << first 
                 << setw(4) << oct << first << setw(4) << hex << first;
            cout << setw(4) << (char)middle << setw(4) << dec << middle 
                 << setw(4) << oct << middle << setw(4) << hex << middle;    
            cout << setw(4) << argv[2] << setw(4) << dec << last
                 << setw(4) << oct << last << setw(4) << hex << last << endl;
            cout << "Press Enter to continue ... ";
            cin.ignore (80, 'n'); 
        }
        else if (range >= 3 && range <= 87)
        {
            cout << "     Dec Oct Hex     Dec Oct Hex     Dec Oct Hex     Dec O"
                    "ct Hex" << endl;
            int count = 0;
            for (int i = first; i <= last; i++)
            {
                if (count < 4)
                {
                    cout << setw(4) << (char)i << setw(4) << dec << i << setw(4)
                         << oct << i << setw(4) << hex << i;
                    count++;
                }
                else 
                {
                    cout << endl;
                    count = 0;
                }
            }
        }
        else if (range >= 88)
        {
            cout << "     Dec Oct Hex     Dec Oct Hex     Dec Oct Hex     Dec O"
                    "ct Hex" << endl;
        }

        for (int i=first; i<=last; i++)
        {
            char working = (char)i;
            cout << (char)i << "  " << dec << i << oct << i << hex << i;
        }
        cin.ignore (80, 'n');
    }
}

argv指向字符串数组(不是字符)。要访问参数的第一个字符,您需要执行以下操作:

int first = (int)argv[1][0];
int last = (int)argv[2][0];

另外,相反

cin.ignore (80, 'n');   

请使用

cin.ignore(numeric_limits<streamsize>::max(), 'n');

要提取(和忽略)的最大字符数。 如果这恰好是numeric_limits<streamsize>::max(),则没有限制:根据需要提取尽可能多的字符,直到找到 delim(或文件末尾)。