I am very new to C and am having some issues with a function I am writing. The assignment is to write a function in which it prompts for height and width parameters to draw a box with. I have the function written and it compiles correctly, but the issue I'm having is that I need to call the function twice and save a width from the first call, and a height from the second. Now, this would be easy if I could use pass-by-reference, but I am not allowed to as the function has to be an int. Here is what I have so far.
//LaxScorupi
//11/21/2021
// C
#include <cstdio>
int GetSize(int min, int max)
{
int range;
while (range < min || range > max)
{
printf("Please enter a value between %d and %d: ", min, max);
scanf("%d", &range);
}
return range;
}
/*
This is where I think I am missing something obvious. Currently, I
have printf in place to
just read the value back to me, but I know my "range" will be saved as
whatever my second call
of GetSize is. I've tried creating variables for height and width, but
am unsure how to take
my return defined as range and store it as two different values.
*/
int main ()
{
int min;
int max;
int range;
range = GetSize(2, 80);
printf("Your width is %d\n", range;
range = GetSize(2, 21);
printf("Your height is %d\n", range);
return 0;
}
Thanks in advance- Lax Scorupi
CodePudding user response:
struct
{
int height;
int width;
}range;
range.width = GetSize(2, 80);
range.height = GetSize(2, 21);
print("Height:%d, Width:%d\n", range.height, range.width);
CodePudding user response:
Basically you can just save them in two different variables and store them in an array so u can use them later.I just added the names and the array into your code down here.
#include<stdio.h>
int GetSize(int min, int max)
{
int range;
while (range < min || range > max)
{
printf("Please enter a value between %d and %d: ", min, max);
scanf("%d", &range);
}
return range;
}
int main ()
{
int min;
int max;
int range1, range2;
range1 = GetSize(2, 80);
printf("Your width is %d\n", range1);
range2 = GetSize(2, 21);
printf("Your height is %d\n", range2);
int a[2] = {range1, range2};
printf("%d %d", a[0], a[1]);
return 0;
}