gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:52:31 26 4
gpt4 key购买 nike

我正在制作一个 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 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指向字符串(不是字符)的数组。要访问参数的第一个字符,您需要这样做:

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

也,相反

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

请使用

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.

关于c++ - 保存命令行参数并对其进行类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18814777/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com