我正在制作一个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');
}
}
答案 0 :(得分:3)
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(或文件结尾)。