语法错误很小,无法找出 [英] got small syntax error cant find out
本文介绍了语法错误很小,无法找出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include< stdio.h>
#define SALESPEOPLE 5
#define PRODUCT 6
int main( void )
{
char * P [PRODUCT] = {" ," ," , 产品4"};
int 推销员;
int 计数;
printf(" , 销售员", ," ," ," ," );
for (计数= 0 ; Count< PRODUCT; Count ++){
printf(" );
scanf(" ,& Product 1 [Count]);
}
for (计数= 0 ; Count< PRODUCT; Count ++){
printf(" );
scanf(" ,& Product 2 [Count]);
}
for (计数= 0 ; Count< PRODUCT; Count ++){
printf(" );
scanf(" ,& Product 3 [Count]);
}
for (计数= 0 ; Count< PRODUCT; Count ++){
printf(" );
scanf(" ,& Product 4 [Count]);
}
for (计数= 0 ; Count< PRODUCT; Count ++){
printf(" );
scanf(" ,& Product 5 [Count]);
}
for (salesman = 1 ;推销员< SALESPEOPLE;推销员++){
printf(" ,业务员,产品 1 [Count],产品 2 [Count],产品 3 [Count],产品 4 [Count],产品 5 [Count]);
}
返回 0 ;
}
我在代码中遇到一个小错误,也许是语法错误.我找不到答案,我可以知道问题出在哪里吗?
解决方案
不要在变量名中使用空格.在上面的代码中,您使用产品1,产品2时,该空间在扫描和打印时会产生问题.前2个错误):
错误1错误C2065:产品":未声明的标识符
错误2错误C2143:语法错误:``常量''之前缺少``)''
Visual Studio
指向以下行
scanf("%f", &Product 1[Count]);
因此,正如 vishal_kapoor 所指出的那样,您使用了无效的标识符(标识符中不能使用space
).
代码.
1-每个人都指出了Product 1
变量中的空间问题.
2-Product1
或Product2
未声明,因此您不能将它们用作变量名.请使用P
或声明所需的变量.
3-您将PRODUCT
定义为6
并仅初始化数组中的4个字符串:正常吗?
4-我认为P
的类型不正确.您将其声明为字符串数组,并使用"Product 1"
,"Product 2"
,...对其进行初始化,但是之后您要求用户为每个产品输入字符串.因此,我认为您要为产品数组的每个条目存储多个字符串,而不仅仅是1个字符串.
5-您的最终循环很奇怪.我认为您想打印出与salesman
相关的所有产品,但是您只有1个循环(对于salesman
),即使使用Count
索引也没有产品循环.
6-您的产品循环看起来一样(或几乎一样).不建议执行复制/粘贴操作,例如,应使用循环将其弄清楚.
我可能是错的,但如果我理解得很好,您想定义一个销售员列表,并为每个销售员关联一个产品列表,最后打印出所有这些表.
如果我是对的,那么您应该声明一个销售员数组,并且不仅要声明一个产品数组,而且要声明与您的销售员数量一样多的数组.
#include <stdio.h>
#define SALESPEOPLE 5
#define PRODUCT 6
int main(void)
{
char* P[PRODUCT] ={"Product 1","Product 2","Product 3","Product 4"};
int salesman;
int Count;
printf("%s%13s%18s%23s%28/n", "Salesperson", "P1", "P2","P3","P4","P5");
for(Count=0; Count< PRODUCT; Count++){
printf("Enter 5 value of product 1 by each respectively salesperson");
scanf("%f", &Product 1[Count]);
}
for(Count=0; Count< PRODUCT; Count++){
printf("Enter 5 value of product 2 by each respectively salesperson");
scanf("%f", &Product 2[Count]);
}
for(Count=0; Count< PRODUCT; Count++){
printf("Enter 5 value of product 3 by each respectively salesperson");
scanf("%f", &Product 3[Count]);
}
for(Count=0; Count< PRODUCT; Count++){
printf("Enter 5 value of product 4 by each respectively salesperson");
scanf("%f", &Product 4[Count]);
}
for(Count=0; Count< PRODUCT; Count++){
printf("Enter 5 value of product 5 by each respectively salesperson");
scanf("%f", &Product 5[Count]);
}
for(salesman=1 ; salesman<SALESPEOPLE ; salesman++){
printf("%5s%13s%18s%23s%28s/n", salesman, Product 1[Count],Product 2[Count], Product 3[Count], Product 4[Count], Product 5[Count]);
}
return 0;
}
i encounter a small error in my code, maybe is syntax error. I cant find it out, may i know where is the problem?
解决方案
Don''t use space in the variable names. In above code you used Product 1, Product 2, this space will create a problem while scanning and printing.
Compiler messages are usually pretty informative, trying to compile your code gives me (reporting just the first 2 errors):
Error 1 error C2065: ''Product'' : undeclared identifier
Error 2 error C2143: syntax error : missing '')'' before ''constant''
withVisual Studio
pointing to the following line
scanf("%f", &Product 1[Count]);
Hence, as already noted by vishal_kapoor, you used invalid identifiers (space
cannot be used in identifiers).
There are several problems in your code.
1- Everybody pointed out the space problems inProduct 1
variable.
2-Product1
orProduct2
are not declared so you can''t use them as variable names. UseP
instead, or declare the variables you need.
3- You definePRODUCT
as6
and initialize only 4 strings in your array: is it normal?
4 - I think type ofP
is not correct. You declare it as a string array, initialize it with"Product 1"
,"Product 2"
, ..., but after that you ask the user to enter a string for each product. Therefore, I think you want to store several strings for each entry of your product array, and not only 1 string.
5 - Your final loop is strange. I think you want to print out all products associated to asalesman
but you have only 1 loop (forsalesman
), and no loop for products even though you useCount
index.
6 - Your products loops look like the same (or almost the same). Doing copy/paste is not recommanded, you should try to make it clearer, using a loop for example.
I might be wrong, but if I understood well, you want to define a list of salesmen, and for each salesman associate a list of products, and finally print out a table of all that.
If I am right, then you should declare 1 array of salesman, and declare not only 1 array of products, but as many arrays as you have salesmen.
这篇关于语法错误很小,无法找出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文