![]() |
|
#1
|
|||
|
|||
Problem with pointers programHello everyone!
I am having trouble with this program I am supposed to write for homework. In its basic form it takes in an array of characters (which might include spaces), of constant size 10 (pointers must be used), and outputs the number of spaces and the number of vowels. That goes perfectly okay. But there are other conditions. The size of the array should automatically increase once the string length increases beyond 9, 19, 29 etc. That is if I enter the 10th character the size should be 20 and so on. Also the program should stop taking in the characters once ENTER is pressed. I have tried various ways, such as using a do while with the condition (ch!='\n'), but they do not work. When I place the code in a do while, the output (no of vowels and spaces) is not displayed. I hope I've explained it correctly. Could anyone please help with those two conditions? As the things I tried do not show any signs of working right, there must be something within the code logic that I do not understand. I'll be grateful if anyone can explain. Thank you. This is the basic program which does work (till size equals 10). CPP / C++ / C Code:
Why doesn't this code CPP / C++ / C Code:
work when I place it just after cin.getline function? |
|||
|
#2
|
|||
|
|||
Re: Problem with pointers programYour array is already set to 10 characters long before you get the input. Because of that, your array will only store the first 9 characters, leaving the tenth for the null character. Your array must be large enough to store the number of values required before you try to store those values into the array. This is also why your if statement with the strlen function doesn't work. You've already created the array with a constant size. You can't just change the variable a... you need to copy the array you have to a new array that is the new size.
|
|
#3
|
|||
|
|||
Re: Problem with pointers programBut thats the condition set in the question. I am supposed to initially declare it with a constant size of 10 and then it has to somehow automatically increase by 10 each time the characters exceed the size.
|
|
#4
|
|||
|
|||
Re: Problem with pointers programWell, then once it reaches 9 characters, you need to create a new array with a size of 20, copy the old array into the new one, and continue reading characters.
|
|
#5
|
|||
|
|||
Re: Problem with pointers programThanks blakekl. But what happens after 20?
|
|
#6
|
|||
|
|||
Re: Problem with pointers programYou do the same thing. Create a new array of size 30, and then copy your old array into it. Then delete your old array and continue adding numbers. Once you get to 39 digits, repeat the same process: create a new array of size +10, copy the old array into the new one, delete the old array, and continue adding digits. It's not too hard to do inside of a loop.
|
|
#7
|
|||
|
|||
Re: Problem with pointers programSo I put the two arrays in a loop to be repeated on infinitely? Thanks a lot blakekl! I get the logic now. Whether I'll be actually able to do it is another question of course but I'll try implementing this. Thank you.
|
|
#8
|
|||
|
|||
Re: Problem with pointers programno problem. Good luck.
|
|
#9
|
|||
|
|||
Re: Problem with pointers programThis is a basic operation in C style string. You can use vector after you already know this one because vector take care memory allocation for you.
Another problem is don't write your statement inside main. I will break it apart into small function. __________________
Linux is the best OS in the world. |
|
#10
|
|||
|
|||
Re: Problem with pointers programWe haven't done vectors yet. Unfortunately I haven't tried this program yet, will break it into functions as you say when I attempt it. Thanks.
|
Recent GIDBlog
Master?s Degree by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| malloc/free usage, and general good programming behavior | lavaka | C Programming Language | 7 | 14-Jun-2007 19:50 |
| Text-Based Roulette Game | mfm1983 | CPP / C++ Forum | 5 | 29-Nov-2006 12:20 |
| Bus error (core dumped) Problem on my C++ program | JKSung5295 | CPP / C++ Forum | 1 | 24-Sep-2006 16:22 |
| pointers problem: 0xC0000005: Access Violation | soc | C Programming Language | 5 | 30-Sep-2005 08:19 |
| [Tutorial] Pointers in C (Part I) | Stack Overflow | C Programming Language | 1 | 08-Apr-2005 18:35 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The