简体   繁体   English

如何编写正确的 do..while 循环?

[英]How to write proper do..while loop?

for some reason this code throws an error when I'm trying to compile it.出于某种原因,当我尝试编译此代码时,它会引发错误。 What is wrong, can you tell me please?有什么问题,你能告诉我吗? I'm taking a CS50 course and it's actually the first assignment.我正在参加 CS50 课程,这实际上是第一个作业。

The program has to prompt the user for input until the condition is false.程序必须提示用户输入,直到条件为假。

#include <stdio.h>
#include <cs50.h>

int n;

do
{
    n = get_int();
}
while ( n < 0 || n > 23 );

This is the Error:这是错误:

pyramid.c:6:1: error: expected identifier or '('
do
^
pyramid.c:10:1: error: expected identifier or '('
while ( n < 0 || n > 23 );

This is the generic example of using do {...} while();这是使用do {...} while();的通用示例do {...} while(); loop in C. cs50 is not the standard C header, this is homecooked for the students who take cs50 course. C. cs50 中的循环不是标准的 C 头文件,这是为参加 cs50 课程的学生准备的。

You should check the get_int() ;您应该检查get_int() definition in the cs50.h header file. cs50.h 头文件中的定义。

Code:代码:

#include <stdio.h>
int main()
{
    int number;

    // Do while loop is executed at least once
    do
    {
      printf("Enter a number from 0-23: ");
      scanf("%d", &number);

    }
    while(number < 0 || number > 23);

    printf("Number = %d\n",number);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM