-5
#include <stdlib.h>
#include <string.h>

int main(void){
double sum=0;
int ii=0;
char buf[256], token[100]; // I am making this "finite length". You need to know how long the line is that you allow...

printf("Enter the numbers to average on a single line, separated by space, then press <ENTER>\n");
gets(buf, 255, stdin);
token = strtok(buf, " ");
while(token != NULL) {
sum += atof(token);
ii++;
token = strtok("", " "); // get next number
}
printf("AVERAGE: ***** %lf\ *****", sum / (double)ii);
return 0;
} 

このエラーが発生します-9行目:stdin undeclared & stdio.hヘッダーファイルを追加するとエラーが発生します-11行目:左辺値が必要です

誰でも修正できますか?

4

4 に答える 4

2

token配列ではなくポインタでなければなりません

だから交換する

char token[100]

char *token;

この行を置き換えます

token = strtok("", " "); // get next number

token = strtok(NULL, " "); // get next number
于 2013-10-08T14:11:16.487 に答える
0

tokenは配列であり、配列名は変更不可能な左辺値です。

次のオブジェクト型は左辺値ですが、変更可能な左辺値ではありません:

An array type
An incomplete type
A const-qualified type
An object is a structure or union type and one of its members has a const-qualified type

charへのポインタが必要です。strtok文字列で見つかった最後のトークンへのポインターを返します。

したがって、この:

char buf[256], token[100]; 

これでなければなりません:

char buf[256], *token; 

これも間違っています:

gets(buf, 255, stdin);

そのはず:

fgets(buf, 255, stdin);

修正されたコード:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){
double sum=0;
int ii=0;
char buf[256], *token; // I am making this "finite length". You need to know how long the line is that you allow...

printf("Enter the numbers to average on a single line, separated by space, then press <ENTER>\n");
fgets(buf, 255, stdin);
token = strtok(buf, " ");
while(token != NULL) {
sum += atof(token);
ii++;
token = strtok("", " "); // get next number
}
printf("AVERAGE: ***** %lf\ *****", sum / (double)ii);
return 0;
} 
于 2013-10-08T14:10:22.453 に答える
0

ヘッダー ファイルを含める必要がありますstdio.h

stdio.h ヘッダー ファイルを追加すると、エラーが発生します - Line 11: lvalue expected

tokenは変更不可ですl-vlue。変更することはできません。
考えられる解決策の 1 つは、ポインターを宣言しtoken_ptrて の戻り値を割り当てることですstrtok

char buf[256], token[100]; // I am making this "finite length". You need to know how       long the line is that you allow...
char *token_ptr ;
printf("Enter the numbers to average on a single line, separated by space, then press  <ENTER>\n");
gets(buf, 255, stdin);
token_ptr = strtok(buf, " ");
 .....
于 2013-10-08T14:08:05.767 に答える