gpt4 book ai didi

c - .txt 文件的输入和输出问题

转载 作者:行者123 更新时间:2023-11-30 20:03:05 26 4
gpt4 key购买 nike

我的作业是计算电阻器的电阻(以欧姆为单位),并提供包含电阻器信息的文本文件。使用此文本文件,我计算电阻并将其打印到另一个文本文件。当我运行此代码时,出现调试错误“运行时检查失败#3-T”并且没有输出到文件。你能告诉我哪里出错了吗?

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<math.h>
int col_to_num(char color, int choice) {
int num;
switch (color) {
case 'B':if (choice == 1) num = 0;
else if (choice == 2) num = 1;
else if (choice == 3)num = 6;

case 'b':if (choice == 1) num = 0;
else if (choice == 2) num = 1;
else if (choice == 3)num = 6;

case 'G':if (choice == 1) num = 5;
else if (choice == 2) num = 8;
else if (choice == 3)num = -1;
case 'g':if (choice == 1) num = 5;
else if (choice == 2) num = 8;
else if (choice == 3)num = -1;
case 'R':num = 2;
case 'r':num = 2;
case 'O':num = 3;
case 'o':num = 3;
case 'Y':num = 4;
case 'y':num = 4;
case 'V':num = 7;
case 'v':num = 7;
case 'W':num = 9;
case 'w':num = 9;
case 'S':num = 10;
case 's':num = 10;
}
return num;
}
int main() {
double resistance, thirdband;
char color1, color2, color3;
int val1, val2, val3;
FILE *inp, *outp;
inp = fopen("resistorcolor.txt", "r");
outp = fopen("resistorvalue.txt", "w");
if (inp == NULL) {
printf("The input file does not exist\n");
}
else {
while (fscanf(inp, "%c %d %c %d %c %d%*c", &color1, &val1, &color2, &val2, &color3, &val3) != EOF) {
if (col_to_num(color3, val3) == -1)thirdband = .01;
if (col_to_num(color3, val3) == 10)thirdband = .01;
if (col_to_num(color3, val3) == 0)thirdband = 1;
if (col_to_num(color3, val3) == 1)thirdband = 10;
if (col_to_num(color3, val3) == 2)thirdband = 100;
if (col_to_num(color3, val3) == 3)thirdband = 1000;
if (col_to_num(color3, val3) == 4)thirdband = 10000;
if (col_to_num(color3, val3) == 5)thirdband = 100000;
if (col_to_num(color3, val3) == 6)thirdband = 1000000;
resistance = ((col_to_num(color1, val1) * 10) + col_to_num(color2, val2))*thirdband;
fprintf(outp, "%.2lf Ohm", resistance);
thirdband = 0;
}
fclose(outp);
}
return 0;
}

最佳答案

运行时检查失败#3 意味着某些内容在未初始化的情况下被使用。还有更多您没有看到的信息。错误消息应该是“运行时检查失败 #3:变量 'XXX' 正在使用但未初始化”,其中 XXX 是代码中的变量。

无论如何,您有很多未初始化的变量,这些变量应该初始化为某个默认值:

double resistance = 0.0, thirdband = 0.0;
char color1 = 0, color2 = 0, color3 = 0;
int val1 = 0, val2 = 0, val3 = 0;
FILE *inp = nullptr, *outp = nullptr;

对于函数:

int col_to_num(char color, int choice) {
int num = -1;

很可能是来自 col_to_num 函数的 int num,如果您没有传入这些值之一,它永远不会遇到任何情况。您需要中断才能退出 switch 语句,通常是在满足条件后,但是您可以通过不中断将案例分组在一起,从而允许您轻松地对不同的情况执行相同的操作。可以将其视为从匹配的案例开始失败。如果没有匹配的情况,您还需要一个默认情况来处理:

int col_to_num(char color, int choice) {
int num = -1;
switch (color) {
case 'B':
case 'b':
if (choice == 1) num = 0;
else if (choice == 2) num = 1;
else if (choice == 3)num = 6;
break;
case 'G':
case 'g':
if (choice == 1) num = 5;
else if (choice == 2) num = 8;
else if (choice == 3)num = -1;
break;
case 'R':
case 'r':
num = 2;
break;
case 'O':
case 'o':
num = 3;
break;
case 'Y':
case 'y':
num = 4;
break;
case 'V':
case 'v':
num = 7;
break;
case 'W':
case 'w':
num = 9;
break;
case 'S':
case 's':
num = 10;
break;
default:
printf("Invalid input: %c\n", color);
}
return num;
}

关于c - .txt 文件的输入和输出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54622081/

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