this code is meant to input words from file, and output to file the amount of letters in them and how many words there are

#include<iostream>
#include<iomanip>
#include<fstream>
#include<cmath>
#include<conio.h>
#define in_file "data.txt"//input file name
#define out_file "result.txt"//output file name
using namespace std;
void inputwords(int&, int&);
void outputwords(int, int);
ifstream ins;// associates ins as an input stream
ofstream outs;// associates outs as an output stream
int letters=0;
int words=0;
char ch;

int main()
{
	ins.open(in_file);// associates ins as an input stream
	outs.open(out_file);// associates outs as an output stream

	bool charcheck;
	inputwords(letters, words);
	outputwords(letters, words);

	ins.close();// closing input file
	outs.close();// closing output file

	return 0;

}

bool charcheck (char c)
{
	if ((c >='a' && c <= 'z') || ((c >= 'A') && (c < 'Z')))
		return 1;
	else
		return 0;
}


void inputwords(int& letters, int& words)
{
	while(!ins.eof())
	{
		ins.get(ch);
		if (charcheck(ch))
		{
			letters++;
		}

		if((ch==' ')||(ch=='.')||(ch=='\n'))
		{
			words++;
		}

		ins.get(ch);
	}
}

void outputwords(int letters, int words)
{
	outs<<ch<<" has "<<letters<<endl;
	outs<<"There are "<<words<<" words."<<endl;
}

that my code, im having trouble with the counting letters and words part, any suggestions??
what i've done is that everytime it has a letter its counted with a counter, same for words
also im not even getting an output, but thats probably sometihng minor with the last function?

Recommended Answers

All 7 Replies

that my code, im having trouble with the counting letters and words part, any suggestions??

Sure. Explain why you think it's not counting words and letters.

also im not even getting an output, but thats probably sometihng minor with the last function?

You're calling the output function. What makes you think it's not giving any output?


When asking for help, it's important to also give the details explaining what is (or isn't) happening. Not just "Broke, why?"

well i tihnk its not counting letters or words because the bool function isnt working, but that may go back to inputting the words, they may have been inputted wrong so they arent going into the bool statment properly

i thinnk after that is solved it should output properly

ok well after making some changes it now outputs, but the word and letter counting still doesnt

if i put
word
word
word
into the data file the program reads it in as
w

r

w

r

w

r

d

ok will i made some changes and i got it working

void inputwords(int& letters, int& words)
{
	while(!ins.eof())
	{
		ins.get(ch);
		outs<<ch;
		if (charcheck(ch))
		{
			letters++;
		}

		if((ch==' ')||(ch=='.')||(ch=='\n'))
		{
			outputwords(letters);
			words++;
			ins.get(ch);
			letters=0;
		}
	}
	outs<<endl;
	outs<<"There are "<<words<<" words."<<endl;
}

for the input i have to put a space after each word but it makes sense, so ya it works now

How many ins.get(ch); statements do you have in your loop? Why?

i have them so that it loops, are u saying i dont need that many??? cause it didnt have a problem so im not sure what ur saying

THINK about it. What does ins.get(ch); do? What do you do with each value they get?

Follow your code with pencil and paper and figure out what it's really doing. That way you won't need to waste 6 hours hoping we'll fix it for you. In 10 minutes you'll get your answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.