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

Page 1 of 1

## 7 Replies - 837 Views - Last Post: 30 October 2018 - 12:43 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=413370&amp;s=c9d6695fb197dadc3214601fa6631f1d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Ivanofff

Reputation: 1
• Posts: 27
• 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

or this trulala

Idea is

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

```#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

• Suitor #2

Reputation: 14691
• Posts: 58,720
• 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.

### #3 Ivanofff

Reputation: 1
• Posts: 27
• Joined: 22-September 18

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

Posted 30 October 2018 - 11:38 AM

modi123_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");
}
```

### #4 modi123_1

• Suitor #2

Reputation: 14691
• Posts: 58,720
• 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

### #5 jimblumberg

Reputation: 5652
• Posts: 17,354
• 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

### #6 Ivanofff

Reputation: 1
• Posts: 27
• Joined: 22-September 18

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

Posted 30 October 2018 - 12:18 PM

modi123_1, on 30 October 2018 - 11:49 AM, said:

thanks

### #7 Ivanofff

Reputation: 1
• Posts: 27
• Joined: 22-September 18

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

Posted 30 October 2018 - 12:33 PM

jimblumberg, 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?

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

Jim

### #8 jimblumberg

Reputation: 5652
• Posts: 17,354
• 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

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }