1

Topic: Problem (C programming)

i have a problem.
look at.

#include <stdio.h>
#include <string.h>
  void main()
  {
     int yr_age;
         printf("Enter your Age please");
     scanf("%d",&yr_age);
     if(yr_age<=15)
     {
         printf("Hello\n");
         printf("your age is:");
         printf( "%d",yr_age);
         printf("\n");
     }
     else
         printf("your age is not ok :)\n");
     char benny,eliav,eli,ronit,name;
    printf("Enter your Name");
    scanf("%d,%d,%d,%d",&benny,&eliav,&eli,&ronit,&name);
  if(benny)
      printf("welcome");
  else
      printf("no");
  }

i just start to learn i have a problem .
the program need to do :
enter your age.
if the age is biger from 15 it's will say "your age is not ok"
if it's ok is will wirte " Hello your age is : **"
all it's ok but this text no:
Enter a name:
if the name is not benny, eliav, eli or ronit it's will say "no" if the name is from the list it's will wirte "welcome"

it's not work why?
help plz.

Yours, Benny.

2

Re: Problem (C programming)

SomeOne please?

Yours, Benny.

Re: Problem (C programming)

its a forum you have to wait more than an hour for a reply

4

Re: Problem (C programming)

Hey Benny,

actually your scanf statement is wrong if I understand your problem right.

scanf("%d,%d,%d,%d", ...

will scan for comma separated numbers. On first glance without further testing I would expect that statement to parse strings like "1,2,3,4". And only if the string matches these conditions the scanf statement will apply the values to the given variables.

In general you lack the error handling for the scanf function. The return value as far as I know it will tell you how many values were able to apply to your variables.

So in this case your variable "benny" wont ever contain a complete string. I will contain a single character only which's value is the ascii code of the number you type.

Back to your problem, what you more likely want to do is to have a character buffer able to hold the complete name like

char name[25];

Then you want to scan a full string like

scanf(%s", &name);

and finally you want to compare the given name to "benny". Here you can use the strcmp() function.

if (strcmp(name, "benny")) { printf("ok\n"); }

Hope this ideas gets you closer to your sollution. All the stuff here is untested. If it doesn't work out of the box just look up to correct syntax in your favorite C-manual smile

5

Re: Problem (C programming)

Guest99 Thank you very much!!!
my man!
plz login to the forum smile

Yours, Benny.