Invalid operand to binary * - c

#include<stdio.h>
float length() {
float length;
printf("Input Length: ");
scanf("%f", &length);
return length;
}
float width(){
float width;
printf("Input Width: ");
scanf("%f", &width);
return width;
}
float area(){
float area=length * width;
return area;
}
float perimeter(){
float perimeter=(length*2) + (width*2);
return perimeter;
}
int main(){
printf("Area = %f\n", area());
printf("Perimeter = %f\n", perimeter());
}
Invalid Operand under Area and Perimeter
skafhasfhsafsakhfsakfhasfkhsafksahfakshfaskfhasfkshfaskfhsfashfkashfasfhasfhasfksahfasfasfasfasfsfsfsfas

length and width in area and perimeter are computed as function pointers, for which multiplication is not defined.

Here is quick fix:
#include <stdio.h>
float width(){
float width;
printf("Input Width: ");
scanf("%f", &width);
return width;
}
float length(){
float length;
printf("Input Length: ");
scanf("%f", &length);
return length;
}
float area() {
return length() * width();
}
float perimeter(){
float perimeter=(length()*2) + (width()*2);
return perimeter;
}
int main(){
printf("Area = %f\n", area());
printf("Perimeter = %f\n", perimeter());
}
but here is how would I do it:
#include <stdio.h>
float width(){
float width;
printf("Input Width: ");
scanf("%f", &width);
return width;
}
float length(){
float length;
printf("Input Length: ");
scanf("%f", &length);
return length;
}
float area(float length, float width) {
return length * width;
}
float perimeter(float length, float width){
return length*2 + width*2;
}
int main(){
float l = length();
float w = width();
printf("Area = %f\n", area(l, w));
printf("Perimeter = %f\n", perimeter(l, w));
}

Related

Function average and stdDev const int tab[ ]. Average problems

I have to use:
float average(const int tab[], int size);
float stdDev(const int tab[], int size);
to printf average and stdDev in C.
I have problem with average and i think with const int.
When i add const int tab[101] i have error with a1;
So how can i make it work with const int (if i can).
And if it is anything wrong with this code.
Any help will be helpful.
#include<stdio.h>
#include<math.h>
float average(const int tab[], int size);
float stdDev(const int tab[], int size);
int main()
{
float ave, std;
int a1;
int j;
int tab[101];
printf("Podaj liczby: ");
for(j=0; j<=99; j++)
{
a1 = scanf("%d", &tab[j]);
if(a1<1)
{
printf("Incorrect input");
return 1;
}
if(tab[0]==0)
{
printf("not enough data available");
return 2;
}
if(tab[j]==0)
{
break;
}
}
ave = average(tab, j);
printf("%.2f\n", ave);
std = stdDev(tab, j);
printf("%.2f", std);
return 0;
}
float average(const int tab[], int size)
{
int i;
float y=0, x;
if(size<=0)
{
return -1;
}
for(i=0; i<size; i++)
{
x = x + tab[i];
}
y = x/size;
return y;
}
float stdDev(const int tab[], int size)
{
int i;
float y, z, z1, z2=0, z3=0;
if(size<=0)
{
return -1;
}
y = average(tab, size);
for(i=0; i<size; i++)
{
z = tab[i] - y;
z1 = pow(z, 2);
z2 = z2 + z1;
z=0;
z1=0;
}
z3 = sqrt(z2/size);
return z3;
}
You define the variable x in average here:
float y=0, x;
without giving it a value. Then here:
x = x + tab[i];
you are reading its value without setting it anywhere beforehand. Because you never gave x a value, its value will be indeterminate and reading it will cause undefined behavior, which means that your program could e.g. print garbage output.
Always initialize your variables:
float y=0, x=0;

The function in my code does not work

I tried running the program but it's not running correctly. The problem is at the functions, but I don't know exactly where. I declared the functions first, then I tried calling them in main. However, I'm not sure that's the case. I think the problem is at the function definition ? But I have no clue what else to do. If anyone could look at it and point it out to me that would be great.
#include <stdio.h>
#include <math.h>
float computeMonthlyPayment(float p, float YearRate, float YearTerm);
float computeMonthlyInterest(float p, float YearRate);
void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n);
void printTable(float MonthlyPayment, float p, float YearRate,float YearTerm);
int main()
{
float n, p, r, MonthlyPayment, YearRate, YearTerm;
printf("Enter the loan amount: ");
scanf("%f", &p);
if (p <= 0.0)
printf("\nERROR: Invalid rate; must be greater than 0\n");
while (p <= 0.0)
{
printf("\nEnter the loan amount: ");
scanf("%f", &p);
}
printf("\nEnter annual interest rate: ");
scanf("%f", &YearRate);
if (YearRate <= 0.0 || YearRate > 30.0)
printf("\nERROR: Invalid rate; must be > 0.0 amd <= 30.0.\n");
while (YearRate <= 0.0 || YearRate > 30.0)
{
printf("\nEnter annual interest rate: ");
scanf("%f", &YearRate);
}
printf("\nEnter the term of the loan (in years): ");
scanf("%f", &YearTerm);
if (YearTerm <= 0.0)
printf("\nERROR: Invalid rate; must be greater than 0\n");
while (YearTerm <= 0.0)
{
printf("\nEnter the term of the loan (in years): ");
scanf("%f", &YearTerm);
}
float computeMonthlyInterest(float p, float YearRate);
float computeMonthlyPayment(float p, float YearRate, float YearTerm);
void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n);
return 0;
}
float computeMonthlyPayment(float p, float YearRate, float YearTerm)
{
float r = YearRate/12;
float n = YearTerm*12;
float MonthlyPayment = 0;
MonthlyPayment = (r*p)/1-((1+r)/n);
return MonthlyPayment;
}
float computeMonthlyInterest(float p, float YearRate)
{
float r = 0;
r = ((YearRate)/12)/12;
return r;
}
void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n)
{
printf("LOAN INFORMATION\n");
printf("-----------------------\n");
printf("Initial loan amount: %.2f\n", p);
printf("Annual interest rate: %.3f\n", YearRate);
printf("Monthly interest rate: %.3f\n", r);
printf("Term of loan (years): %f\n", YearTerm);
printf("Term of loan (months): %f\n", n);
printf("Monthly payment amount: %.2f\n", MonthlyPayment);
}
Take a look at lines 48..50, in main().
int main()
{
// ...
float computeMonthlyInterest(float p, float YearRate);
float computeMonthlyPayment(float p, float YearRate, float YearTerm);
void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n);
// ...
}
Those are the functions' prototypes, not how you actually call them. You're supposed to call them the same way you call printf() or scanf(), by passing them the appropriate parameters. As it stands now, you're just redeclaring the functions in main(), and not actually calling them. Try something like this:
int main()
{
// ...
r = computeMonthlyInterest(p, YearRate); // r should be labeled better.
MonthlyPayment = computeMonthlyPayment(p, YearRate, YearTerm);
printLoanInfo(p, MonthlyPayment, YearRate, r, YearTerm, n);
// ...
}
This still isn't 100% fixed, though, because n isn't being initialised. While printLoanInfo() says what it's for (this is also how I identified r), you forgot to set it in main(). So..
int main()
{
// ...
r = computeMonthlyInterest(p, YearRate);
n = YearTerm * 12; // You did this in computeMonthlyPayment(), but not here.
MonthlyPayment = computeMonthlyPayment(p, YearRate, YearTerm);
printLoanInfo(p, MonthlyPayment, YearRate, r, YearTerm, n);
// ...
}
So, with those changes in mind, your main() should probably look like this:
int main()
{
float n, p, r, MonthlyPayment, YearRate, YearTerm;
printf("Enter the loan amount: ");
scanf("%f", &p);
if (p <= 0.0)
printf("\nERROR: Invalid rate; must be greater than 0\n");
while (p <= 0.0)
{
printf("\nEnter the loan amount: ");
scanf("%f", &p);
}
printf("\nEnter annual interest rate: ");
scanf("%f", &YearRate);
if (YearRate <= 0.0 || YearRate > 30.0)
printf("\nERROR: Invalid rate; must be > 0.0 amd <= 30.0.\n");
while (YearRate <= 0.0 || YearRate > 30.0)
{
printf("\nEnter annual interest rate: ");
scanf("%f", &YearRate);
}
printf("\nEnter the term of the loan (in years): ");
scanf("%f", &YearTerm);
if (YearTerm <= 0.0)
printf("\nERROR: Invalid rate; must be greater than 0\n");
while (YearTerm <= 0.0)
{
printf("\nEnter the term of the loan (in years): ");
scanf("%f", &YearTerm);
}
// Remove these lines:
// float computeMonthlyInterest(float p, float YearRate);
// float computeMonthlyPayment(float p, float YearRate, float YearTerm);
// void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n);
// Add these lines:
r = computeMonthlyInterest(p, YearRate);
n = YearTerm * 12;
MonthlyPayment = computeMonthlyPayment(p, YearRate, YearTerm);
printLoanInfo(p, MonthlyPayment, YearRate, r, YearTerm, n);
return 0;
}
The problem is not the function definitions. It is the fact that, in main(), you have copy and pasted these lines.
float computeMonthlyInterest(float p, float YearRate);
float computeMonthlyPayment(float p, float YearRate, float YearTerm);
void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n);
These are function declarations which inform the compiler on HOW to call the functions, but do not actually call them.
If you intend to actually call them, you will need to do something like
variable1 = computeMonthlyInterest(p, YearRate);
variable2 = computeMonthlyPayment(p, YearRate, YearTerm);
printLoanInfo(p, MonthlyPayment, YearRate, r, YearTerm, n);
Essentially this removes the type specification from the function arguments and passes actual values. variable1 and variable2 are variables that you will need to define (possibly with more meaningful names) before these statements to hold returned values. Presumably you will also need to write more code in main() to use those variables.
Keeping in mind what the other answers have said concerning calling your functions correctly. The code below should work. I had to rework your computeMonthlyPayment, since it was calculating incorrectly.
#include <stdio.h>
#include <math.h>
float computeMonthlyPayment(float p, float YearRate, float YearTerm);
float computeMonthlyInterest(float p, float YearRate);
void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n);
void printTable(float MonthlyPayment, float p, float YearRate,float YearTerm);//why are you defining this if it doesn't exist?
int main()
{
float n=0, p, r=0, MonthlyPayment=0, YearRate, YearTerm;
printf("Enter the loan amount: ");
scanf("%f", &p);
if (p <= 0.0)
printf("\nERROR: Invalid rate; must be greater than 0\n");
while (p <= 0.0)
{
printf("\nEnter the loan amount: ");
scanf("%f", &p);
}
printf("\nEnter annual interest rate: ");
scanf("%f", &YearRate);
if (YearRate <= 0.0 || YearRate > 30.0)
printf("\nERROR: Invalid rate; must be > 0.0 amd <= 30.0.\n");
while (YearRate <= 0.0 || YearRate > 30.0)
{
printf("\nEnter annual interest rate: ");
scanf("%f", &YearRate);
}
printf("\nEnter the term of the loan (in years): ");
scanf("%f", &YearTerm);
if (YearTerm <= 0.0)
printf("\nERROR: Invalid rate; must be greater than 0\n");
while (YearTerm <= 0.0)
{
printf("\nEnter the term of the loan (in years): ");
scanf("%f", &YearTerm);
}
printLoanInfo(p,MonthlyPayment,YearRate,r,YearTerm,n);
}
float computeMonthlyPayment(float p, float YearRate, float YearTerm)
{
float r = YearRate/(12*100);
float n = YearTerm*12;
return (p*r*pow(1 + r, n)) / (pow(1 + r, n) - 1); // just return the calculation
}
float computeMonthlyInterest(float p, float YearRate)
{
float r = 0;
return (r = ((YearRate)/12)/12);
}
void printLoanInfo(float p, float MonthlyPayment, float YearRate, float r, float YearTerm, float n)
{
printf("LOAN INFORMATION\n");
printf("-----------------------\n");
printf("Initial loan amount: %.2f\n", p);
printf("Annual interest rate: %.3f\n", YearRate);
printf("Monthly interest rate: %.3f\n", computeMonthlyInterest(p,YearRate)); //call function computeMonthlyInterest
printf("Term of loan (years): %.0f\n", YearTerm);
printf("Term of loan (months): %.0f\n", (YearTerm * 12));//simple calc for number of months
printf("Monthly payment amount: %.2f\n", computeMonthlyPayment(p,YearRate,YearTerm)); // call function computeMonthlyPayment
}

Am I properly passing parameters and passing by reference?

I am getting no output for the grosspay and taxespaid functions for this programming assignment I am writing to keep track of employee payroll. I am just wondering if I am properly passing parameters and passing by reference, which is the method we are supposed to use. My class doesn't meet often and the book sucks so I would appreciate any advice.
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5
void userinput(char name[][20], int hoursworked[], float rate[]);
void calculatepay(float rate[], int hoursworked[]);
float tax(float grosspay[]);
float totalpay(float grosspay[], float taxespaid[]);
void print(char name[][20], float rate[], int hoursworked[], float grosspay[], float sum, float taxespaid[]);
int main(){
int hoursworked[]={};
float rate[]={};
char name[][20]={};
float grosspay[]={};
float taxespaid[]={};
float sum;
userinput(name, hoursworked, rate);
calculatepay(rate, hoursworked);
sum = tax(grosspay);
totalpay(grosspay, taxespaid);
print(name, rate, hoursworked, grosspay, sum, taxespaid);
return (0);
}
void userinput(char name[][20], int hoursworked[], float rate[]){
for(int i=0; i<SIZE; i++){
printf("Enter name: ");
scanf("%s", &name[i] , 20);
if(name[i][0] == '-' && name[i][1] == '1'){
break;
}
printf("Enter hours worked: ");
scanf("%d", &hoursworked[i]);
if(hoursworked[i] == -1){
break;
}
printf("Enter hourly rate: ");
scanf("%f", &rate[i]);
if(rate[i] == -1){
break;
}
puts("\n");
}
}
void calculatepay(float rate[], int hoursworked[]){
float grosspay[]={};
for(int i=0; i<SIZE; ++i){
if(hoursworked[i]<=40){
grosspay[i] = rate[i] * hoursworked[i];
}
else{
grosspay[i] = (40*rate[i])+((hoursworked[i] - 40)*1.5*rate[i]);
}
}
}
float tax(float grosspay[]){
float taxespaid[]={};
for(int i=0; i<SIZE; ++i){
taxespaid[i] = grosspay[i]*0.2;
return taxespaid[i];
}
return 0;
}
float totalpay(float grosspay[], float taxespaid[]){
float sum = 0.0;
for (int i=0; i<SIZE; i++)
{
sum=sum+(grosspay[i] - taxespaid[i]);
}
return sum;
}
void print(char name[][20], float rate[], int hoursworked[], float grosspay[], float sum, float taxespaid[]){
for(int i=0; i<SIZE; i++){
printf("Pay to: %s\n", name[i] , 20);
if(name[i][0] == '-' && name[i][1] == '1'){
break;
}
printf("Hours worked: %d\n", hoursworked[i]);
if(hoursworked[i] == -1){
break;
}
printf("Hourly rate: %.2f\n", rate[i]);
if(rate[i] == -1){
break;
}
if(hoursworked[i]<=40){
printf("Gross Pay: %.2f\n", grosspay[i]);
}
else
{
printf("Gross Pay: %.2f\n", grosspay[i]);
printf("Base Pay: %.2f\n", rate[i]*40);
printf("Overtime Pay: %.2f\n", grosspay[i] - (rate[i]*40));
}
printf("Taxes paid: %.2f\n", taxespaid[i]);
printf("Net pay: %.2f\n\n", grosspay[i] - taxespaid[i]);
}
printf("Total paid to all employees: %.2f\n", sum);
}
I hate to be the bearer of bad news, but there is no pass-by-reference in C. The only method of parameter passing that C defines is pass-by-value, in 6.5.2.2p4:
In preparing for the call to a function, the arguments are evaluated, and each parameter is assigned the value of the corresponding argument.
Perhaps you intended to ask about C++, in which case... No, you're not using pass-by-reference.
Otherwise, if you intended to ask how C mimics pass-by-reference, it is by using pointers (that is, the parameters have pointer types and pointer values).
In void userinput(char name[][20], int hoursworked[], float rate[]);, for example, name, hoursworked and rate are all pointer types. The values that are passed will be pointer values. The pointer values point into the objects that are to be modified, so the same outcome is possible (that is, the changes that are made to the array are still visible once the function returns).
In case you're curious as to why this doesn't technically qualify as pass-by-reference, it's because the unary *pointer or array[subscript] (or pointer[subscript] operators are required to change the value pointed to, where-as when pass-by-reference is used you should merely be able to assign a value without those like: array = value;. It makes more sense if you look at it as a test in writing a swap function:
void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
}
If you try calling this function, you will notice that it hasn't successfully swapped its arguments. If you were to use pass-by-value, those three lines would have swapped its arguments. For example, in C++:
void swap(int& x, int& y) {
int temp = x;
x = y;
y = temp;
}
This will swap the arguments. All I had to change was the function signature; I didn't have to change the way the function is used or the code in the function itself... That is pass-by-reference.
When I declared the variables before main() and initialised the arrays with SIZE then it worked fine :)
#define SIZE 5
void userinput(char name[][20], int hoursworked[], float rate[]);
void calculatepay(float rate[], int hoursworked[]);
float tax(float grosspay[]);
float totalpay(float grosspay[], float taxespaid[]);
void print(char name[][20], float rate[], int hoursworked[], float grosspay[], float sum, float taxespaid[]);
int hoursworked[SIZE];
float rate[SIZE];
char name[SIZE][20];
float grosspay[SIZE];
float taxespaid[SIZE];
float sum;
int main(){
userinput(name, hoursworked, rate);
calculatepay(rate, hoursworked);
sum = tax(grosspay);
totalpay(grosspay, taxespaid);
print(name, rate, hoursworked, grosspay, sum, taxespaid);
return (0);
}
void userinput(char name[][20], int hoursworked[], float rate[]){
int i;
for(i=0; i<SIZE; i++){
printf("Enter name: ");
scanf("%s", &name[i] , 20);
if(name[i][0] == '-' && name[i][1] == '1'){
break;
}
printf("Enter hours worked: ");
scanf("%d", &hoursworked[i]);
if(hoursworked[i] == -1){
break;
}
printf("Enter hourly rate: ");
scanf("%f", &rate[i]);
if(rate[i] == -1){
break;
}
puts("\n");
}
}
void calculatepay(float rate[], int hoursworked[]){
float grosspay[SIZE];
int i;
for(i=0; i<SIZE; ++i){
if(hoursworked[i]<=40){
grosspay[i] = rate[i] * hoursworked[i];
}
else{
grosspay[i] = (40*rate[i])+((hoursworked[i] - 40)*1.5*rate[i]);
}
}
}
float tax(float grosspay[]){
int i;
float taxespaid[SIZE];
for(i=0; i<SIZE; ++i){
taxespaid[i] = grosspay[i]*0.2;
return taxespaid[i];
}
return 0;
}
float totalpay(float grosspay[], float taxespaid[]){
float sum = 0.0;
int i;
for (i=0; i<SIZE; i++)
{
sum=sum+(grosspay[i] - taxespaid[i]);
}
return sum;
}
void print(char name[][20], float rate[], int hoursworked[], float grosspay[], float sum, float taxespaid[]){
int i;
for(i=0; i<SIZE; i++){
printf("Pay to: %s\n", name[i] , 20);
if(name[i][0] == '-' && name[i][1] == '1'){
break;
}
printf("Hours worked: %d\n", hoursworked[i]);
if(hoursworked[i] == -1){
break;
}
printf("Hourly rate: %.2f\n", rate[i]);
if(rate[i] == -1){
break;
}
if(hoursworked[i]<=40){
printf("Gross Pay: %.2f\n", grosspay[i]);
}
else
{
printf("Gross Pay: %.2f\n", grosspay[i]);
printf("Base Pay: %.2f\n", rate[i]*40);
printf("Overtime Pay: %.2f\n", grosspay[i] - (rate[i]*40));
}
printf("Taxes paid: %.2f\n", taxespaid[i]);
printf("Net pay: %.2f\n\n", grosspay[i] - taxespaid[i]);
}
printf("Total paid to all employees: %.2f\n", sum);
}

getting three errors

This code is showing following errors:
missing ) before type
calc: too few arguments to call
syntax error ) Visual stuio 2013 platform
MyCode:
#include "math.h"
void main()
{
float num[5];
float (calc (float num[5]));
calc(float num);/* transferring control to calc function)*/
getch();
}
float calc(float nun[5])
{
int i;
float num[5];
float sum, avg, sqmn1, sumsqmn = 0, sqsd = 0; float sd;
printf("\nEnter 5 numbers");
for (i = 0; i < 5; i = i + 1)
{
scanf("%f", &num[i]);
}
sum = 0;
for (i = 0; i < 5; i = i + 1)
{
sum = sum + num[i];
}
avg = sum / 5;
for (i = 0; i < 5; i = i + 1)
{
sqmn1 = (avg - num[i])*(avg - num[i]);
sumsqmn = sumsqmn + sqmn1;
}
sqsd = sumsqmn / 5;
sd = sqrt(sqsd);
printf("\nThe sum is %f", sum);
printf("\nThe average is %f", avg);
printf("\nThe stabdard deviation is %f", sd);
getch();
}
float (calc (float num[5]));
in your main(), what is this exactly?
IMO, it can be,
float ff;
ff = calc(num);
Other than that,
#include <stdio.h> is missing.
Forward declaration of float calc(float nun[5]) is missing.
You can rewite your main() as
int main()
{
float num[5];
float ff;
ff = calc(num);/* transferring control to calc function)*/
getch();
return 0;
}
but then also, you're passing num from main() to calc() but i see you never used it. What are you upto?

Checking For user input between certain limit if not asking again until correct input is entered

My program compiles correctly but i am having problem when i run it. The first scanf (width) works correctly but when i try with another scanf(height) i get segmentation fault 11.
And can i do this program to work without using pointers. (Also i need limit checker function because i have to use it again and again in my program).
#include <stdio.h>
void limitChecker(int x, int y, int* input);
int main(void)
{
int* x;
int* y;
printf("Enter the width of the windows. (3 - 5) : ");
scanf("%d", x);
limitChecker(3, 5, x);
printf("width: %d \n", *x);
printf("Enter the height of the windows. (2 - 4) : ");
scanf("%d", y);
limitChecker(2, 4, y);
printf("Height: %d \n", *y);
}
void limitChecker(int x, int y, int* input)
{
while(!(*input>=x && *input<=y))
{
printf("Please enter a value between (%d - %d): ",x,y);
scanf("%d", input);
}
}
You need to use a reference to the variables used in the scanf().
For example, scanf("%d", &x);
The first parameter of scanf() is for the type of data, and the following parameter(s) are a list of pointers to where you would like the user input to be stored.
CORRECTED CODE:
#include <stdio.h>
void limitChecker(int x, int y, int* input);
int main(void)
{
int x;
int y;
printf("Enter the width of the windows. (3 - 5) : ");
scanf("%d", &x);
limitChecker(3, 5, &x);
printf("width: %d \n", x);
printf("Enter the height of the windows. (2 - 4) : ");
scanf("%d", &y);
limitChecker(2, 4, &y);
printf("Height: %d \n", y);
}
void limitChecker(int x, int y, int* input)
{
while(!(*input>=x && *input<=y))
{
printf("Please enter a value between (%d - %d): ",x,y);
scanf("%d", input);
}
}
You did not allocate memory to hold x and y.
Allocate them on the stack and then use the & address of operator to obtain a pointer to that memory.
#include <stdio.h>
int limitChecker(int x, int y, int input);
int main(void)
{
int x;
int y;
printf("Enter the width of the windows. (3 - 5) : ");
scanf("%d", &x);
x = limitChecker(3, 5, x);
printf("width: %d \n", x);
printf("Enter the height of the windows. (2 - 4) : ");
scanf("%d", &y);
y = limitChecker(2, 4, y);
printf("Height: %d \n", y);
}
int limitChecker(int x, int y, int input)
{
while(!(input>=x && input<=y))
{
printf("Please enter a value between (%d - %d): ",x,y);
scanf("%d", &input);
}
return input;
}
If you want x and y to be pointers then you have to assign them valid memory before you use them.
int * x = malloc(sizeof(int));
int * y = malloc(sizeof(int));
#include <stdio.h>
int limitChecker(int x, int y, int value){
return x <= value && value <= y;
}
int inputInt(void){
//x >= 0
int x = 0;
int ch;
while('\n'!=(ch=getchar())){
if('0'<=ch && ch <= '9')
x = x * 10 + (ch - '0');
else
break;
}
return x;
}
int main(void){
int x, y;
do{
printf("Enter the width of the windows. (3 - 5) : ");
x = inputInt();
}while(!limitChecker(3, 5, x));
printf("width: %d \n", x);
do{
printf("Enter the height of the windows. (2 - 4) : ");
y = inputInt();
}while(!limitChecker(2, 4, y));
printf("Height: %d \n", y);
return 0;
}

Resources