私は現在作業中のCプログラミング課題を持っています。収穫した作物のポンドを表す5つの数値を読み込んで棒グラフを表示するプログラムそれらの値を表す。これを行うには、for、while、do whileステートメントを使用する必要があります。文字列を解析するグラフィック、配列、コマンドを使用することはできません。ここで配列や文字列を使用しないCの水平バーグラフ
は私が複製する必要がある出力のサンプルです:
Pounds of Beans: 1
Pounds of Carrots: 2
Pounds of Lettuce: 3
Pounds of Peas: 4
Pounds of Squash: 5
Beans: B
Carrots: CC
Lettuce: LLL
Peas: PPPP
Squash: SSSSS
私は成功した5つの入力を取ることができました。私の問題は、適切な構文とフロー構造から始まり、必要に応じて棒グラフを印刷できます。私は現在、最初の作物のために棒グラフを適切に印刷することができますが、各作物について再帰的に行う方法は非常に混乱します。このプログラムを正しく実行するために私が理解しなければならない流れ、私が作っている構文上の誤り、正確には "ガーディング"の意味について説明してください。ここで
は、私がこれまで持っているコードです:
#include <stdio.h>
// Initialize main
int main(void)
{
// Introduce variables
int count, b, c, l, p, s;
// Prompts & obtaining of pounds of crops
printf("Pounds of Beans: ");
scanf("%d", &b);
printf("Pounds of Carrots: ");
scanf("%d", &c);
printf("Pounds of Lettuce: ");
scanf("%d", &l);
printf("Pounds of Peas: ");
scanf("%d", &p);
printf("Pounds of Squash: ");
// Print empty line between input and output
printf("\n");
// Precursory print of "Beans:"
printf("Beans: ");
// Display pounds of beans using for statment
for (count=0; count<b; count++)
{
printf("%.*s", b, "B");
}
printf("\n");
printf("Carrots: ");
// Display lbs of carrots using for statment
for (count=0; count<c; count++);
{
printf("%.*s", c, "C");
}
printf("\n");
}
私は現在、エラーを取得しています:あなたが提供することができます
graph.c:44:2: warning: this ‘for’ clause does not guard... [-Wmisleading-indentation]
for (count=0; count<c; count++);
^~~
graph.c:45:3: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the ‘for’
{
^
すべてのヘルプは大歓迎されます。
その行の 'for'文の後ろにある漂白セミコロンを削除します。 –
あなたは文字列を使用しています。 – EOF
はい私は必要な行を表示するために文字列を使用していることを知っています。しかし、例えばstringlengthのような文字列を解析するコマンドは使用できません。 –