GIDForums

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Mar-2008, 22:53
hira hira is offline
Junior Member
 
Join Date: Nov 2007
Posts: 47
hira is on a distinguished road

Problem with pointers program


Hello 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:
#include<iostream>
#include<iomanip>


using namespace std;

int main()
{
	char *line;
	int a=10, counter=0, counter2=0;
	char ch;

 

	line=new char[a];

    cout<<"Enter the characters"<<endl;
	cin.getline(line, a);


	for (int j=0;j<a;j++)
	{
				switch (line[j])
		{
		case 'a':
		case 'A':
		case 'e':
		case 'E':
		case 'i':
		case 'I':
		case 'o':
		case 'O':
		case 'u':
		case 'U':counter++;
			break;
                case ' ':counter2++;
			break;
				
				}
	}
	
		
  	cout<<endl<<"No of vowels="<<counter<<endl<<"No of spaces="<<counter2<<endl;

	delete [] line;


	return 0;
}

Why doesn't this code
CPP / C++ / C Code:
	if (strlen(line)>=a)
		a=a+10;

work when I place it just after cin.getline function?
  #2  
Old 22-Mar-2008, 04:21
blakekl blakekl is offline
New Member
 
Join Date: Nov 2007
Posts: 21
blakekl is on a distinguished road

Re: Problem with pointers program


Your 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  
Old 22-Mar-2008, 04:27
hira hira is offline
Junior Member
 
Join Date: Nov 2007
Posts: 47
hira is on a distinguished road

Re: Problem with pointers program


But 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  
Old 23-Mar-2008, 01:21
blakekl blakekl is offline
New Member
 
Join Date: Nov 2007
Posts: 21
blakekl is on a distinguished road

Re: Problem with pointers program


Well, 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  
Old 24-Mar-2008, 18:16
hira hira is offline
Junior Member
 
Join Date: Nov 2007
Posts: 47
hira is on a distinguished road

Re: Problem with pointers program


Thanks blakekl. But what happens after 20?
  #6  
Old 26-Mar-2008, 00:50
blakekl blakekl is offline
New Member
 
Join Date: Nov 2007
Posts: 21
blakekl is on a distinguished road

Re: Problem with pointers program


You 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  
Old 26-Mar-2008, 08:21
hira hira is offline
Junior Member
 
Join Date: Nov 2007
Posts: 47
hira is on a distinguished road

Re: Problem with pointers program


So 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  
Old 26-Mar-2008, 09:57
blakekl blakekl is offline
New Member
 
Join Date: Nov 2007
Posts: 21
blakekl is on a distinguished road

Re: Problem with pointers program


no problem. Good luck.
  #9  
Old 26-Mar-2008, 19:14
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 348
Peter_APIIT is on a distinguished road

Re: Problem with pointers program


This 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  
Old 01-Apr-2008, 07:04
hira hira is offline
Junior Member
 
Join Date: Nov 2007
Posts: 47
hira is on a distinguished road

Re: Problem with pointers program


We 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 GIDBlogMaster?s Degree by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 13:08.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.