|
查看: 1236|回复: 10
|
C programming 急
[复制链接]
|
|
|
C programming 急Hi
我的menu 有
(D)epth :
(S)ave
(L)oad
(P)rint
(Q)uite
当我click D 的时候会 load 去double depth(double seconds2Bottom); 请问在 我的char menuSelect(const char prompt[]); switchstatment 的 case 'd' 那边要怎样写。。。。。 还有 我这样的 code 对吗??
谢谢
double depth(double seconds2Bottom);
char menuSelect(const char prompt[]);
bool yesNo(const char prompt[]);
int main(void)
{
char selection;
printf("(D)epth: Enter pit location. Compute depth \n");
printf("(S)ave : Write pits date to file \n");
printf("(L)oad : Read mine shaft information from file. \n");
printf("(P)rint: Display mine shaft information.\n");
printf("(Q)uit : Exit program.\n");
printf("enter your choice : ", menuSelect);
scanf("%c", &selection);
}
double depth(double seconds2Bottom)
{
/* Acceleration of gravity */
const double G = 9.81;
double toBottom; /* Time to reach the bottom of the shaft */
do
{
printf("\nEnter time to bottom: ");
scanf("%lf", &toBottom);
fflush(stdin);
printf("\nShaft depth is %.2f m\n", depth(toBottom));
} while( yesNo("Do you want to take another measure"));
return -0.5 * G * seconds2Bottom * seconds2Bottom;
}
char menuSelect(const char prompt[])
{
char selection;
switch(selection)
{
case 'd':
case 'D':
depth(seconds2Bottom);
}
} |
|
|
|
|
|
|
|
|
|
|
发表于 26-10-2008 03:05 PM
|
显示全部楼层
回复 1# vzhen 的帖子
1. double seconds2Bottom , seconds2Bottom 的值是多少? 从那里拿的?
2. char menuSelect(const char prompt[]) , 这个是要做什么的? 我猜你要得是用来做namu selection 吧, 那么array 就去除吧。
3. char menuSelect(const char prompt[]) 者个好像没return 东西, 给他void 吧。
4. printf("enter your choice : ", menuSelect); menuSelect 是什么来的?而且好像这个variable 没出现过的, 去除吧
5. 你要将你的selecttion pass 给 menuSelect,那么就将你的argument 写进去。
6. bool yesNo(const char prompt[]); c programming 没有 bool 的。。
7. } while( yesNo("Do you want to take another measure")); 这个是什么来的?
基本上, 除了seconds2Bottom的问题 ( 因为我不懂哪里来得) 之外, 应该是这样写。- double depth(double seconds2Bottom);
- void menuSelect(const char prompt);
- //bool yesNo(const char prompt); NO BOOL IN C PROGRAMMING
- int yesNo(const char prompt);
- int main(void)
- {
- char selection;
- printf("(D)epth: Enter pit location. Compute depth \n");
- printf("(S)ave : Write pits date to file \n");
- printf("(L)oad : Read mine shaft information from file. \n");
- printf("(P)rint: Display mine shaft information.\n");
- printf("(Q)uit : Exit program.\n");
- printf("enter your choice : ");
- scanf("%c", &selection);
- menuSelect(selection);
- return 0;
- }
- double depth(double seconds2Bottom)
- {
- /* Acceleration of gravity */
- const double G = 9.81;
- double toBottom; /* Time to reach the bottom of the shaft */
- do
- {
- printf("\nEnter time to bottom: ");
- scanf("%lf", &toBottom);
- fflush(stdin);
- printf("\nShaft depth is %.2f m\n", depth(toBottom));
- } while( yesNo("Do you want to take another measure")); // wrong logic
- return -0.5 * G * seconds2Bottom * seconds2Bottom;
- }
- void menuSelect(const char prompt)
- {
- // double seconds2Bottom = ????? ;
- //
- switch(prompt)
- {
- case 'd':
- case 'D':
- depth(seconds2Bottom);
- break;
- }
- }
复制代码 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 26-10-2008 03:38 PM
|
显示全部楼层
回复 2# onlylonly 的帖子
嗨,
谢谢你的回复。。。。
那个yesNo 其实是没有问题的。。。因为有另一部分没paste 上来。。。。
那个 double depth 是 return -0.5 * G * seconds2Bottom * seconds2Bottom; 计算出来的。。。。
我的问题是, 一开始时menu .... 然后click D 就会出现 double depth 的function
Enter the time to bottom :
可是 我的case 'D':
这里 link 不到 double depth 的function.
seconds2bottom 就是 我输入的 号码 double toBottom ............
对了 seconds2bottom = ?? 这里应该放什么。。。。。。。。。
[ 本帖最后由 vzhen 于 26-10-2008 03:45 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 26-10-2008 03:49 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 26-10-2008 03:49 PM
|
显示全部楼层
回复 3# vzhen 的帖子
我的问题你没看懂啊。。
你在 argument passing, seconds2Bottom ( 不是 double depth, 在看多一次)你的argument 振么可能为uninitialize?
而且你的 yesno 为 bool, 在c里面更本没有bool 的, 怎么可能没问题的?
至于如何输入d 就可以渠道function, 我的code哪里已经写了, 直接呼唤menuSelect就可以了 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 26-10-2008 04:05 PM
|
显示全部楼层
回复 5# onlylonly 的帖子
如果我要这样, 你可以帮我修正吗
#include <stdio.h>
#include <stdbool.h>
double depth(double seconds2Bottom);
void menuSelect(char selection);
bool yesNo(const char prompt[]);
int main(void)
{
char selection;
printf("(D)epth: Enter pit location. Compute depth \n");
printf("(S)ave : Write pits date to file \n");
printf("(L)oad : Read mine shaft information from file. \n");
printf("(P)rint: Display mine shaft information.\n");
printf("(Q)uit : Exit program.\n");
printf("enter your choice : ");
scanf("%c", &selection);
menuSelect(selection);
return 0;
}
double toBottom;
do
{
printf("\nEnter time to bottom: ");
scanf("%lf", &toBottom);
fflush(stdin);
printf("\nShaft depth is %.2f m\n", depth(toBottom));
} while( yesNo("Do you want to take another measure"));
// 我想把这个do while 放在 int main 应该怎么修改 。。。。。就是说 选了 D 才会跳出 Enter time to bottom :
double depth(double seconds2Bottom)
{
const double G = 9.81;
return -0.5 * G * seconds2Bottom * seconds2Bottom;
}
void menuSelect(char selection)
{
double seconds2Bottom = ???; // 这里应该放什么
switch(selection)
{
case 'd':
case 'D':
depth(seconds2Bottom);
break;
case 'q':
case 'Q':
// exit 是 return 0; 吗?
}
}
/* Display a question and returns true if the answer is 'y' or 'Y' */
bool yesNo(const char prompt[])
{
char answer;
printf("\n%s (Y/N)? ", prompt);
answer = getchar();
fflush(stdin);
return (answer == 'y' || answer == 'Y');
}
[ 本帖最后由 vzhen 于 26-10-2008 04:11 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 26-10-2008 04:12 PM
|
显示全部楼层
回复 6# vzhen 的帖子
你的second2bottom 都没用到, 没什么不在argument哪里放void呢?
这几天不是很得空, 过些时候才写个sample code 给你行吗? 大约明天或后天, 你这个是否紧急的? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 26-10-2008 04:38 PM
|
显示全部楼层
相当急。。。。
星期3交。。。。。。。
明天你有时间吗?
可不可以在msn 教我, 我send 题目和 code 给你。。。。好吗
我也是奇怪为什么 secondstoBottom 没用到但是能算出答案。。。。 |
|
|
|
|
|
|
|
|
|
|
发表于 26-10-2008 06:24 PM
|
显示全部楼层
回复 8# vzhen 的帖子
看看这个code是否是你要的
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdlib.h>
- void depth(void);
- void menuSelect(const char prompt);
- bool yesNo(const char prompt[]);
- int main(void)
- {
- char selection;
- printf("(D)epth: Enter pit location. Compute depth \n");
- printf("(S)ave : Write pits date to file \n");
- printf("(L)oad : Read mine shaft information from file. \n");
- printf("(P)rint: Display mine shaft information.\n");
- printf("(Q)uit : Exit program.\n");
- printf("enter your choice : ");
- scanf(" %c", &selection);
- menuSelect(selection);
- return 0;
- }
- void depth(void)
- {
- /* Acceleration of gravity */
- const double G = 9.81;
- double toBottom; /* Time to reach the bottom of the shaft */
- do
- {
- printf("\nEnter time to bottom: ");
- scanf(" %lf", &toBottom);
- fflush(stdin);
- printf("\nShaft depth is %.2f m\n", -0.5 * G * toBottom * toBottom);
- } while( yesNo("Do you want to take another measure"));
- }
- void menuSelect(char selection)
- {
- switch(selection)
- {
- case 'd':
- case 'D':
- depth();
- break;
- case 'q':
- case 'Q':
- exit(0);
- // exit
- }
- }
- /* Display a question and returns true if the answer is 'y' or 'Y' */
- bool yesNo(const char prompt[])
- {
- char answer;
- printf("\n%s (Y/N)? ", prompt);
- getchar(); // eliminate linebreak
- answer = getchar();
- fflush(stdin);
- return (answer == 'y' || answer == 'Y');
- }
复制代码
[ 本帖最后由 onlylonly 于 26-10-2008 06:26 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 27-10-2008 01:40 AM
|
显示全部楼层
OnlyOnly,
那个问题我解决廖。。。。。就把do while 放上去int main 就可以了。
我又有三个问题。
1. exit(0); = compile 后出现warning 因为required standard code 所以warning message 是不可以的,我试过 return 0; 有error. 在这里要放什么才对。
2. 如果在 输入data 的时候 required 一个 space
Example : abc 123 必须要有一个space between abc 123 在 printf 还是 scanf 要加些什么?
3 .如果在menu 的时候输入没有固定 的 char 要怎样loop back 回 menu 从新输入到正确为字?
char selection;
double toBottom;
char name;
printf("(D)epth: Enter pit location. Compute depth \n");
printf("(S)ave : Write pits date to file \n");
printf("(L)oad : Read mine shaft information from file. \n");
printf("(P)rint: Display mine shaft information.\n");
printf("(Q)uit : Exit program.\n");
printf("enter your choice : ");
scanf("%c", &selection);
menuSelect(selection);
if((selection) !='d' && (selection) != 'D')
printf("invalid entry");
else
这里应该要怎样loopback 回去?
THANKS |
|
|
|
|
|
|
|
|
|
|
发表于 18-11-2008 11:55 PM
|
显示全部楼层
原帖由 vzhen 于 27-10-2008 01:40 AM 发表 
OnlyOnly,
那个问题我解决廖。。。。。就把do while 放上去int main 就可以了。
我又有三个问题。
1. exit(0); = compile 后出现warning 因为required standard code 所以warning message 是不可以的,我试 ...
那个function 的 return 是 void, 当然不能回传 value. 用 "return;"
loopback 的话就用个while loop就可以了,选对了就invalidate那个 statement |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|