Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-运行我编译的.exe文件时出错_C - Fatal编程技术网

C-运行我编译的.exe文件时出错

C-运行我编译的.exe文件时出错,c,C,我使用geany编译代码,没有发现任何错误。 但是当我运行.exe文件时,程序就停止工作了,而且我不是一个好的程序员,这是学校的工作 我的程序由两个单词组成,在这个单词中,它将计算每个单词有多少个字母,然后他将wordA中的字母数除以wordB中的字母数 这是我的密码 #include <stdio.h> int main(int argc, char *argv[]) { int i, j; float n; printf ("Insert first

我使用geany编译代码,没有发现任何错误。 但是当我运行.exe文件时,程序就停止工作了,而且我不是一个好的程序员,这是学校的工作

我的程序由两个单词组成,在这个单词中,它将计算每个单词有多少个字母,然后他将wordA中的字母数除以wordB中的字母数

这是我的密码

#include <stdio.h>

int main(int argc, char *argv[])
{
    int i, j;
    float n;

    printf ("Insert first word:\n");

    for(i=0; argv[1][i] != '\0'; i++);

    printf ("Insert second word:\n");

    for(j=0; argv[2][j] != '\0'; j++);

    n=i/j;

    printf("%.4f", n);

    return 0;
}
#包括
int main(int argc,char*argv[])
{
int i,j;
浮动n;
printf(“插入第一个单词:\n”);
对于(i=0;argv[1][i]!='\0';i++);
printf(“插入第二个单词:\n”);
对于(j=0;argv[2][j]!='\0';j++);
n=i/j;
printf(“%.4f”,n);
返回0;
}
在这行中

n = i/j;
您正在执行整数除法。例如,假设
i
是3,
j
是5,然后执行
3/5
,它等于
0

但我认为您希望执行
3.0/5.0
,并希望得到答案
0.6
。所以需要执行浮点除法。可以通过将其中一个操作数强制转换为浮点来强制执行该操作

n = (float)i/j;

在你写的问题中,
Int
而不是
Int
。当我问这个问题时,我认为这是一个抄写错误。但也许你真正的代码是这样的。在这种情况下,您需要将其更改为
int
,以便对其进行编译

您可能遇到的另一个问题是,程序希望在命令行上传递参数。您正在向程序传递两个参数吗?换句话说,您需要像这样执行程序:

program.exe firstword secondword
myapp word1 word2
myapp
Insert first word:
> word1
Insert second word:
> word2
如果未传递参数,则在尝试访问
argv[]
中不存在的参数时,将遇到运行时错误。至少您应该在程序中添加一个检查,以确保
argc==3


如果要从
stdin
读取输入,而不是传递命令行参数,请使用
scanf

我认为这是一个概念错误。您的程序(可能)在这样调用时运行良好:

program.exe firstword secondword
myapp word1 word2
myapp
Insert first word:
> word1
Insert second word:
> word2
但我想你希望它能像这样工作:

program.exe firstword secondword
myapp word1 word2
myapp
Insert first word:
> word1
Insert second word:
> word2
但这不是
argv
的内容。你应该调查一下


具体来说,第二种情况下的错误是因为
argv[1]
为空,因此
argv[1][i]
是一种不正确的内存访问。

是否将单词作为参数传递您应该缩进代码,并粘贴它,而不是复制它。因为
Int
不是定义的类型,所以它不会编译。在第一个
printf
之前,您应该插入:
if(argc<3){printf(“用法:%s\n”,argv[0]);exit(-1);}
+1
Int
应该是
Int
。也许值得一提的是,另一个变量将被隐式转换。@Marounnaroun在实际代码中显然是这样,否则编译器会反对。@FranciscoOgando除非你告诉我,否则我猜不出错误是什么。你说运行时出错?@DavidHeffernan program.exe停止工作,然后关闭程序请阅读我的更新。您是否传递了两个单词作为命令行参数?您需要这样执行程序:
program.exe firstword secondword