7 Replies - 551 Views - Last Post: 30 October 2018 - 12:43 PM Rate Topic: -----

#1 Ivanofff   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 22-September 18

How to check: is number integer or not (C)

Posted 30 October 2018 - 10:52 AM

Hi, this is my program. I want to add a condition in the first "if", that there must be typed only integer, because when I'm typing float, my program works and I don't want this trulala
Attached Image
or this trulala
Attached Image
Idea is
Attached Image

One more question. I know, that using goto is not very good. How can I make it better? :sweatdrop:
Please, help!!!

#include<stdio.h>
#include<math.h>

main()
{
    int n, p;
    float y, a, b, x,
          A[p], B[p], X[p], Y[p];
    start:
    //Введіть, скільки y-ів ви хочете мати
    printf("Type, how many y u want to have: ");
    scanf("%d", &n);
    if(n <= 0)
    {   //Спробуйте ще раз. Введіть додатнє число
        printf("Try again. Type a positive number!!!\n");
        goto start;
    }
    else
    {
        for(p = 0; p < n; p++)
        {
            printf("Type a(%d) = ", p);  scanf("%f", &A[p]); a = A[p];
            printf("Type b(%d) = ", p);  scanf("%f", &B[p]); b = B[p];
            printf("Type c(%d) = ", p);  scanf("%f", &X[p]); x = X[p];

            Y[p] = (a + pow(sin(b*b + x*x), 2))/ pow(b*b + x*x, 1/3); y = Y[p];
            printf("y(%d) = %f\n", p, y);
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: How to check: is number integer or not (C)

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14487
  • View blog
  • Posts: 58,070
  • Joined: 12-June 08

Re: How to check: is number integer or not (C)

Posted 30 October 2018 - 11:11 AM

"trulala"?

Quote

I know, that using goto is not very good. How can I make it better

GOTOs can be substituted for loops. A do-while loop to ask for input and get it.. then check if it needs to be repeated to get the right input.
Was This Post Helpful? 1
  • +
  • -

#3 Ivanofff   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 22-September 18

Re: How to check: is number integer or not (C)

Posted 30 October 2018 - 11:38 AM

View Postmodi123_1, on 30 October 2018 - 11:11 AM, said:

"trulala"?

I mean, that program doesn't work correctly. When I type 3.5 or 3,5 , then I don't do anything. But programm do all stuff(trulala), that is down of Type, how many y u want to have:

And how can I check, is my typed number integer or not? Is there some command for this in C?
I mean
if((n <= 0) & (n not integer))
{
printf("try again");
}

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14487
  • View blog
  • Posts: 58,070
  • Joined: 12-June 08

Re: How to check: is number integer or not (C)

Posted 30 October 2018 - 11:49 AM

Like 'isdigit'?
https://www.tutorial...ion_isdigit.htm
Was This Post Helpful? 1
  • +
  • -

#5 jimblumberg   User is offline

  • member icon

Reputation: 5593
  • View blog
  • Posts: 17,255
  • Joined: 25-December 09

Re: How to check: is number integer or not (C)

Posted 30 October 2018 - 12:05 PM

Quote

I mean, that program doesn't work correctly.

Yes, there are quite a few errors in your code that are much more important than erroneous input validation.

First you have a whole series of variables being used before they have been initialized. For example on line 8 you have several arrays that you're attempting to use a uninitialized variable for the sizes (woops).

Second array sizes should be compile time constants (#defined). While run-time array sizes may be valid in C99 they are either not allowed or Optional (and not supported) in other versions of the C standard. You really should stick with malloc if you want run-time based sizes.

Third your if() statement inside that horrible "goto loop" (please learn to use proper loop mechanisms) will not catch if you entered a digit followed by anything other than another digit or the end of line character. The easiest way of solving this problem is to follow the "loop" with another loop that clears the input buffer.

And lastly, for now. Please don't put multiple statements on one line, this only tends to obscure the program logic and make debugging a total nightmare (lines 22 - 24).


Jim
Was This Post Helpful? 2
  • +
  • -

#6 Ivanofff   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 22-September 18

Re: How to check: is number integer or not (C)

Posted 30 October 2018 - 12:18 PM

View Postmodi123_1, on 30 October 2018 - 11:49 AM, said:


thanks :bananaman:
Was This Post Helpful? 0
  • +
  • -

#7 Ivanofff   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 22-September 18

Re: How to check: is number integer or not (C)

Posted 30 October 2018 - 12:33 PM

View Postjimblumberg, on 30 October 2018 - 12:05 PM, said:

Quote

First you have a whole series of variables being used before they have been initialized. For example on line 8 you have several arrays that you're attempting to use a uninitialized variable for the sizes (woops).

p is initialized like integer (default value 0)
Float array A[p]
In the middle of the programm my array A[p] get elements A[0], A[1], A[2]......
Have I correct understood? :withstupid:

Quote

Third your if() statement inside that horrible "goto loop" (please learn to use proper loop mechanisms) will not catch if you entered a digit followed by anything other than another digit or the end of line character. The easiest way of solving this problem is to follow the "loop" with another loop that clears the input buffer.

Now I made some changes in code
#include <stdio.h>
#include <math.h>

main()
{
    int n, p;
    float y, a, b, x, sum, ser,
          A[p], B[p], X[p], Y[p];
    do
    {
        //Введіть, скільки y-ів ви хочете мати
        printf("Type, how many y u want to have: ");
        scanf("%d", &n);
    }
    while(n <= 0);
    for(p = 0; p < n; p++)
    {
        printf("Type a(%d) = ", p);
        scanf("%f", &A[p]);
        a = A[p];
        printf("Type b(%d) = ", p);
        scanf("%f", &B[p]);
        b = B[p];
        printf("Type c(%d) = ", p);
        scanf("%f", &X[p]);
        x = X[p];
        Y[p] = (a + pow(sin(b*b + x*x), 2))/ pow(b*b + x*x, 1/3);
        y = Y[p];
        printf("y(%d) = %f\n", p, y);
    }
    printf("Y = { ");
    for(p = 0; p < n; p++)
    {
        printf(" %f  ", Y[p]);
    }
    printf(" }\n");
    for(p = 0; p < n; p++)
    {
        sum = sum + Y[p];
    }
    printf("Amount of Y = %f\n", sum);
    ser = sum / n;
    printf("Average of Y = %f", ser);
}


Quote

And lastly, for now. Please don't put multiple statements on one line, this only tends to obscure the program logic and make debugging a total nightmare (lines 22 - 24).

Ok. thanks :smartass:

Jim

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg   User is offline

  • member icon

Reputation: 5593
  • View blog
  • Posts: 17,255
  • Joined: 25-December 09

Re: How to check: is number integer or not (C)

Posted 30 October 2018 - 12:43 PM

Quote

p is initialized like integer (default value 0)
Float array A[p]
In the middle of the programm my array A[p] get elements A[0], A[1], A[2]......
Have I correct understood?

No you haven't understood. On line 8 of your code you are using p before it has been initialized, which produces undefined behavior. By the way p is not default intitialized, it has an undefined value until you initialize it. And secondly p really should be a #defined constant. Using run-time sized arrays when learning the language is a big mistake, unless you're using malloc() to allocate the memory required.

You also need to start using meaningful variable names, your single letter names will just tend to confuse you as the program gets larger.


Jim
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1