【发布时间】:2020-07-18 14:41:51
【问题描述】:
嘿,伙计们,我第一次尝试使用 int main(int argc, argv[]) 时不知道我在哪里做错了……我只是不知道我是否正确使用它。这是一些代码...
- 代码编译得当,也能正确给出结果
使用 4 作为密钥将“BaRFoo”加密为“FeVJss” 日志 运行./凯撒 4... 发送输入 BarFoo... 检查输出“密文:FeVJss\n”...
*****但是这是我的问题,当我输入 2r 或 4w 或任何非数字键时,它会给我这样的信息:***
:( 处理非数字键 等待程序退出时超时
谁能告诉我如何为这个函数添加另一个循环,以使 nummerica 键打印为 -Usage: ./caesar key 。请 ?非常感谢您的帮助和有用的建议。
#include <string.h>
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
// Variable declarations
bool keySuccessful = false;
int key = 0;
int input_length = 0;
string text = "" ;
// The number of command line args submitted was incorrect.
do
{
if(argc != 2)
{
printf("Usage: ./caesar key .\n");
return 1;
}
else
{
// Convert ASCII char to a alphabate char
// Access individul char in the string plain
// get the key val and converted to integer
key = atoi(argv[1]);
keySuccessful = true;
printf("%s",argv[1]);
}
}
while(!keySuccessful);
// get user input
text = get_string("%s",text);
printf("ciphertext: ");
input_length = strlen(text);
for (int i=0; i<input_length; i++)
{
// Checking if is the lowercase a=97 to z=112
// Print out lower case with ky a=65 to z=90
if(isupper(text[i]))
{
printf("%c", (((text[i] - 65) + key) % 26) + 65);
}
else if(islower(text[i]))
{
printf("%c", (((text[i] - 97) + key) % 26) + 97);
}
else
{
printf("%c", text[i]);
}
}
printf("\n");
return 0;
}
【问题讨论】:
-
查看
strtol以确保参数是有效数字并获取其值,切勿使用atoi
标签: c cs50 caesar-cipher