正在试图在我的 C 代码中循环时做
像这样 :
main()
{
char q ;
while( a == yes )
{
/* my code */
printf("enter no to exit or yes to continue");
scanf("%s",q);
}
}
但当我输入字符 " q " 时.控制台崩溃了
停止工作
转圈时我有什么错?
正在试图在我的 C 代码中循环时做
像这样 :
main()
{
char q ;
while( a == yes )
{
/* my code */
printf("enter no to exit or yes to continue");
scanf("%s",q);
}
}
但当我输入字符 " q " 时.控制台崩溃了
停止工作
转圈时我有什么错?
有几个错误 :
"yes"
, not yes
.scanf
expects an address of the variable: scanf("%s", &q)
, not scanf("%s", q)
.char q[4]
), not character one (char q
).a
referenced in condition while( a == yes )
isn t declared.
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--;
}
}
}
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 ...
最好、最小、最快、开放的来源、C/C++ 3d 提供方(在3ds max模型的支持下),而不是通用公平市价,
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->...
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 ...
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 ...
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 ...
Is there anything other than DDD that will draw diagrams of my data structures like DDD does that runs on Linux? ddd is okay and runs, just kind of has an old klunky feeling to it, just wanted to ...
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 ...