[英]CS50 Pset 2: Readability
I'm having issues returning the string as an integer through printf. Heres the error i keep getting, but if I just %i to %s it compiles and prints text.我在将字符串作为 integer 到 printf 返回时遇到问题。这是我不断收到的错误,但如果我只是 %i 到 %s,它会编译并打印文本。 i need to print the number of letters in a string, not the actual text itself.
我需要打印字符串中的字母数,而不是实际文本本身。
readability.c:20:20: error: format specifies type 'int' but the argument has type 'string' (aka 'char *') [-Werror,-Wformat] printf("%i\n", text); readability.c:20:20: error: format specified type 'int' but the argument has type 'string' (aka 'char *') [-Werror,-Wformat] printf("%i\n", text); ~~ ^~~~ %s
~~ ^~~~ %s
my count_letters function seems to work okay but I'm sure I have a mistake somewhere that I'm missing.我的 count_letters function 似乎工作正常,但我确定我在某处遗漏了一个错误。 code here:
代码在这里:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int count_letters(string text);
int main(void)
{
// provides text: statement
printf("Text:");
// gets string from user
string text = get_string(" ");
// should count amount of letters and skip spaces and punctutation
int count_letters(string text);
// prints number of characters in int form
printf("%i\n", text);
}
int count_letters(string text)
{
int letters = 0;
int i;
for (i = 0; letters < strlen(text); i++)
{
if isalpha(text[i])
{
letters++;
}
}
return letters;
}
I don't know where the cs50 header comes from but I assume it's from a course.我不知道 cs50 header 来自哪里,但我认为它来自课程。 I can see two problems:
我可以看到两个问题:
int count_letters(string text);
in main: Here:这里:
// should count amount of letters and skip spaces and punctutation
int count_letters(string text);
You redeclare the function instead of using it and getting it's return value, use something like int result=count_letters(text);
您重新声明 function 而不是使用它并获取它的返回值,使用类似
int result=count_letters(text);
instead.反而。
Here:这里:
// prints number of characters in int form
printf("%i\n", text);
You pass text
which is a string (type char*) but %i (or %d) requires an integer, you can (and should) use the return value of the previous function in this case: so something like printf("%i\n", result);
您传递的
text
是一个字符串(类型为 char*),但 %i(或 %d)需要一个 integer,在这种情况下您可以(并且应该)使用前一个 function 的返回值:所以像printf("%i\n", result);
if you made the modification I suggested in 1).如果您进行了我在 1) 中建议的修改。
Otherwise in standard c (please keep in mind I kept it simple and tried to do something similar to your code so there are missing error checking):否则在标准 c 中(请记住我保持简单并尝试做类似于您的代码的事情,因此缺少错误检查):
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int count_letters(char* text);
int main(void)
{
// provides text: statement
printf("Text:");
// gets string from user with size 14 (+1 for \0)
char text[15];
scanf("%14s", text);
// should count amount of letters and skip spaces and punctutation
int result=count_letters(text);
// prints number of characters in int form
printf("%i\n", result);
}
int count_letters(char* text)
{
int letters = 0;
int i;
for (i = 0; letters < strlen(text); i++)
{
if isalpha(text[i])
{
letters++;
}
}
return letters;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.