【问题标题】:cs50 initials pset2 unexpected errorcs50 首字母 pset2 意外错误
【发布时间】:2016-09-24 00:08:33
【问题描述】:

当我尝试在我的 for 循环中使用“i

int main()
{
    int i;
    int n;
    //Program to get the user's name and reply with their capitalized initials
    {
    //Ask user for their name
    printf("What is your full name?\n");
    }
    //look for 1st character of each part of name given
    string name = GetString();

    for (i = 0; (n = strlen (name)); i < n; i++)
    {
        printf("Your intitals are %c", toupper(name[0]));
        {
            if (isspace(name[i]))
            {
            printf("%c", toupper(name[i+1]));
            }
        printf("!\n");
        }
    }
    return 0;
}

【问题讨论】:

  • 您可能只想打印一次“您的姓名缩写”消息,而不是每个用户输入的字符一次。你甚至不知道空格后面有一个字母;您最终可能会打印一个空字符,这不是一个好主意,尽管它不会造成太大的伤害。祝贺"!\n" 也应该在for 循环之外。

标签: c if-statement cs50 isspace


【解决方案1】:

您的for() 语法错误。那里只能有 2 个; 字符。如果要初始化多个变量,用,分隔它们,而不是;

for (i = 0, (n = strlen (name)); i < n; i++)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多