English 中文(简体)
如何在绕圈 c 时做出
原标题:How to make while loop in c
  • 时间:2012-05-23 13:30:16
  •  标签:
  • c
  • while-loop

正在试图在我的 C 代码中循环时做

像这样 :

main()
{
char q ;
while( a ==  yes  )   
{
    /* my code */
    printf("enter no to exit or yes to continue");
    scanf("%s",q);
 } 
}

但当我输入字符 " q " 时.控制台崩溃了

停止工作

转圈时我有什么错?

最佳回答

有几个错误 :

  1. String literals should be surrounded by double quotes: "yes", not yes .
  2. String literals can t be compared to character variables.
  3. scanf expects an address of the variable: scanf("%s", &q), not scanf("%s", q).
  4. Apparently, you need array variable (char q[4]), not character one (char q).
  5. Variable a referenced in condition while( a == yes ) isn t declared.
  6. You won t get your message on the screen since it s being buffered until is printed.

所以,你可能需要的是:

#include <stdio.h>
#include <string.h>

#define INP_SIZE 3
int main() {
    char inp[INP_SIZE + 1] = { 0 };
    while (strcmp(inp, "yes")) {
        printf("enter yes to continue or whatever else to exit
");
        scanf("%3s", inp);
    }
    return 0;
}

P.S.我曾考虑建造一个格式字符串,以避免重复 3 ,但我的懒惰赢了。

问题回答

您无法将字符串与 a 比较 : yes 。 您需要使用 strcmp 函数 。

你需要这样的东西:

int main(int argc, char **argv)
{
    char a[200];
    strcpy(a, "yes");
    while( !strcmp(a, "yes") )
    {
       /* my code */
       printf("enter no to exit or yes to continue");
       scanf("%s",a);
    } 
}

使用 scanf ("%s", & amp;q); 而不是 scanf ("%s",q);

您没有通过 q 变量的地址, 在 scanf 函数中 。

You have a number of mistakes. 1. a char is only a single character - infact it is actually a number 2. you write yes using single quotes. This gives a char type and you should only ever have a single character in single quotes. e.g. y 3. in c strings are held as arrays of char and you can t just compare them like you can integets.

我检查过这个,但尝试一下:

main() {
char buf[255]; //Set up an array of chars to hold the string
buf[0] =  /0 ; //set the first charactory in the array to a null charactor.
               //c strings are null terminated so this sets it to an empty string

while ( strcmp(buf,"yes")==0) { //we have to use the strcmp function to compare the array
                                //also yes in double quotes is a null terminated char array so
printf("enter no to exit or yes to continue:"); //looks better with a colon
scanf("%s",buf); //scan f function will fill the buffer with a null terminated array
printf("
"); //a new line here is nice
时 时
时 时

这也许对你有用,我没有C编译器可以手动,所以我无法测试

时 时

时 时;

没有 q 的地址, 请在 q 之前尝试添加一个 & amp;, 并添加一个 strcmp (a, “ 是 ” 来正确评价表达式 。

class WhileLoopExample {
     public static void main(String args[]){
        int i=10;
        while(i>1){
          System.out.println(i);
          i--;
        }
     }
}




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签