-4

I am solving a question to calculate LCM and highest prime number from a set of 4 integers. I have trouble solving exceptions w.r.t scanf such as

  1. some inputs are not integers
  2. if there are more/less than 4 inputs
  3. if inputs not entered with commas or
  4. if there is no prime number from input

I tried using the following code:

if((scanf("%d,%d,%d,%d", &num1,&num2,&num3,&num4))!=4) /* for 4 input verification */

if((scanf("%d,%d,%d,%d", &num1,&num2,&num3,&num4))<4) /* for non-integer inputs */

if((scanf("%d,%d,%d,%d", &num1,&num2,&num3,&num4))<4) /* used comma in between %d for comma check, but how to generate error if input is without commas. */

and how to identify prime number & how to implement the program using multiple threads.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
user5362313
  • 23
  • 1
  • 3

2 Answers2

0

Your first bit of code is almost correct: you should check the returned value to know how many values were read by scanf. But you should probably capture that return value in a variable so you can find out if it is 0, 1, 2, 3, or 4.

As for how to identify a prime number, show us the code you've tried so far in a new question. Likewise, multithreading is a separate question.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
0

You first condition is fine , like if there is anything entered other than intger than scanf wont return 4 , so it will cover each case of wrong input .

But you need to check vairables to find out what it contains.

ameyCU
  • 16,489
  • 2
  • 26
  • 41