Hello all. I am taking a beginning C++ class and am having problems with a section in my assignemnt. I thought with arrays, you had to assign them a size. From reading the assignment requirements below, the instructor is saying that we are not allow to limit the size. Am I reading the requirements section correcly, or, is there a way create an array without limiting its size?? Would any of you be able to point me in the right direction? Below are the requirents as well as the required output from the program.
Assignment requirements:
Let me say this another way in case this makes more sense. You will need to declare a struct with two fields, an int and a char. (This should be declared above main, so that it is global; that is, so it can be used from anywhere in your program.) Then you need to declare an array (not global!) whose elements are those structs. The int in each struct will represent the number of times that the char in the same struct has occurred in the input. This means the count of each int should start at 0 and each time you see a letter that matches the char field, you increment the int field.
Don't forget that limiting the length of the input is prohibited. If you understand the above paragraph you'll understand why it is not necessary to limit the length of the input.
Assingment Output:
Enter a sequence of characters (end with '.'): do be Do bo. xyz
The number of different characters you can read is limited so you can read the input of any size even with a fixed length array.
You can have variable-sized arrays in C++ but you don't need them for this.
In C or C++ as far as I know no array can be defined without a specific size. But I could be wrong.
However, there is a simple solution to your problem. Instead of using arrays you might want to use pointers to store unspecified amount of data. You can declare a pointer of char type in your struct and increment the count of the int variable as data gets stored in the other variable.
Here is one example which ran perfectly when I compiled it. It does what you want to do, it reads a line from the input buffer till the full stop '.' character is encountered and then displays what the user has entered. Along with the length of the string.
#include <cstdio.h>
#include <cstdlib.h>
#include <conio.h>
typedefstruct
{
int i;
char *ch;
} test_t;
void pause(void)
{
printf("\nPress any key to continue...");
fflush(stdin);
_getch();
return;
}
int main()
{
test_t test = {0};
atexit(pause); /* Pause :) */
test.ch = (char *)malloc(sizeof(char));
printf("Enter a line: ");
while((*(test.ch + test.i++) = _getche()) != '.') /* Get the input from the user */
{
realloc(test.ch, (test.i+1)*sizeof(char)); /* Allocate one extra space for the next character*/
if(test.ch == 0)
{
printf("Error reallocating memory\n"); /* Print error message upon realloc() failure */
exit(EXIT_FAILURE);
}
}
*(test.ch + test.i) = '\0'; /* Terminate the string with a NULL character */
printf("\nYou entered: %s which is %d characters long\n", test.ch, test.i); /* Print what the user entered */
free(test.ch) /* Free allocated memory */
return 0;
}
But yes I didn't know we could resize arrays. I'm new to C and I have to learn a lot of things... But I felt that I could answer to this thread correctly.
The code I posted will work in C++. (at least it does on my C++ compiler). I prefer printf than using cout; don't ask why.
EDIT: Do you mean I can store 7 values in an array originally declared to hold 5? I don't know how to do that without using pointers and the first link you posted applies to pointers only - as far as I know - and not with arrays.
Can you please post an example which resizes an array? I'm curious as to how you do that without using pointers.
You are using C headers, they aren't standard in C++ - you should have <cstdio> -
Most C++ compilers are also C compilers so standard libraries provide both versions
Do you mean I can store 7 values in an array originally declared to hold 5? I don't know how to do that without using pointers and the first link you posted applies to pointers only - as far as I know - and not with arrays.
An array is an unmovable pointer to the first element. You can create an array on the heap of any size ( you'll need to copy it if you want to enlarge it )
Can you please post an example which resizes an array? I'm curious as to how you do that without using pointers.
Thanks, Bazzy. That is interesting... I'm just a beginner and I didn't even know a header file named <deque> existed.
I was trying to help someone but I got helped instead. Thanks again I'll do a bit of research on that later.... I'm too lazy to do that atm.
But what I posted also works :P (I'll change the stdlib to cstdlib). The reason I've been using "stdio.h" because that is what is there in the Text book I have. (now that I think about it, there is no mention of "cstdio.h" in there; all the examples have include<stdio.h>)
The number of different characters you can read is limited so you can read the input of any size even with a fixed length array.
You can have variable-sized arrays in C++ but you don't need them for this.
After reading his post, it makes sense now. My fixed array size will be 26.