在回圈內列印變數時,我收到有關未宣告識別符號的錯誤訊息。給出錯誤的變數是“j”。
// Record preference if vote is valid
bool vote(int voter, int rank, string name)
{
for (int i = 0; i < candidate_count; i )
{
if (strcmp(candidates[i].name, name)==0)
{
for(int j = 1; j <candidate_count; j )
preferences[i][j] = rank;
printf("i = (%i) j = (%i)\n", i , j);
printf("prefenrences[i][j], (%i)(%i)\n", preferences[i][j]);
return true;
}
}
printf("text");
return false;
}
我得到的錯誤資訊如下:
" runoff.c:137:43: 錯誤:使用未宣告的識別符號 'j' printf("i = (%i) j = (%i)\n", i , j); ^ 致命錯誤:發出的錯誤太多,現在停止 [-ferror-limit=] 生成 2 個錯誤。make: *** [: runoff] Error 1 "
我試圖重命名變數,或切換它以查看問題所在。我正在學習 CS50,這是一門編程入門課程,所以我現在不太確定該做什么。感謝所有幫助。
uj5u.com熱心網友回復:
如果您使用較少不穩定的縮進,您會意識到它只是preferences[i][j] = rank;在for (int j = 1; …)回圈的主體中。您需要在至少兩個printf()陳述句(和賦值)周圍使用大括號將它們分組到回圈體中;回圈體可能也應該包含該return陳述句。
你有:
bool vote(int voter, int rank, string name)
{
for (int i = 0; i < candidate_count; i )
{
if (strcmp(candidates[i].name, name)==0)
{
for (int j = 1; j <candidate_count; j )
preferences[i][j] = rank;
printf("i = (%i) j = (%i)\n", i , j);
printf("prefenrences[i][j], (%i)(%i)\n", preferences[i][j]);
return true;
}
}
printf("text");
return false;
}
你可能需要:
bool vote(int voter, int rank, string name)
{
for (int i = 0; i < candidate_count; i )
{
if (strcmp(candidates[i].name, name) == 0)
{
for (int j = 1; j < candidate_count; j )
{
preferences[i][j] = rank;
printf("i = (%i) j = (%i)\n", i , j);
printf("prefenrences[i][j], (%i)\n", preferences[i][j]);
return true;
}
}
}
printf("text\n");
return false;
}
請注意,我已經更正了第二個printf(),因此只有一個%i轉換規范,因為您只傳遞了一個要格式化的值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/524602.html
標籤:C印刷cs50
上一篇:如何通過SWIG使用單個C函式?
