Go Down

Topic: basic c question (Read 3107 times) previous topic - next topic

jonwhite

Code: [Select]

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int age;
    char name[41];

    printf("please enter your age \n");
    scanf("%d", &age);
    printf("you are %d years old\n", age);

    if (!(age <= 19 && age >=13))
         printf("you are not a teenager\n");

    if (age > 19 || age < 13)
         printf("you are not a teenager\n");

    if  (age == 10 || 20 || 30 >=100)
        printf("you have a special age\n");


        printf("please enter your name: ");
        scanf("%s", name);

     if (strcmp(name, "bruce") != 0 && age != 40);   ///error on strcmp ??
        printf("your name is bruce, can i call you bruce\n");

    very_old = age > 80;

    if (!very_old)
        printf("you are not very old\n");
    }



hi there trying to teach myself c ive got an error in strcmp line i cant understand why its there could someone please help
thanks

Grumpy_Mike

Quote
ive got an error in strcmp

What are you trying to compile this on?

This is an arduino forum you know and that is code is not in a format that is suitable for an arduino.

KeithRB


jonwhite

#3
Apr 07, 2014, 10:45 pm Last Edit: Apr 07, 2014, 10:51 pm by jonwhite Reason: 1
hi mike
yes i understand just thought one needed to understand c first  then start with the auduino its compiled on codeblocks

cheers

the error

C:\Users\jons\Desktop\Main file\c examples\Advance relation Operators\main.c|26|warning: implicit declaration of function 'strcmp' [-Wimplicit-function-declaration]|

KeithRB

You need a #include <string.h>

AWOL

very_old isn't declared.

jonwhite

thank you got it going, is it ok to ask general c questions here, honestly i am obsesed with the 328

UKHeliBob

Code: [Select]
     if (strcmp(name, "bruce") != 0 && age != 40);   ///error on strcmp ??Should that semi-colon be there I wonder ........
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

jonwhite

if i take the semicolumb out it doesnt work
strangely in the example im working to it isnt there

KeithRB

With the semi-colon it should *always* execute the following printf.

UKHeliBob

If you leave the semi-colon there then it becomes the empty body of the if statement effectively disabling the test.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

jonwhite

if (strcmp(name, "bruce"), age != 1 && age != 40)

ok panic off works now my statement as wrong, i missed an age out. ""bruce"), age" <--- here

thanks

KeithRB

You don't want a comma there. You want an || or &&

UKHeliBob

Quote
works now
I bet it doesn't
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

BijendraSingh

Quote
I bet it doesn't

i compiled that code after replacing
Quote
if (strcmp(name, "bruce") != 0 && age != 40);
with
Quote
if (strcmp(name, "bruce"), age != 1 && age != 40)

and, it is not showing any error message.
Bijendra

Go Up