Hello and good evening
I'm trying to make a a program that will take text from a input file to a variable in main then use that variable with nested while loops to count all the spaces in the text then display it using cout.
I been at this for hours and I can't figure this out if anyone can please help me I would really appreciate it! Thanks in advance.

#include<iostream>
#include<fstream>


using namespace std;


void processBlanks(int& wrd,char& cp);

int main()
{
ifstream inFile;
ofstream outFile;

int linecount,numpar;
int wrd=0;
char ch;


inFile.open("C:\\Documents and Settings\\Owner\\Desktop\\test1.txt");
outFile.open("C:\\Documents and Settings\\Owner\\Desktop\\output.txt");

if (!inFile)
{
	cout<< "can't open File"<<endl;
	return 1;
}

inFile.get(ch);


while(inFile)
{

while(inFile&&ch !='\n')
{

processBlanks( wrd, ch);


}
inFile.get(ch);

}

cout<< wrd<<endl;

}



void processBlanks(ifstream& infile, int& wrd,char& cp)
{
	

if(cp==' ')

		
wrd++;


	return ;
}

basically I use to the get comand to retrive a char then I want to use this char in my function to process the char but I think I messed up in the while loops

Hi Iznar

I did some corrections on your program hopefully you will learn something of these. The lines I modified, added or deleted are marked by // and sometimes also explained.

Because of your true problems with pointers you should particularly focus on the usage of & and * in the following code.

#include <fstream>
#include <iostream>
#include <cmath>
using namespace std;

void processBlanks(int*wrd, char cp);  // & dropped and * added

int main()
{
ifstream inFile;
////ofstream outFile; not necessary

////int linecount, numpar;  not necessary
int wrd=0; 
char ch;

inFile.open("C:\\Documents and Settings\\Owner\\Desktop\\test1.txt");

////outFile.open("C:\\Documents and Settings\\Owner\\Desktop\\output.txt");  not necessary

/////if (!inFile) wrong, replaced by:
if (! inFile.is_open())
{
   cout<< "can't open File"<<endl;
   return 1;
}

/////inFile.get(ch); wrongly placed, must be part of while loop

/////while(inFile) wrong, replaced by:
while (inFile.get(ch) && !inFile.eof())
{
   /////while(inFile&&ch !='\n') wrong,  deleted
   /////{ deleted

   processBlanks(&wrd, ch);  ///// actually not necessary, yet consider the added &

    // if you arent't require to use a method/function, you should drop it 
    // and then uncomment the following line:
    // if (ch==' ') wrd++ ;   // that's all

//////} deleted
//////inFile.get(ch); overmuch, deleted

}
cout <<"Number of spaces " << wrd <<endl; 
inFile.close();     //// added 
return 0;       //// added
}

///// void processBlanks(ifstream& infile, int& wrd,char& cp) replaced by:
void processBlanks(int* wrd, char cp) //  <--- consider the * and there is NO &
{

if(cp==' ') (*wrd)++;  // <--- carefully consider the added () and *

//////return ; deleted
}

I only corrected your program in such a manner that mistakes were stamped out so it finally produced a correct result. Actually, above program could be further improved.

-- tesu

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.