C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28695
Number of posts: 94715

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Find and count spaces, letters and words using pointers Posted by obtuse on 21 Nov 2009 at 12:08 PM
I'm fairly new to pointers so I'm having some problem with this. I'm trying to write a program to receive a sentence as input, use pointers to find the spaces in the sentence, count the words (which would be numbers of spaces + 1) and find the number of letters. The compiler tells me:
c:\temp\lab11out\lab11out.cpp(37) : error C2664: 'getWords' : cannot convert parameter 2 from 'char *' to 'char *[]'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\temp\lab11out\lab11out.cpp(40) : error C2664: 'displayInfo' : cannot convert parameter 2 from 'char *' to 'char *[]'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\temp\lab11out\lab11out.cpp(58) : error C2440: '=' : cannot convert from 'char' to 'char *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
c:\temp\lab11out\lab11out.cpp(64) : error C2440: '=' : cannot convert from 'char' to 'char *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast 


This is the code:

#include <iostream>
#include <iomanip>

using namespace std;

void getString(char* string, int max_length);
void getWords(char *orig_string, char* words[], int* word_length, int& wc);
void displayInfo(char* orig_string, char* words[], int* word_length, int word_count);

int main()
{
	//Variable Declaration/Initialization
	const int MAX_LENGTH = 255;
	char name[MAX_LENGTH] = {'\0'};
	char *string;
	const int MAX_ELE = 50;
	char ptr[MAX_ELE]; 
	char* words;
	int wl[MAX_ELE];
	int* word_length;
	int wc = 0;

	//Make string and words point to set array
	string = name;
	words = ptr; 
	word_length = wl;

	//Function call

	//1. Prompting user for sentence
	getString(name, MAX_LENGTH);

	//2. Counting words in sentence and letters in words
	getWords(name, words, word_length, wc);

	//3. Displaying info
	displayInfo(name, words, word_length, wc);

	return 0;
}

void getString(char* string, int max_length)
{
	cout << "Original string: ";
	cin.getline(string, max_length);
}

void getWords(char* orig_string, char* words[], int* word_length, int& wc)
{
	//Allocating adress of first letter of each word
	int i = 0;
	int wl = 0;
	int count = 0;
	//Trying to assign the adress of the first letter to the first element of pointer words array
	words[i] = orig_string[i];
	for( i = 0; orig_string[i] != '\0'; i++ )
	{
		if( orig_string[i] == ' ' )
		{
			count++;
			words[i] = orig_string[i];
		}
	}
	
	
	//Word length
	int j = 0;
	while (j < i)
	{
		//Count letters as long as null terminator is not reached
		while (words[j] != '\0')
		{
			wl++;
		}
		//Increase counter for each word
		j++;
	}
	
	word_length[j] = wl;

	//Word count
	wc = i+1;
}

void displayInfo(char* orig_string, char* words[], int* word_length, int word_count)
{
	cout << "Number of words: " << word_count << endl;
	for (int i = 0; i < word_count ; i++)
	{
		cout << left << setw(20) << "Word 1: " << *words[i];
		cout << left << setw(20) << "Length of Word " << i+1 << ": " << word_length[i];
	}
}




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.