c - 使用代码块编写重复提示输入的 C 程序

标签 c codeblocks

要点,我正在使用Codeblocks来编写这个程序,并且需要以仅编写C语言的目的来编写它,仅此而已。我希望它尽可能基本。这就是我到目前为止所遇到的困惑。

我需要这个程序继续向用户询问一个值,直到1-29之间,直到达到176。一旦超过176,我需要做的就是打印数字的总和,输入的最大数字和最小数字输入的号码。

从那里程序需要循环并询问是否将输入另一组数字。使用适当的用户输入终止程序。我需要输出以下信息:

  • 所有数字的总和
  • 输入的最大数字
  • 输入的最小数字

如果有人有任何建议会很有帮助。

/* Matthew Gambrell CSC 120 12/4/14*/

#include <stdio.h>

main ()
{
  int total, num, large, small, again;

  again=1;

  while (again==1);
  {
    total = 0;
    large = 0;
    small = 50;
  }

  while(total<=176);
  {
    num=inputNum();
    total=tot(num, total);
    large=CheckLarge(num, total);
    small=CheckSmall(num, small);}
    printresult(total, large, small);
    printf("play again");
    printf("1=yes 0=no");
    scanf("%d", &again);
  }

  float inputNum();
  {
    float badnum, num;
    badnum=1;
    while (badnum==1);
      printf("please enter a number between 1 and 29");
      scanf("%f", &num);
    if(num>0 and num<30)
      badnum = 0;
    else
      printf("error renter");
      getch("press enter to continue");
    return(num)
  }

  tot(num, total)
  total += num
  return (total)
}

return(num)

最佳答案

就像你的同学所说,你可以使用循环中的循环来做到这一点。您的代码结构使这看起来像是您的目标。

首先,如评论中所述,删除 while(..); 语句末尾的分号。这将允许控制进入您在其下方定义的 block 。

其次,您可能希望将第一个 while 循环的右括号放在第二个 while 循环结束之后。这是您的“循环中的循环”结构。

第三,您目前没有任何真正的方式来显示您的结果。通常,您的 int main(){ ... } 函数会返回一个与程序运行情况相关的数字。虽然您可以让它返回 total 变量,但您更有可能希望它返回 0;,并在程序执行期间将变量输出到屏幕。

例如,正如您在 inputNum(..) 中的输入解析中所做的那样,您可以编写

printf('The total is %i\n', total);
printf('The largest number entered was %i\n', large);
printf('The smallest number entered was %i\n', small);

这些数字(largesmall)需要位于您的(第一个)while 循环内 - 它们应该在每次循环重新启动时重置,但不是每次都重置您输入一个数字。

关于c - 使用代码块编写重复提示输入的 C 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27307179/

相关文章:

c - 有一些我没用过的奇怪的 malloc 函数

c++ - 为什么同一编译器的不同版本会给出不同的结果?

c - CodeBlock 中的 kbhit 函数在 C 语言中不起作用

c++ - 如何在 Code::Blocks IDE 中使用 Rcpp

c++ - sdl_image 找不到我的jpg图片;不会加载

c - 如何在 gcc 中清除屏幕?

对于大于 4GiB 的数组,在 64 位系统上调用 calloc 失败

c - 通过*指针分配后,相邻的内存块将被零填充。为什么?

c - objcopy 生成的原始二进制文件太大

c++ - 在C++应用程序中嵌入Lua脚本会把Lua部分编译成机器码吗?