我正在编写一个简单的程序,从控制台获取用户输入以计算电阻的电阻值,其中一部分涉及将用户输入与预定字符串列表进行比较。
我已经弄明白了(至少我相信),但每当我调用“搜索”函数时,它应该返回匹配输入的字符串数组的索引,否则返回-1 。问题是,每当我在程序中调用它时它总是返回-1,因此即使不是,程序也会打印“无效颜色”。我不确定问题出在哪里,我想知道我到底能做些什么。
这是有问题的节目:
#include <stdio.h>
#include <math.h>
#include <string.h>
char COLOR_CODES[10][7] = {"black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "gray", "white"};
int COMPARE_LIMIT = 7;
int search(char array[10][7], int size, char target[7]);
int main(void)
{
//used for repeating input menu
int menu = 0;
char menuChoice = 0;
//string variables hold input colors.
char band1[7];
char band2[7];
char band3[7];
//int variables hold int values of subscript for bands 1-3
int value1;
int value2;
int value3;
//value of resistor in ohmn
int ohms;
//holds value of multiplier for third band
int powerMultiplier;
while(menu == 0)
{
printf("Enter the colors of the resistor's three bands,\n");
printf("beginning with the band nearest the end. Type\n");
printf("the colors in lowercase letters only. NO CAPS\n");
printf("OR SPACES.\n");
printf("Band 1:");
scanf("%s", &band1);
printf("Band 2:");
scanf("%s", &band2);
printf("Band 3:");
scanf("%s", &band3);
if(search(COLOR_CODES, 10, band1) == -1)
{
printf("Invalid Color: %s\n", band1);
}
else if(search(COLOR_CODES, 10, band2) == -1)
{
printf("Invalid Color: %n\n", band2);
}
else if(search(COLOR_CODES, 10, band3) == -1)
{
printf("Invalid Color: %s\n", band3);
}
else
{
value1 = search(COLOR_CODES, 10, band1);
value2 = search(COLOR_CODES, 10, band2);
value3 = search(COLOR_CODES, 10, band3);
powerMultiplier = (int)pow(10, value3);
ohms = (value1 * 10) + value2;
ohms = ohms * powerMultiplier;
if(ohms < 1000)
{
printf("Resistance value: %s ohms", ohms);
}
else
{
int kiloOhms = (ohms / 1000);
printf("Resistance value: %s kilo-ohms", kiloOhms);
}
printf("Do you want to decode another resistor? (y/n)");
scanf("%c", &menuChoice);
while(menuChoice != 'y' && menuChoice != 'Y' && menuChoice != 'n' && menuChoice != 'N')
{
if(menuChoice != 'Y' && menuChoice != 'y')
{
if(menuChoice == 'N' || menuChoice == 'n')
{
menu = 0;
}
else
{
printf("Invalid choice.");
}
}
}
}
}
return 0;
}
//returns the subscript of array[][] that matches target[].
int search(char array[10][7], int size, char target[7])
{
int n;
for (n = 0; n < size; n++)
{
if (strncmp(array[n], target, COMPARE_LIMIT) == 0)
{
return n;
}
else
{
return -1;
}
}
}
答案 0 :(得分:1)
在查看列表中的第一项后,您的search()
函数会返回-1
。所以它可能会找到black
,但不会找到任何其他内容。建议修改如下:
int search(char array[10][7], int size, char target[7])
{
int n;
for (n = 0; n < size; n++)
{
if (strncmp(array[n], target, COMPARE_LIMIT) == 0)
{
return n;
}
}
return -1;
}
这样,for
循环将运行完成,然后才能确定找不到您键入的内容,因此返回-1
。
答案 1 :(得分:1)
Greg非常友好地解决了所提出的具体问题,搜索功能在for循环的第一次迭代中返回。
我认为值得一提的是不要忽略编译器警告,尤其是在学习新语言时。以下是编译器对您提交的代码示例(在名为tutorial2.c的文件中)的说法:
cc tutorial2.c -o tutorial2
tutorial2.c:40:17: warning: format specifies type 'char *' but the argument has type 'char (*)[7]' [-Wformat]
scanf("%s", &band1);
~~ ^~~~~~
tutorial2.c:42:17: warning: format specifies type 'char *' but the argument has type 'char (*)[7]' [-Wformat]
scanf("%s", &band2);
~~ ^~~~~~
tutorial2.c:44:17: warning: format specifies type 'char *' but the argument has type 'char (*)[7]' [-Wformat]
scanf("%s", &band3);
~~ ^~~~~~
tutorial2.c:53:37: warning: format specifies type 'int *' but the argument has type 'char *' [-Wformat]
printf("Invalid Color: %n\n", band2);
~~ ^~~~~
tutorial2.c:74:45: warning: format specifies type 'char *' but the argument has type 'int' [-Wformat]
printf("Resistance value: %s ohms", ohms);
~~ ^~~~
%d
tutorial2.c:80:50: warning: format specifies type 'char *' but the argument has type 'int' [-Wformat]
printf("Resistance value: %s kilo-ohms", kiloOhms);
~~ ^~~~~~~~
%d
tutorial2.c:123:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^
7 warnings generated.
[以0.1秒完成]
行号将对应于提交的代码示例中的行号。请注意,最终警告告诉您搜索功能中存在一些异常行为:该控件具有脱离功能结束的能力,因为存在“返回”永远不会被击中的情况。这应该让你逐步逐步完成for循环。此外,其他警告值得研究解决。
答案 2 :(得分:0)
你应该把句子“return -1;”在最后。喜欢这个。
int search(char array[10][7], int size, char target[7])
{
int n;
for (n = 0; n < size; n++)
{
if (strncmp(array[n], target, COMPARE_LIMIT) == 0)
{
return n;
}
}
return -1;
}
此外,还有一些错误。 这一点非常重要,欧姆和千欧姆应该很长,int太短。 这句话:
else if(search(COLOR_CODES, 10, band2) == -1)
{
printf("Invalid Color: %n\n", band2);
}
应该是%s。
在这里,
if(ohms < 1000)
{
printf("Resistance value: %s ohms", ohms);
}
else
{
int kiloOhms = (ohms / 1000);
printf("Resistance value: %s kilo-ohms", kiloOhms);
}
应该是%d。