マイコード:何らかの理由で文字をスキャンし、IF文で使用していますか? (もしchar型の== 'C'){}など、これまで
#include <stdio.h>
main()
{
float p_Asphalt = 5.2, p_Concrete = 4.93, p_Stones = 2.21;
float c_Concrete = 8.88;
float d_Length =, d_Width;
char typeOfPaving, c_Curbing;
float totalCost = 0;
float GST = 1.13, PST = 1.10;
printf("asphalt company\n\n");
printf("Length of Driveway: ");
scanf("%f", &d_Length);
printf("Width of Driveway: ");
scanf("%f", &d_Width);
printf("\nType of Paving:\n");
printf(" 'A' for asphalt paving\n");
printf(" 'C' for concrete paving\n");
printf(" 'S' for paving stones\n\n");
printf("Select (A,C,S): ");
scanf("%c%c", &typeOfPaving);
printf("%c", typeOfPaving);
if (typeOfPaving == "A")
{
totalCost = p_Asphalt * (d_Length * d_Width);
printf("Concrete curbing? [y/n]: ");
scanf("%c%c", c_Curbing);
if (c_Curbing == 'y') {
totalCost = totalCost + (d_Length * c_Concrete);
}
else {
totalCost = totalCost * GST;
printf("Quoted Price of Paving: %f\n", totalCost);
}
}
else if (typeOfPaving == "C")
{
totalCost = p_Concrete * (d_Length * d_Width);
printf("Quoted Price of Paving: %f\n", totalCost);
}
else if (typeOfPaving == "S")
{
totalCost = p_Stones * (d_Length * d_Width);
printf("Quoted Price of Paving: %f\n", totalCost);
}
}
、私はコンパイル時に、私は次のような出力が得られます。
:~/> cc assignment1.c ^C
:~/>
:~/> a.out
asphalt company
Length of Driveway: 123
Width of Driveway: 123
Type of Paving:
'A' for asphalt paving
'C' for concrete paving
'S' for paving stones
Select (A,C,S): A
:~/>
何も起こりません。私がscanf("%c%c", &typeOfPaving);
を持っている22行目で、私の教授が、最初の%cが16行目の変数d_Widthにデータを入力した後に新しい行文字を格納していると私に教えてくれたので%c%cを使用します:scanf("%f", &d_Width);
問題は私が今見ているのは(私は思う?)char typeOfPavingは\ nAを格納していますか? Aだけでなく、私のif文がうまくいきませんか?あれは正しいですか?
これは宿題なので、意図的に曖昧になりますが、二重引用符はCの文字列を示し、一重引用符は一文字を示します。あなたは文字列を扱っていませんが、文字列と比較します。 – mikebabcock
あなたの教授があなたを誤って案内しています。もしあなたが 'scanf'で2つの変換指定子を持っているなら、あなたは次の引数に2つのアドレスを持つべきです。 – pmg
@pmg私もそう言っていましたが、... OPは%c%cを使って\ nをスキップするように言われましたが、後で2つの引数を指定してもOKです。 scanfのBtw C Prog Lang(2番目)は 'c'フォーマッタを示しています。次の白以外の文字を読み込むために白い文字をスキップしないので%1sです。 – wmz