简体   繁体   English

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

[英]Saving command line parameters and typecasting them

I'm making a c++ program that takes two ASCII characters as input in the command line. 我正在制作一个使用两个ASCII字符作为命令行输入的c ++程序。 It then displays the range between those characters along with the corresponding decimal, octal, and hexadecimal codes for said characters. 然后,显示这些字符之间的范围以及相应的十进制,八进制和十六进制代码。

The problem I am having is with typecasting the command line parameters. 我遇到的问题是强制转换命令行参数。

the command line characters are saved in char* argv[] and if I type cast them directly to an int (for the decimal, octal, hexadecimal) I get some wacky output 命令行字符保存在char * argv []中,如果我直接将其强制转换为int(对于十进制,八进制,十六进制),则会得到一些古怪的输出

if i try saving them in a char variable to then typecast that separate variable into an int it doesn't allow me to save argv[1] into the char. 如果我尝试将它们保存在char变量中,然后将该单独的变量转换为int类型,则不允许我将argv [1]保存到char中。 I get this error: 我收到此错误:

Error: A value of type "char *" cannot be used to initialize an entity of type "char" 错误:“ char *”类型的值不能用于初始化“ char”类型的实体

Note: the logic for displaying it is not complete. 注意:显示逻辑不完整。 As I am having issues with the command line stuff and wanted to tackle that first 当我遇到命令行问题时,我想先解决

#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 first\ncharacter preceding the second in the ASCII character se"
        "quence. The program\nthen displays all characters in the range det"
        "ermined by those two characters,\nalong with their corresponding d"
        "ecimal, octal and hexadecimal codes, four per\nline, with a suitab"
        "le header and a pause if the display consumes more than a\nsingle "
        "screen of output. The two input character values must be entered a"
        "s\ntwo separate command-line parameters, and there is no error che"
        "cking.\n\n"

        "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\".\n\n"

        "When entering characters at the command line to determine the char"
        "acter range\nwe 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 treated\nas \"meta characters\" by "
        "the operating system, and are thus not passed to the\nprogram for "
        "processing.\n\n";

        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) for\nthe double-quote character itself (\"), which can be "
        "\"escaped\" by placing a\nbackslash character (\\) in front of it. Her"
        "e is a list of such characters,\nand how they should be entered on the"
        " command line:\n\n"

        "\" \" the blank space\n"
        "\"&\" the ampersand\n"
        "\"<\" the less-than operator, which redirects input\n"
        "\">\" the greater-than operator, which redirects output\n"
        "\"^\" the hat symbol\n"
        "\"|\" the vertical bar, or pipe symbol\n"
        "\\\"  the double-quote symbol\n\n"

        "All other characters can be entered as themselves.\n\n\n\n\n\n\n\n\n";

        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 points to array of strings (not characters). argv指向字符串数组(不是字符)。 To access the first character of arguments you need to do this: 要访问参数的第一个字符,您需要执行以下操作:

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

Also, instead 而且,相反

cin.ignore (80, '\n');   

please use 请用

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

Maximum number of characters to extract (and ignore). 要提取(和忽略)的最大字符数。 If this is exactly numeric_limits<streamsize>::max() , there is no limit: As many characters are extracted as needed until delim (or the end-of-file) is found. 如果这恰好是numeric_limits<streamsize>::max() ,则没有限制:根据需要提取尽可能多的字符,直到找到delim(或文件末尾)为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM