Question about 'strcmp'

I'm just wondering if I did the strcmp string correctly, if I didn't then what is the right way? Cause when I manually calculate my math, it's not adding up to the right result and i'm kinda stuck. What's a way to troubleshoot this and see the value of my if statements if it is executed, if there is a way to do this? Sorry i'm fairly new to c programming.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
int main()
{
    int length, width, numcrowns, total;
    char colour, frame;
    char crowns;
    double totalperim, value;
    float colour_frame = 0.10;
    float regular_frame = 0.15;
    float fancy_frame = 0.25;
    float cardboard = 0.02;
    float glass_front = 0.07;
    float crowns_corner = 0.35;
    
    printf("Please enter the length in inches: ");
    scanf("%d", &length);
    printf("Please enter the width in inches: ");
    scanf("%d", &width);
    printf("What kind of frame do you want, regular or fancy? ");
    scanf("%s", &frame);
    printf("What colour do you want the frame to be? ");
    scanf("%s", &colour);
    printf("Do you want to add crowns on the corners? [0 = No; 1 = Yes]: ");
    scanf("%s", &crowns);
    
    totalperim = length * width;
    totalperim *= cardboard + glass_front;
    
    if (strcmp(&frame, "regular"))
    {
        value=0.15;
    }
    else
        value=0.25;

    if (strcmp(&colour, "white"))
    {
        value+=0.10;
    }
    
    if (crowns=='1')
    {
        printf("Enter Number of crowns: ");
        scanf("%d", &numcrowns);
        total=numcrowns*0.35;
        value+=total;
        
    }
    
    value+=totalperim;
    
    printf("Your Total is %.2f", value);

    return 0;
}
You have to allocate space for the string.

char colour[100], frame[100];

and to get the string input:

scanf("%s", colour);

and to compare

1
2
3
4
if (strcmp(colour, "white") == 0)
{
    /* it's white */
}

Topic archived. No new replies allowed.