I've taken out lines 35-42 in palindromesdefs.c
that's the while statement
Trendkiller 0 Newbie Poster
thanx for the tip jephthah
Trendkiller 0 Newbie Poster
Ok. I'm way closer than I was but still need some help. I'll place my code below for the 2 .c files and the .h file. I'm needing to get it to take out the punctuation and write the palindromes to an output.txt file.
main.c
#include <stdio.h>
#include "PalindromeDefs.h"
int main(void)
{
readFile();
return 0;
}
PalindromeDefs.c
#include <stdio.h>
#include "PalindromeDefs.h"
#define SIZE 100
void readFile()
{
Stack A;
//CreateStack(*A);
int wordCount=0;
int palindromeCount=0;
int i = 0;
int counter = 0;
char *token;
char txt[SIZE];
char txtname[SIZE];
char checkString[SIZE];
FILE *rfPtr;
puts("Enter a directory path and we will check it for palindromes.\n");
scanf("%s[^\n]",txtname);
rfPtr=fopen(txtname,"r");
if (rfPtr==NULL)
{
printf("File cannot be opened!\n");
}
else
{
while(fgets(txt,200,rfPtr)!=NULL)
{
token=strtok(txt," .\n");
while(token!=NULL)
{
fprintf(rfPtr,"%s\n",token);
token=strtok(NULL," .\n");
sscanf(token, "%s", txt[i]);
i++;
}
checkString=strupr(strrev(txt));
txt=getStringFromStack(A,counter);
if(isPalindrome(txt,strrev(txt))==1)
{
writeFile(txt);
palindromeCount++;
}
wordCount++;
}
}
printf("\n\nThe number of words is %d\nThe number of palindromes is %d\n\n",wordCount,palindromeCount);
fclose(rfPtr);
return;
}
/*char getStringFromStack(Stack C, int counter)
{
char temporary[counter];
int place = 0;
while(counter!=-1)
{
temporary[place]=Pop(&value,&A);
}
return temporary;
}*/
void writeFile(char txt[])
{
FILE *cfPtr;
cfPtr=fopen("c:\\output.txt","a");
if(cfPtr==NULL)
{
printf("File cannot be opened!\n");
}
else
{
fprintf(cfPtr,"%s ",txt);
}
fclose(cfPtr);
return;
}
/* CreateStack: will initalize the stack to empty */
/* preconditions: Stack variable declared */
/* postconditions: Stack will be initialized and empty */
void CreateStack(Stack *s)
{
s->top=0;//' ' maybe
return;
}
/* Push: puts an item onto the stack */
/* pre: Stack exists and is not full */
/* post: …
Trendkiller 0 Newbie Poster
Ok. I'm way closer than I was but still need some help. I'll place my code below for the 2 .c files and the .h file. I'm needing to get it to take out the punctuation and write the palindromes to an output.txt file.
main.c
#include <stdio.h>
#include "PalindromeDefs.h"
int main(void)
{
readFile();
return 0;
}
PalindromeDefs.c
#include <stdio.h>
#include "PalindromeDefs.h"
#define SIZE 100
void readFile()
{
Stack A;
//CreateStack(*A);
int wordCount=0;
int palindromeCount=0;
int i = 0;
int counter = 0;
char *token;
char txt[SIZE];
char txtname[SIZE];
char checkString[SIZE];
FILE *rfPtr;
puts("Enter a directory path and we will check it for palindromes.\n");
scanf("%s[^\n]",txtname);
rfPtr=fopen(txtname,"r");
if (rfPtr==NULL)
{
printf("File cannot be opened!\n");
}
else
{
while(fgets(txt,200,rfPtr)!=NULL)
{
token=strtok(txt," .\n");
while(token!=NULL)
{
fprintf(rfPtr,"%s\n",token);
token=strtok(NULL," .\n");
sscanf(token, "%s", txt[i]);
i++;
}
checkString=strupr(strrev(txt));
txt=getStringFromStack(A,counter);
if(isPalindrome(txt,strrev(txt))==1)
{
writeFile(txt);
palindromeCount++;
}
wordCount++;
}
}
printf("\n\nThe number of words is %d\nThe number of palindromes is %d\n\n",wordCount,palindromeCount);
fclose(rfPtr);
return;
}
/*char getStringFromStack(Stack C, int counter)
{
char temporary[counter];
int place = 0;
while(counter!=-1)
{
temporary[place]=Pop(&value,&A);
}
return temporary;
}*/
void writeFile(char txt[])
{
FILE *cfPtr;
cfPtr=fopen("c:\\output.txt","a");
if(cfPtr==NULL)
{
printf("File cannot be opened!\n");
}
else
{
fprintf(cfPtr,"%s ",txt);
}
fclose(cfPtr);
return;
}
/* CreateStack: will initalize the stack to empty */
/* preconditions: Stack variable declared */
/* postconditions: Stack will be initialized and empty */
void CreateStack(Stack *s)
{
s->top=0;//' ' maybe
return;
}
/* Push: puts an item onto the stack */
/* pre: Stack exists and is not full */
/* post: …
Trendkiller 0 Newbie Poster
cfPtr=fopen("d:\\output.txt","w");
What I found is that you should append like this if you want it to write to the output.txt file.
cfPtr=fopen("d:\\output.txt","a");
Trendkiller 0 Newbie Poster
Thanks for the help guys. I'm about to start on this again and probably will make another post. I just got off work and will be at this until about 2 am central time. This is one of the best sites and I thank everyone for their information on helping me learn.
Trendkiller 0 Newbie Poster
This is now what I have. The word count is correct, however I can't get the palindromes to count. The output says--" " is a palindrome--
The help given already has helped by leaps and bounds.
#include<stdio.h>
#include "Palindrome.h"
#define SIZE 300
int main(void)
{
char txt[SIZE];
char *s;
int wordCount=0;//word count
int palCount=0; //palindrome count
char strsrc[SIZE];
char strtmp[SIZE];
FILE *rfPtr;
/* Open file */
printf("Please enter a file to read for palindromes.\n");
scanf("%s",txt);
gets(strsrc);
s = strtok (txt, ", ! .");
rfPtr=fopen(txt,"r");
if (rfPtr==NULL)
{
printf("File cannot be opened!\n");
}
else
{
/* While reading next word does not give EOF */
while(!feof(rfPtr))
{
fscanf(rfPtr,"%s",txt);
printf("%s ",txt);
wordCount++; /* Increment count of words */
}
strcpy(strtmp,strsrc);
strrev(strtmp);
if(strcmp(strsrc,strtmp)==0)
{
printf("\n Entered string \"%s\" is a palindrome",strsrc);
palCount++;
}
else
{
printf("\n Entered string \"%s\" is not a palindrome",strsrc);
getch();
}
/* If the word is a palindrome */
/* Increment count of palindromes */
}
fclose(rfPtr);
printf("\n\nThe word count is: %d\n",wordCount);
printf("The palindrome count is: %d\n\n",palCount);
/* Ouput to palindrome file */
/* Show results */
/* Close files */
return 0;
}
Trendkiller 0 Newbie Poster
also if you try to run it it doesn't read from the file it reads the enter key from the keyboard only
Trendkiller 0 Newbie Poster
I've got some of my project finished. This is supposed to 1) check to see if each word in the input file is a palindrome using stacks
2) If the word is a palindrome, write the word to a separate output filr.
3) Count the number of words read and the number of words which are found to be palindromes.
Here is my main.c file
#include<stdio.h>
#include "Palindrome.h"
#define SIZE 300
int main(void)
{
char txt[500];
readFile(txt);
writeFile(txt);
checkPalindrome();
return 0;
}
This is my function definition.c file. Now should I use stacks to check for palindromes in the current functions? or should I make a whole new function for that?
#include<stdio.h>
#include "Palindrome.h"
#define SIZE 300
void readFile(char txt[])
{
char txtname[100];
FILE *rfPtr;
printf("Please enter a file to read for palindromes.\n");
scanf("%s",txtname);
rfPtr=fopen(txtname,"r");
if (rfPtr==NULL)
{
printf("File cannot be opened!\n");
}
else
{
while(!feof(rfPtr))
{
fscanf(rfPtr,"%s",txt);
printf("%s ",txt);
}
}
fclose(rfPtr);
return;
}
void writeFile(char txt[])
{
FILE *cfPtr;
cfPtr=fopen("J:\\Palindrome\\Debug\\output.txt","w");
if(cfPtr==NULL)
{
printf("File cannot be opened!\n");
}
else
{
fprintf(cfPtr,"%s ",txt);
}
fclose(cfPtr);
return;
}
and here is my palindrome.h file
#include <stdio.h>
void readFile(char txt[]);
void writeFile(char txt[]);
//void checkPalindrome();
Trendkiller 0 Newbie Poster
I figured out my own thread. There are some &'s which don't need to be in here on the function prototype line and also the function definition line.
Trendkiller 0 Newbie Poster
I am getting some errors. 4 are C2143 2 of which point to the function prototype void ArrayFunc(int, int, float, int[SIZE][2],float[SIZE],int &,int &); and the other 2 C2143 errors point to void ArrayFunc (int count, int hours, float total, int array1[][2], float array2[], int &row, int &column) (the function definition). Also there are 4 C2059 errors that direct me to the same lines as the C2143 errors. Please help me with this problem, someone.
#include <stdio.h>
#define SIZE 10
void PrintHeading (void);
void PrintValues (int, int, float);
float CalcVal (int);
void ArrayFunc (int, int, float, int[SIZE][2],float[SIZE],int &,int &);
char ReadChoice(void);
int ReadHours(int hours);
void PrintArray(int [SIZE][2], float[SIZE]);
int main (void)
{
int count=0; //counter
int hours=0; //hours
char choice; //users choice for continuing
float total=0; //total charge
int array1[SIZE][2]={0}; //holding customer number and hours
float array2[SIZE]={0}; //holding customer total
int row = 0, column = 0; //
do
{
//assinging customer number
count++;
hours=ReadHours(hours);
total=CalcVal(hours);
ArrayFunc(count,hours,total,array1, array2,row,column);
PrintHeading();
PrintValues(count, hours, total);
choice = ReadChoice();
} while (choice=='y' || choice=='Y');
PrintArray(array1,array2);
return 0;
}
void PrintHeading(void)
{
//print display
printf ("%-15s %-10s %-10s","Customer","Hours","Total Charge\n");
}
void PrintValues(int count, int hours, float total)
{
//print display
printf ("%-15d %-10d %5c %-10.2f\n", count, hours, '$',total);
}
float CalcVal(int hours)
{
//begin loop for repetition
float total=0;
if (hours<3 && hours>0)
total=2;
else
if (hours>=19 && hours<=24)
total = 10;
else
if (hours > 3 && hours < 19)
total = (hours-3)*.5 + 2;
else
if (hours > 24)
printf ("Invalid Entry...Try Again"); …
Trendkiller 0 Newbie Poster
I can't find the error and also C2182 error: illegal else without matching if.
#include <stdio.h>
int main (void)
{
int count=0; //counter
int hours; //hours
char choice; //users choice for continuing
float total=0; //total charge
//print display
printf ("%-15s %-10s %-10s","Customer","Hours","Total Charge");
//assinging customer number
count++;
//ask for # of hours
printf("Please enter # of hours:\n");
scanf ("%d",&hours);
//calculating total parking charge
if (hours<3) && (hours>0)
total=2;
else
if (hours>=10) && (hours<=24)
total = 10;
else
if (hours > 3) && (hours < 19)
total = (hours-3)*.5 + 2;
else
if (hours > 24)
printf ("Invalid Entry...Try Again");
//need to repeat if entry is incorrect
//print display
printf ("%10d %20d %15fl", count, hours, total);
return 0;
}