Reputation: 2833
My code thus far:
#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);
}
}
For some reason, when I compile, I get the following output:
:~/> 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
:~/>
Nothing happens. On line 22 where I have scanf("%c%c", &typeOfPaving);
, I use %c%c because my professor told me that the first %c stores the new line character after inputting data into the variable d_Width on line 16: scanf("%f", &d_Width);
. Problem I see now is that (I think?) that char typeOfPaving stores \nA? not just A so my if statements won't work? Is that correct?
Can somebody please help me, not sure what to do lol, kind of frustrating
Upvotes: 1
Views: 11457
Reputation: 143129
First thing I've seen is a comparison typeOfPaving == "A"
, that is a character and an array of characters, which is wrong.
Doesn't your compiler ever warn you?
Also, when you pass variables to scanf
you should always pass pointers, not just sometimes. (unless your variable is a pointer, of course).
And you should pass as many pointers as %
-placeholders you have, otherwise things will go wrong.
Upvotes: 1
Reputation: 14329
You've done well by forming a hypothesis that explains why your program is misbehaving. The next important step is figuring out a test that will confirm or refute the hypothesis. That is, if you are claiming that typeOfPaving
stores \nA
, how can you test if this is the case?
If it is the case, what can you do to correct the problem? If it is not the case, what is your next hypothesis?
You'll find that practising this will lead you further than seeking an exact answer from someone.
Upvotes: 1
Reputation: 399881
This:
scanf("%c%c", c_Curbing);
is very wrong, and should give you a compiler error (or at least a warning). The scanf()
function needs a pointer, so you must use:
scanf("%c", &c_Curbing);
Also, it's strange to supply more formatting specifiers than pointers to variables to recieve the values.
Lastly, you really should check the return value to make sure that it succeeded in reading the expected data.
Upvotes: 0
Reputation: 258618
"C"
is a char
array, 'C'
is a char
. That's what you probably need:
if (typeOfPaving == 'C')
Upvotes: 2