944,123 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1379
  • C RSS
Mar 30th, 2006
0

problems with function pointers

Expand Post »
I am doing my program here which is a simple input characters classify them and display them, it has other functions as well. My problem is that i had to modify my program starting to use pointers. In the menu number 7 it asks for a charcter you want to replace and then it replaces it in the string if it is avaialable, now here is my problem after replacing characters in the string with new ones i have to classify the string again to make sure it is updated. In my case 7 i use the functions Modify() and count() to replace characters, i have a classify () function but it does not go well, basically you can't use it again or call it. Here is the reason why in my clasiffy function i ask the user to input characters the string can not be bigger than 60, i have to monitor every character along the way, if a blank character or return is hit its gotta end the process. I want to make my classify function to be general with out the process of asking for characters and monitor, i want to modify it so you can call it to just update the summary. How do i go about this and still monitor the characters being inputed, do i make another funcition or do i just replicate the classify code in my modify code and update it that way, that seems so repititive and not good programming. Need help tackling this problem, my code is blow, all of it.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <iostream.h>


void instruct(void);
void classify(char cInput[], float fstats[], int i, int iOdd, int iEven, int iUpper,
int iLower, int iOaverage, int iEaverage);
void summary(float fstats[], int i, int iOdd, int iEven, int iUpper,
int iLower, int iOaverage, int iEaverage);
int count(char cChoice, char cInput[]);
int search(char cGet, char cInput[]);
void menu(void);
int modify ( const char x, const char y, char *s);


void main(void)
{

int iOdd=1, iEven=2;
int i=0, iCheck=0, j, iChange = 0;
int iUpper=3, iLower=4;
int iOaverage =5, iEaverage=6;
char cInput [61] = {"0"};
float fstats[7]={0,0,0,0,0,0,0};
char cPick , cChoice, cGet, cReplace;

cInput[i]='\0';
menu();
cPick = getche();

for (;cPick != '9'; )
{
switch(cPick)
{
case '1': system("cls");
cInput[i]='\0';
instruct();
for (j=0; j<=6; j++)
fstats[j]=0;
classify(cInput,fstats,i,iOdd,iEven,iUpper,iLower,iOaverage,iEaverage);
break;

case '2': iCheck = strlen(cInput);
if (iCheck >=1)
{
system("cls");
for (j=0; j<iCheck;j++)
printf("%c", cInput[j]);
printf("\n");
}
else
printf("\nThere is no string in the memory please input a string first\n");
//cin.clear();
//cin.ignore(80,'\n');
//cin.get();
system("pause");
break;

case '5': iCheck = strlen(cInput);
if (iCheck >=1)
{
system("cls");
printf("Please input the character to search:");
cGet = getche();
if (search(cGet, cInput) > 0)
printf("\nthe character occurs first at the index %d\n",search(cGet,cInput));
else
printf("\nthere are no occurences of that character\n");
}
else
printf("\nThere is no string in the memory please input a string first\n");

//cin.get();
system("pause");
break;

case '6': iCheck = strlen(cInput);
if (iCheck >=1)
{
system("cls");
printf("please input character:");
cChoice = getche();
if (count(cChoice,cInput) >0)
printf("\nThere are %d occurences of the character %c\n", count(cChoice, cInput), cChoice);
else
printf("\nthere are no occurences of that character\n");
}
else
printf("\nThere is no string in the memory please input a string first\n");
//cin.get();
system("pause");
break;

case '8': iCheck = strlen(cInput);
if (iCheck >=1)
{
system("cls");
summary(fstats,i,iOdd,iEven,iUpper,iLower,iOaverage,iEaverage);
}
else
printf("\nThere is no string in the memory please input a string first\n");
//cin.get();
system("pause");
break;

case '7': iCheck = strlen(cInput);
if (iCheck >=1)
{
system("cls");
printf("character to be replaced:");
cChoice = getche();
if (count(cChoice,cInput) >0)
{
printf("\nWith:");
cReplace = getche();
iChange = modify(cChoice,cReplace,cInput);
printf("\nThere have been %d changes made to the string\n", iChange);
}
else
printf("\nthere are no occurences of that character\n");
}
else
printf("\nThere is no string in the memory please input a string first\n");
system("pause");
break;
}
system("cls");
menu();
cPick = getche();
}
printf("\n\n");

}

void instruct(void)
{
printf("To terminate the program, strike the spacebar or Enter.\n\n\n");
printf("Next key>");
}

void classify(char cInput[], float fstats[], int i, int iOdd, int iEven, int iUpper,
int iLower, int iOaverage, int iEaverage)
{
int j =0, iResult =0;
char cTemp;

cTemp = getche();

for(;((cTemp != 32) && (cTemp!= 13)); ) //ascii 32 for blank and 13 for carriage return
{
if ( j<=59)
{
if (isalpha(cTemp) != 0)
{
if (isupper(cTemp) !=0)
fstats[iUpper]++;
else
fstats[iLower]++;
}

if(isdigit(cTemp) !=0)
{
if( (cTemp%2) == 0)
{
fstats[iEven]++;
iResult = cTemp - '0';
fstats[iEaverage] = (fstats[iEaverage] + iResult);
}

else
{
fstats[iOdd]++;
iResult = cTemp - '0';
fstats[iOaverage] = (fstats[iOaverage] + iResult);
}
}

if ((cTemp != 8) && (cTemp != 9))
{
cInput[j]= cTemp;
cInput[j+1] = '\0';
fstats[i]++;
j++;
}
cTemp = getche();
}
else
{
printf("\nYou cannot enter more than 60 characters, the first 60 characters will remain\n");
//printf("hit enter to continue");
//cin.get();
system("pause");
break;
}
}// for loop

if (fstats[iEven] ==0)
fstats[iEaverage] =0;
else
fstats[iEaverage]= fstats[iEaverage]/fstats[iEven];

if (fstats[iOdd] ==0)
fstats[iOaverage] =0;
else
fstats[iOaverage] = fstats[iOaverage]/fstats[iOdd];
}

void summary(float fstats[], int i, int iOdd, int iEven, int iUpper,
int iLower, int iOaverage, int iEaverage)
{
system("cls");

printf("Thank you! Your summary:\n\n");
printf("Total number of keystrokes entered:%.f\n", fstats[i]);
printf("Upper case alphabetic characters:%.f\n", fstats[iUpper]);
printf("Lower case alphabetic characters:%.f\n", fstats[iLower]);
printf("Even digits:%.f\n", fstats[iEven]);
printf("Odd digits:%.f\n", fstats[iOdd]);
printf("Average value of the even digits entered:%.2f\n", fstats[iEaverage]);
printf("Average value of the odd digits entered:%.2f\n", fstats[iOaverage]);

}

int count(char cChoice, char *cInput)
{
int i, iNum, iTotal =0;

iNum = strlen(cInput);
for ( i=0; i<iNum; i++, cInput = cInput + 1)
{
if (cChoice == *cInput)
iTotal++;
}

return iTotal;
}


int search(char cGet, char *cInput)
{
int i, iIndex=0, iNum=0;

iNum = strlen(cInput);
for (i=0; i < iNum; i++)
{
if (cGet == cInput[i])
{
iIndex = i;
iIndex++;
break;
}
}

return iIndex;
}

void menu(void)
{
printf("1. Enter a string of characters\n");
printf("2. Display the current string\n");
printf("3. Write current string to file\n");
printf("4. Read a string from a file\n");
printf("5. Search for a character\n");
printf("6. Count occurences of a specific character\n");
printf("7. Replace all occurences\n");
printf("8. Produce a summary report on the current string\n");
printf("9. Exit\n\n");

}

int modify ( const char x, const char y, char *s)
{
int iNum, j, i=0;

iNum = strlen(s);
for ( j=0; j < iNum; j++, s= s+1)
{
if (x == *s)
{
*s = y;
i++;
}
}

return i;
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chelo77 is offline Offline
12 posts
since Mar 2006
Mar 31st, 2006
0

Re: problems with function pointers

Hasn't anybody figured out the magic of code tags in this place?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: I developd a COM to custom IE printing
Next Thread in C Forum Timeline: edit boxes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC