我今年夏天刚刚开始使用操作系统课程,我们需要使用C语言进行编程。
我只用C ++编码,所以我是C的新手。我们的第一个任务是输入多个字符串的简单提示。我们最多只能读取10个字符串。
我的问题是scanf()
没有等待循环中的用户输入,程序只是在我有机会输入任何内容之前显示提示10次。我已经读过,因为scanf()
在缓冲区中有换行符,所以我在%c
语句scanf()
之前添加了一个额外的空格,但是没有解决问题。
#include <stdio.h>
int main (void)
{
int SIZE = 10;
char* states[SIZE];
int cnt = 0;
printf ("Here\n");
while (cnt < 10)
{
printf ("Enter State and Pop: ");
scanf (" %c ", &states[cnt]);
cnt++;
printf ("%d\n", cnt);
}
return 0;
}
答案 0 :(得分:0)
char* states[SIZE]; //pointers not allocated with memory
scanf (" %c ", &states[cnt]); //wrong format specifier and argument
这是未定义的行为,因为您使用了错误的格式说明符,并且还发送了错误的参数以便在字符串中进行扫描
以及更多指向字符串的指针,即states
未分配内存
为避免这种情况,首先为states
数组
包含stdlib.h
标题文件
为states
for(int i=0; i<10; i++)
{
states[i] = malloc(max_size_of_string);
}
或者你可以用这种方式动态分配内存:
char states[10][max_size_of_strings];
最后以这种方式使用scanf:
scanf (" %(size_of_string-1)[^\n]", states[cnt]);
,即如果你将max_size_of_string指定为10则使用这种方式:
scanf (" %9[^\n]", states[cnt]);
这是为了避免覆盖终止空字符
答案 1 :(得分:0)
这是一个工作示例
// for printf() and scanf()
#include <stdio.h>
// for malloc() and free()
#include <stdlib.h>
// Global constants should be define'd
#define MAX_STRING_SIZE 100
#define STACK_SIZE 10
int main()
{
// pointer to an array of strings (roughly)
char *states[STACK_SIZE];
// used as the iterator throughout
int i;
// allocate memory for every string in the array "states"
for (i = 0; i < 10; i++) {
// Normally sizeof(char) == 1, but that's how it works in general:
// the number of elements times the size of on element, be it
// a char (1) an integer (2,4 or 8 these times) or some
// complicated struct
states[i] = malloc(MAX_STRING_SIZE * sizeof(char));
// check if it actually happened
if (states[i] == NULL) {
// the error message should explain a bit more than just:
fputs("malloc() failed\n", stderr);
// free the already allocated memory
while (i >= 0) {
free(states[i--]);
}
// exit with a return code signaling an error
exit(EXIT_FAILURE);
}
}
// reset iterator
i = 0;
// I would have used a for-loop, but it's a matter of taste, I guess
while (i < 10) {
printf("Enter State and Pop: ");
// scan 99 characters of the input between start of line and line-end
// one less than MAX_STRING_SIZE to leave room for the string-end '\0'
// scanf() returns the number of characters read or EOF in case of an error
// You might find it useful at one time.
scanf(" %99[^\n]", states[i]);
// Print what we just gathered
printf("State %d is %s\n", i, states[i]);
// increment iterator, if that is not obvious--but sometimes it isn't
i++;
}
// Just a string to stdout, so puts(). The function printf() is too expensive for that
puts("All states:");
for (i = 0; i < 10; i++) {
printf("State %d is %s\n", i, states[i]);
// normally not necessary, the OS does it here at the end main but
// it should always be the habit of a good programmer to clean up at the end
free(states[i]);
}
exit(EXIT_SUCCESS);
}
答案 2 :(得分:0)
第一个getchar()获取输入字符串,第二个getchar获取Enter键u按;)并且它是:
#include <stdio.h>
#define SIZE 10
int main(void)
{
char* states[SIZE];
int cnt = 0;
printf("Here\n");
while (cnt < 10)
{
printf("Enter State and Pop: ");
states[cnt] = (char*)getchar();
getchar();
cnt++;
printf("%d\n", cnt);
}
return 0;
}