【问题标题】:How do I get specific char from an array so that i can put into another array [closed]如何从数组中获取特定的字符,以便我可以放入另一个数组 [关闭]
【发布时间】:2020-02-28 12:03:54
【问题描述】:

我是 C++ 新手 基本上,我被要求编写一个程序,要求输入名字首字母、中间名首字母和整个姓氏,并在一行输出名字首字母,在另一行输出中间名首字母,在另一行输出姓氏。一开始我做了一个程序,但输入并没有全部放在一行中,我不确定如何做到这一点,如果我的解释不是很有帮助,抱歉。

我被要求做什么 编写一个程序,提示用户在单行中输入他们的名字首字母,后跟一个空格,中间名首字母后跟一个空格和整个姓氏。将其存储在 char 数组中。 然后程序应该在一行上输出第一个首字母,然后在另一行上输出中间的首字母,在它自己的一行上输出姓氏。 这个程序是为了演示 char 数组的使用。确保您使用 char 数组完成此程序,并且用户输入的数据存储在单个 char 数组中。之后您可以将名称的各个部分分离到单独的数组中,但是从控制台的初始读取应该将整个输入放入单个 char 数组中。

#include <iostream>
using namespace std;

int main()
{
    char firstNameInitial[2];
    char middleNameInitial[2];
    char surname[11];

cout << "Enter your first name initial: ";
cin >> firstNameInitial;
cout << "Enter your middle name initial: ";
cin >> middleNameInitial;
cout << "Enter your surname initial: ";
cin >> surname;

cout << firstNameInitial << endl << middleNameInitial << endl << surname << endl;

system("pause");
return 0;
}

这是我一开始所做的,但输入在不同的行上,我将如何在 1 中执行它而输出不同

【问题讨论】:

  • 你的 C++ 书应该有很多关于如何使用数组、如何搜索它们、如何将它们的一部分复制到不同的数组或其他地方的示例。你的 C++ 书中是否有一些具体的内容,关于这个,你不清楚或者你有疑问? “我如何做 X”,其中“X”是每本 C++ 书籍中解释的内容,对于 stackoverflow.com 来说并不是一个好问题。
  • 我投票结束这个问题,因为“没有工作的家庭作业”

标签: c++ arrays char


【解决方案1】:

如何从数组中获取特定的字符,以便将其放入 另一个数组?

这就是向初学者教授数组的方式。彻底了解数组的基础知识和使用索引来访问数组元素,从而解决您的问题。可以参考this教程。

解决方案

您可以通过多种方式解决您的任务。我将向您展示使用fgets 函数的解决方案。此外,您不需要任何其他变量来分隔输入。如果您现在具备数组操作的一些基本知识,就可以使用单个数组执行读取和显示操作。

#include <iostream>

// Modify this according to your requirements
#define NAME_SIZE 30

int main() 
{
    // A single character array as per the constraints
    char name[NAME_SIZE];

    // Do it in the C-style!
    fgets(name, NAME_SIZE, stdin);

    // First initial
    std::cout << name[0] << '\n';

    // name[1] is the space that follows

    // Second initial
    std::cout << name[2] << '\n';

    // name[3] is the space that follows

    // Display the surname which starts at index 4
    // Remember that the string terminates with a null character
    for (unsigned i = 4; name[i] != '\0'; ++i) std::cout << name[i];

    // Success
    return 0;
}

【讨论】:

  • 我在我的程序中使用了你的代码,如果我将无符号 i=4 更改为 i = 3,因为 4 使我的第一个姓氏字母不出现,所以姓氏出现的意思是名字首字母和中间名首字母显示为空白。
  • @Bernardocosta 您的问题陈述说首字母后跟一个空格。您在输入时是否忘记了它们?
  • 不,我给空间。 name[0] 没有取第一个首字母,但取了一个空格,而 name[2] 取了第二个空格,我设法用 name[1] 得到第二个首字母,但无论如何我都无法得到第一个首字母
  • @Bernardocosta 我无法通过对这个问题的如此狭隘的描述得出结论。此外,我在提交之前测试了代码。 使用导致此问题的完整代码提出一个单独的问题。我认为除了我给您的代码之外,您还有其他代码,或者输入有问题。因此,不要忘记提及导致此错误的输入,以及任何其他有助于我为您诊断此问题的有用细节。请注意,如果您不回复新问题的链接,我将不会收到通知。
  • 啊,在你说我有一个 cin >> 名字之后,我才设法修复它;不需要,非常感谢您的帮助
【解决方案2】:

您当前的代码(强调我的)是必需的和不是的:

...提示用户在一行中输入...

您当前的程序使用 3 种不同的提示,大多数用户会在自己的行中回答每个元素。

确保 [...] 用户输入的数据存储在单个字符数组中

您正在使用 3 个不同的字符数组作为输入。

提示:要在 C++ 中进行面向行的输入,键的名称为 getline。并且您是否只需要使用 char 数组,您应该使用istream::getline

constexpr int SIZE = 32;
...
char line[SIZE];
cout << "Enter first name initial, a space, middle name initial, a space and your last name\n";
cin.getline(line, SIZE);
...

然后有不同的方法将输入行分成 3 部分。在这里,我将假设(并控制)输入符合预期:

if ((line[0] == ' ') || (line[1] != ' ') || (line[2] == ' ') || (line[3] != ' ') {
    cerr << "Incorrect input" << endl;
    return 1;
}
char *firstinitial = line;
firstinitial[1] = '\0';            // ensure null termination
char *midinitial = line + 2
midinitial[1] = '\0';
char *lastname = line + 4;
for(char *ix = line; ix<line+SIZE; ix++) {   // ensure last name is null terminated
    if (*ix == '\n') {
        *ix = '\0';
        break;
    }
}
line[line+SIZE-1] = '\0'                     // if SIZE characters were read
cout << "First name initial is: " << firstinitial << '\n';
cout << "Middle name initial is: " << midinitial << '\n';
cout << "Last name is: " << lastname<< '\n';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-01
    • 2017-06-27
    • 2020-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    相关资源
    最近更新 更多