Tag: while循环

C中的简单循环和字符串长度

我是新手C.在Visual Studio 2015中编写,我试图通过使用fgets安全地提示用户输入字符串。 我想使用fgets来获取字符串,检查字符串是否太长,并重新启动用户,直到他们输入一个好的字符串。 这是我的代码 /* * Nick Gilbert * COS317 Lab 2 Task 2 */ #include “stdafx.h” int main() { char str[10]; int isValid = 0; while (isValid == 0) { printf(“Please enter a password: “); fgets(str, 10, stdin); if (strlen(str) == 9 && str[8] != ‘\n’) { //http://stackoverflow.com/questions/21691843/how-to-correctly-input-a-string-in-c printf(“Error! String is too long\n\n”); […]

在c中重新启动while循环而没有整数

我试图让这个代码工作,但我不知道如何重新启动内部while循环。 我该怎么办? /* * Return a pointer to the first occurrence of any character in * in the given or NULL if the contains no characters * in . ***** * YOU MAY *NOT* USE INTEGERS OR ARRAY INDEXING. ***** */ char *find_any_ptr(char *string, char* stop) { char *newstring = (char*)0; while(*stop != ‘\0’){ while(*string […]

while循环不终止 – 浮点运算

我正在使用浮点值处理C程序,下面是我的代码。 #include #include #include int main() { int counter = 0; float quarter = 0.25; float dime = 0.10; float nickel = 0.05; float penny = 0.01; float change = 0.00; printf(“hi, how much do i owe u?\t”); scanf(“%f”, &change); while(change > 0.0) { if(change >= quarter) { change -= quarter; printf(“quarter %.2f\n”, quarter); } […]

如何在文件*流中的特定点停止并扫描某些值?

我有一个名为test.txt的文件,该文件包含: 10.213123 41.21231 23.15323 当我看到我希望扫描后的3个数字,如下所示: x = 10.213123 y = 41.21231 z = 23.15323 我有点困惑,因为在这里,fgets扫描整条线,我怎样才能将3个数字扫描成双线? 因为数字可以是各种长度? 我这样做是为了打印出它从文件中读取的内容,但我无法绕过它。 void print_lines(FILE *stream) { char line[MAX_LINE_LENGTH]; while (fgets(line, MAX_LINE_LENGTH, stream) != NULL) { fputs(line, stdout); } }

运行时检查失败变量周围的堆栈已损坏

#include main() { int num[9], i = 0, count = 0; while (i<10) { scanf("%d", &num[i]); if (num[i] % 2 == 0) { count++; } i++; } printf("we have %d double numbers\n", count); } 运行时检查失败#2 – 变量周围的堆栈已损坏 我该怎么办?

循环跳过scanf它的条件。

我不明白为什么While循环只是加速并跳过scanf的char? 它甚至不会要求我的输入,只是循环就像没有明天一样。 #include int main() { int number; int multiply, ans; char choice; printf(“————————————-“); printf(“\n MULTIPLICATION TABLE “); printf(“\n————————————-“); do { printf(“\nEnter an integer number:”); scanf(“%d”, &number); printf(“\nMultiplication of %d is :-\n”, number); printf(“\n”); for(multiply=1; multiply<11; multiply++){ ans = number * multiply; printf(" %d", ans); } printf("\n"); printf("\nWould you like to continue? [Y] for Yes,[N] […]

C – 如何读取输入

它是家庭作业的一部分,但它不是主要部分 我已经通过自己手动更改数据来完成主要function,但我真的不知道如何正确读取用户输入。 以下是输入的内容: 3 1 2 9 6 1 1 3 4 4 6 0 所以基本上第一个输入是下一个输入的#的大小。 因此对于3,数组大小为3,[1,2,9]为元素,对于6,数组大小为6,[1,1,3,4,4,6]为元素 当输入为0时,程序终止。 我认为通过使用while循环并说当输入[0] =’0’并且中断时,我可以终止程序,但我不知道如何将其他输入转换为char数组。 如您所见,有空格,因此scanf将以不同的整数读取char数组。 在我获得char的输入后,我相信我可以使用atoi将其恢复为整数… 那么,帮助我如何编码以便正确地获得用户输入… 也许这太模糊了:这是我有点想要的版本: while(1) { scanf(“%d”, &ui); if(ui == 0) { break; } else { size = ui; int temp[size]; for(c = 0; c < size; c++) { scanf("%d", &input); temp[c] = input; } […]

控制命令行的提示

我用C制作了基于udp的客户端和服务器示例程序。 我的代码如下 [服务器] #include #include #include #include #include #include #include #include int printerror2(char func[], int errnum) { printf(“%s error = %d:%s\n”, func, errnum, strerror(errnum)); perror(func); return errnum; } int printerror(char func[]) { return printerror2(func, errno); } int main(int argc, char *argv[]) { int ret; /* WSADATA wsaData; ret = WSAStartup(MAKEWORD(2,2), &wsaData); if (ret != 0) […]

二进制打印在C中不起作用

我正在尝试使用c中的32位位掩码打印二进制文件,但二进制表示不会在if语句中打印出来。 unsigned int bit_mask = 2147483648; int decimal = 2; printf(“\nBinary representation of 2: \n”); while(bit_mask > 0){ if((decimal & bit_mask) == 0) printf(“0”); else printf(“1”); bit_mask = bit_mask >> 1; } decimal = 255; printf(“\n\nBinary representation of 255: \n”); while(bit_mask > 0){ if((decimal & bit_mask) == 0) printf(“0”); else printf(“1”); bit_mask = bit_mask >> […]

如何跳转到代码的开头?

我正在制作一个程序,要求用户输入一个输入。 如果用户输入输入,则显示输入,然后完成程序。 如何让程序从头开始? 我的代码是这样构建的:(只显示构建而不是代码本身) please enter user input: while (x != y) { if ( x == y ) { printf(“printing something”); } else if (x > y ) { printf(“printing something”); } else { printf(“printing something”); } }