我對 C 非常陌生,并且我正在撰寫的函式存在一些問題。任務是撰寫一個函式,在該函式中它提示輸入用于繪制框的高度和寬度引數。我撰寫了函式并正確編譯,但我遇到的問題是我需要呼叫該函式兩次并保存第一次呼叫的寬度和第二次呼叫的高度。現在,如果我可以使用傳遞參考,這將很容易,但我不允許,因為函式必須是 int。這是我到目前為止所擁有的。
//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;
}
提前致謝 - Lax Scorupi
uj5u.com熱心網友回復:
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);
uj5u.com熱心網友回復:
基本上,您可以將它們保存在兩個不同的變數中并將它們存盤在一個陣列中,以便您以后可以使用它們。我只是在此處將名稱和陣列添加到您的代碼中。
#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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/367749.html
上一篇:fork()開始執行
