如何在不使用argv - CS50 pset2的情况下获取字符串数组

时间:2017-06-27 19:50:55

标签: cs50

我目前正在参加CS50哈佛课程,而且我遇到问题集2。 我创建了这个带有名称并打印首字母的程序,它在命令行中取名。我如何使用 get_string()而不是argv,而argc非常不正统和草率,所以我可以提示用户给我/她的名字。谢谢。

#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>

int main(int argc, string argv[])
{

//How do I use Get_string() so I don't have to use argv and argc?? 


//iterate over strings on the vector (words)
    for (int i = 0; i < argc; i++)
    {
        //prints the 0 character of each string, use "toupper" to convert into capital letters
        printf("%c", toupper(argv[i][0]));

    }
    printf("\n");
}

2 个答案:

答案 0 :(得分:0)

你考虑使用getline()?而不是get_string?

答案 1 :(得分:0)

使用数组, 假设有10个名字

string name[10];
for (int i = 0; i < 10; i++)
{
    name[i] = get_string("Enter your name: /n");
}
相关问题