c - Detection of data type through scanf + usage of it in further functions -


so i'm relatively new c , trying out beginner programs. 1 of program reads user input stdin (through usage of scanf).

i'm gonna explain step step sake of easier comprehension of intentions (although part of code functional/operational , not require specific help. if wish, may skip straight question below)

so have program reads input scanf() , decides if it's number or string.

#include <stdio.h> int isnumber(const char *input){  /* while not end of string */  while (*input != '\0'){ /* if not detect number, return 0 */   if (*input < '0' || '9' < *input)    return 0;  input++;   } return 1; }  int main(void){ char uinput[30];  /*ask user input */ printf("please enter number or word \n"); scanf("%29s", uinput);  if (isnumber(uinput)){  printf("we found number %s \n", uinput); }  else {  printf("we found word %s \n", uinput); }  return 0; } 

so yeah, in program works far. everything's clear. know not optimal way of detection, that's not of concern.

question begins here:

now i've decided spice things little, adding function.
more specific, it's concerning prime numbers.

so i've began function of

int prime(int number){  int divider;   (divider = 2; divider <= number - 1; divider++){    if (number%divider == 0)   return 0;     else if (number == divider)   return 1;  } } 

under presumption in function correct, decided initialize function in main() function.

basically want do:

if user input word - print out word if user input number - check if number prime , print out number , whether it's prime.

so here's how change code:

int main (void){  char uinput[30];  int result;    /*user input goes here*/  printf("please enter number or word \n");  scanf("%29s", uinput);  /* if contains numbers */ if(isnumber(uinput)){ result = prime(uinput);    /* i've created if condition, if prime number */   if(result == 1){    printf("it prime number: %d \n", uinput);   } else {    printf("it number: %d \n", uinput);  } else { printf("it word %s \n", uinput); }  return 0; } 

suspicion: i'm 99% sure there error in code (and not syntax one). know lies, due lack of knowledge i'm not sure how fix it.

so doing function prime number, wasn't if should make result of string or integer. issue is, program runs through word string , if number, technically remains string under 'illusion it's number'.

issue was, i'm not sure how compare prime number, that's not integer string. i've changed %s %d on printf. assume there must form of conversion, i'm not aware of.

but of no succes. currently, word detection works, when input example 2, output results in

"i input 2 scanf() through console"

it number 2686752

desired result be

it prime number: 2

(which means deduction, doesn't use first if , skips else, because if have been prime number printf different. there may wrong "prime" function well.

so issue is, can't use scanf(%d, uinput) because otherwise string detection wouldn't work , solution not correct either.

any suggestions?

first there problem in prime function. check (number == divider) incorrect. can remove else if part below.

int prime(int number){  int divider;   (divider = 2; divider <= number - 1; divider++){   if (number%divider == 0)    return 0;  }  return 1; } 

next issue in way number uinput. can use atoi below

if(isnumber(uinput)){ result = prime(atoi(uinput));    /* i've created if condition, if prime number */   if(result == 1){    printf("it prime number: %s \n", uinput);   } else {    printf("it number: %s \n", uinput);  } else { printf("it word %s \n", uinput); } 

complete code below

#include <stdio.h> #include <stdlib.h> int isnumber(const char *input){  /* while not end of string */  while (*input != '\0'){ /* if not detect number, return 0 */   if (*input < '0' || '9' < *input)    return 0;  input++;   } return 1; }  int prime(int number){  int divider;   (divider = 2; divider <= number - 1; divider++){    if (number%divider == 0)   return 0;  }   return 1; }  int main (void){  char uinput[30];  int result;    /*user input goes here*/  printf("please enter number or word \n");  scanf("%29s", uinput);  /* if contains numbers */ if(isnumber(uinput)){ result = prime(atoi(uinput));    /* i've created if condition, if prime number */   if(result == 1){    printf("it prime number: %s \n", uinput);   } else {    printf("it number: %s \n", uinput);    } } else { printf("it word %s \n", uinput); }  return 0; } 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -