尝试一次将单个字符读入不确定大小的数组中 [英] Trying to read a single character at a time into an array of indefinite size

查看:72
本文介绍了尝试一次将单个字符读入不确定大小的数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名从事c ++项目的CS学生。我们被指示去声明一个结构,并使用它来读取字符数组,并保持该字符串中使用多少个字母的计数。我们不允许使用字符串。它必须是我们声明的结构的数组。
输入必须是用户想要的时间;该代码必须能够接受新的输入行并被'。'终止。’
我真的在这里苦苦挣扎。我什至不知道从哪里开始。我已经汇总了一些代码,以做为最好的猜测,但是按。后它崩溃了。然后输入,我不知道为什么。

I am a CS student working on a c++ project. We have been instructed to declare a struct and use it to read in an array of chars and keep a tally of how many letters are used in the string. We are not allowed to use a string; it MUST be an array of our declared struct. The input must be as long as the user wants; the code has to be able to accept new lines of input and be terminated by '.' I'm really struggling here. I don't even know where to begin. I've thrown together some code as best-guess for what to do, but it crashes after pressing "." then enter, and I don't know why.

//declare struct
struct data
{
    int tally = 0;
    char letter;
};

//size of string to read in at a time
const int SIZE_OF_CHUNK = 11;
int main()
{
//input chunk of struct
data    input[SIZE_OF_CHUNK];
int     placemark,
        length;

cout << "Enter sequence of characters, '.' to terminate:" << endl;

do
{
    for (int index = 0; (input[index].letter != '\0') && (input[index - 1].letter != '.'); index++)
    {
        cin >> input[index].letter;
        placemark++;
    }

    //I intend to put something here to handle if the code
    needs to read in another chunk, but I want to fix the crashing
    problem first
}
while (input[placemark].letter != '.');

//print out what was read in, just to check
for (int index = 0; input[index].letter != '\0'; index++)
{
    cout << input[index].letter;
}

return 0;
}

我尝试查找如何以单个字符阅读,但还没有到目前为止没有发现任何对我的情况有用的东西。任何关于我在做错事情或在哪里可以找到有用资源的技巧都将不胜感激。

I've tried looking up how to read in a single character but haven't found anything helpful to my circumstances so far. Any tips on what I'm doing wrong, or where I can find helpful resources, would be very much appreciated.

推荐答案

是您确定必须使用声明的结构吗?

Are you sure you must use a declared struct?

如果您只想计算字符出现的次数,则无需存储字符;您只需要存储它出现的次数。因此,只需 unsigned charactersCount [26] ,每个索引都映射到一个字母(即索引 0 表示 a ,索引 1 表示 b )。每当出现字母时,只需增加该索引的数量即可。

If you just want to count the number of times a character has appeared, you don't need to store the character; you just need to store the number of times it appeared. So just unsigned lettersCount[26], and each index maps to a letter (i.e. index 0 means a, index 1 means b). Whenever a letter appears, just increase the count of that index.

您可以使用 ASCII 将字母映射到索引。每个字母都由一个十进制数字表示,您可以在 ASCII表中查找该数字。例如,字母 a 用十进制值97表示, b 是98等等。这个数字不断增加,我们可以利用。因此,如果要将字母映射到索引,只需要做 value-97 value-'a'。例如,如果您阅读字母 a ,则从中减去97,您将得到 0 ,是你想要的。获得索引后,只需增加一个字母的计数即可,只需简单的 ++

You can map a letter to the index by making use of ASCII. Every letter is represented by a decimal number that you can look it up at ASCII table. For example, the letter a is represented by the decimal value 97, b is 98 and so on. The number increases successively, which we can make use of. So if you want to map a letter to an index, all you need to do is just value - 97 or value - 'a'. For example, if you read in the letter a, take away 97 from that and you'll get 0, which is what you want. After getting the index, it's just a simple ++ to increment the count of that letter.

关于处理大写和小写(例如,对它们进行相同或不同的处理),这取决于您自己决定如何执行(如果您能够理解我的解释,这应该非常简单)。

Regarding the treatment of uppercase and lowercase (i.e. treat them the same or differently), it'll be up to you to figure it out how to do it (which should be fairly simple if you can understand what I've explained).

这篇关于尝试一次将单个字符读入不确定大小的数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆