2012-02-21 93 views
0

我有一个家庭作业任务,基本上需要用户输入来创建一个高尔夫游戏,询问要打多少个洞,每个洞有多少个标准杆,然后随机生成该人在该洞上获得的内容,然后将其打印出来出。最后,它要求用户再次播放,输入Y或Y代表是,N或N代表否,等等。我的程序中的所有内容都可以正常工作,除非我不能再次播放功能。这里是我的代码,特别是我的主要好戏再次方法:再次玩功能c

int main() { 
int holes, par, strokes, count = 1, low, high, go; 
char *shotName; 
go = 1; 
while (go != 0) { 
    count = 1; 
    holes = readHoles(); 
    do { 
     printf("\nHole number: %i\n", count); 
     par = readPar(holes); 
     low = 1; 
     high = par + 5; 
     strokes = calcStrokes(low, high); 
     shotName = getName(par, strokes); 
     printStatement(count, par, strokes, shotName); 
     count++; 
    }while (count <= holes); 
    go = goAgain(); 
} 
return 0; 

}

int goAgain() { 
char *temp; 
printf("\nWould you like to play again(Y/N)? "); 
scanf("%s", temp); 
while (temp != 'y' || temp != 'Y' || temp != 'n' || temp != 'N') { 
    printf("\nI am sorry that is invalid -- try again\n"); 
    printf("Would you like to play again(Y/N)? "); 
    scanf("%c", &temp); 
} 
if (temp == 'y' || temp == 'Y') { 
    return 1; 
} else { 
    return 0; 
} 

}

我猜IM如何使用while循环,使这项工作只是困惑或做while循环。这是有效的,但是当我运行程序并到达必须输入yes或no的点时,我输入的任何内容都会导致程序突然崩溃。我不知道该怎么做。基本上,我希望用户输入一些东西,如果它是肯定的,再次玩整个游戏,如果没有,结束循环,如果它的其他东西,给他们一个错误,并再次提示他们。任何帮助表示赞赏今晚晚上! :/感谢

回答

2
while (temp != 'y' || temp != 'Y' || temp != 'n' || temp != 'N') { 

温度不能全部4一下子让这将始终为true,将其更改为&&

+0

我想,它仍然不起作用。在运行它崩溃.... – anthony 2012-02-21 00:46:11

0

让我来帮你收拾回路一点。教训:对()是你的朋友?

for (go = 1; go ; go = goAgain() 
    holes = readHoles(); 
    for (count=0; count < holes; count++) { 
     printf("\nHole number: %i\n", 1+count); 
     /* Note: should this be: par = readpar(count+1); ? 
     ** otherwise, it would be loop-invariant 
     ** , and could be hoisted out of the loop. 
     */ 
     par = readPar(holes); 
     low = 1; 
     high = par + 5; 
     strokes = calcStrokes(low, high); 
     shotName = getName(par, strokes); 
     printStatement(count+1, par, strokes, shotName); 
    } 
} 

你可能会认为,count+1是丑陋(两次!)。在这种情况下,你可以改变循环条件:for (count=1; count <= holes; count++) {。但请记住:那是非标准的成语。计数通常从零开始。

+0

我应该这样做只与do和while循环。不能用于循环抱歉,我应该指定 – anthony 2012-02-21 00:48:05

+0

看来你不允许使用作业标签。其他人必须为你做这件事。 – wildplasser 2012-02-21 08:29:52

1

看看你的scanf语句。其中之一,你没有传递你的变量的地址。

bool goAgain() 
{ 
    bool validInput = true; 

    char temp; 
    do 
    { 
     if (!validInput) 
     { 
      printf("\nI am sorry that is invalid -- try again"); 
     } 

     printf("\nWould you like to play again(Y/N)? "); 
     scanf("%c", &temp); // <== Make sure you pass the address of your variable 

     validInput = (temp == 'y' || temp == 'Y' || temp == 'n' || temp == 'N'); 
    } while (!validInput); 

    return (temp == 'y' || temp == 'Y'); 
} 
+0

谢谢!稍微修改一下你的,(即宣布温度外的做),它完美的作品!凡事完美谢谢 – anthony 2012-02-21 01:34:41

+0

@anthony请选择这个答案作为正确的答案,如果这是解决您的问题。 – 2012-02-21 03:17:30

+0

如果你将'temp'声明为'char'而不是'char *',并且使用'%c'而不是'%s',这个答案是正确的。事实上,scanf调用仍然是完全错误的,它正在内存中涂写它不应该写的内容。 (当然,使用'scanf'很难避免这么做......) – jamesdlin 2012-02-21 04:40:41

1
char *temp; 
printf("\nWould you like to play again(Y/N)? "); 
scanf("%s", temp); 

你问scanf读取一个字符串,scanf需要一个地址写入字符串。您提供temp,但temp未初始化为任何内容。您需要为temp分配内存。这天真地完成通过:

char temp[1024]; 

char *temp = malloc(1024); 

这应该解决您的崩溃。但是,1024字节的缓冲区大小完全是任意的,您无法保证用户的输入将适合该缓冲区。

你的具体情况,而不是你能读一个字符,而不是未知长度的字符串:

char temp; 
printf("\nWould you like to play again(Y/N)? "); 
scanf("%c", &temp); 

注意,现在你需要调用scanf&temp。 (如果您不明白为什么,请参阅comp.lang.c FAQ中的Q12.12Q12.12b。)However, beware of the newline left in the input buffer使用此方法。

一般而言,it's best to avoid scanf entirelyscanf难以置信地难以正确使用(更糟糕的是,这很难使用)。

一些其他的东西:

  • printf调用应遵循fflush(stdout),以确保在等待用户输入时的提示出现。
  • 比较temp == 'Y'等等。如果你打算为temp是不是一个单一的char一个字符串(char*),那么这些比较需要是temp[0] == 'Y'