The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How do i change to super user then revert back to ordinary user ,using shell script? wrapster Shell Programming and Scripting 3 06-04-2008 04:11 AM
switching user from root to ordinary user sasia Shell Programming and Scripting 3 01-25-2008 07:25 PM
Other than root user .Normal user is unable to create files mallesh UNIX for Advanced & Expert Users 1 06-22-2005 09:18 AM
Limiting length of user in while creating user Satya Mishra AIX 2 04-14-2005 11:40 PM
single user mode - user accounts passwords orestis UNIX for Dummies Questions & Answers 2 03-09-2005 06:54 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 10-28-2007
Registered User
 

Join Date: Oct 2007
Posts: 2
new c++ user that needs help

I'm in a programming class and I have an assignment where I have to open a file, and count the number of times a certain character apperas that the user input, in that file. I need help in figuring out how to create a function to count the number of occurence of a give characeter, and with any mistakes that I may have made. So far this is what I have



#include "stdafx.h"
#include <iostream>
using namespace std;

// function to find the number of occurences of a character
int count_file(ifstream& in_file, int num);
int count_file(in_stream, out_stream);
int main()
{
int num, total;

//open file input
in_stream.open("infile.txt");
if (in_stream.fail())
{
cout << "opening input file failed" << endl
<< "program terminated" << endl;
exit(1);
}

cout << "Enter a charcter that you would like to know whether or not it appears";
cin >> num;


// function to find the number of occurences of a character
count_file()

//open file for output
out_stream.open("outfile.txt");

count_file(in_stream, out_stream);

// closing files
in_stream.close();
out_stream.close();

return 0;
}

int count_file(ifstream& in_file, int num)
{
while(! in_file.eof())
{
fin.get(num)
total =
}
}

// function to output the answer
int count_file(in_stream, out_stream)
{
cout << "The charcetr" << num << "occured" << total << "times";
}

Last edited by cold16; 10-28-2007 at 01:11 PM.
Reply With Quote
Forum Sponsor
  #2  
Old 10-28-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Simple rules of the UNIX.COM forums:

Rule 6.

surely you would do the equivalent of

Code:
int count_file(FILE *fpIn,char whatToLookFor)
{
         int result=0;
         int c;

         while (EOF!=(c=fgetc(fpInf)))
         {
               if (c==whatToLookFor) result++;
         }

         return result;
}
But you're in a C++ class so you do it in C++.

I think you mean "whether", not "weather".
Reply With Quote
  #3  
Old 10-29-2007
Registered User
 

Join Date: Oct 2007
Posts: 2
thankyou for the help
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0