new c++ user that needs help

Thread Tools Search this Thread
Top Forums Programming new c++ user that needs help
# 1  
Old 10-28-2007
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"infile.txt");
if (
cout << "opening input file failed" << endl
<< "program terminated" << endl;

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

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

count_file(in_stream, out_stream);

// closing files

return 0;

int count_file(ifstream& in_file, int num)
while(! in_file.eof())
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 05:11 PM..
# 2  
Old 10-28-2007

Rule 6.

surely you would do the equivalent of

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".
# 3  
Old 10-29-2007
thankyou for the help
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Pam.d and make difference between AD User and local user on Linux

Hello, i configured rhel linux 6 with AD directory to authorize windows users to connect on the system and it works. i have accounts with high privileges (oracle for example) if an account is created on the AD server i would to block him. I looked for how to do, for the moment all the... (3 Replies)
Discussion started by: vincenzo
3 Replies

2. UNIX for Dummies Questions & Answers

Switching from root to normal user takes me to user's home dir

Whenever i switch from root to another user, by doing su - user, it takes me to home directory of user. This is very annoying as i want to be in same dir to run different commands as root sometimes and sometimes as normal user. How to fix this? (1 Reply)
Discussion started by: syncmaster
1 Replies

3. Shell Programming and Scripting

Script interacts with user , based on user input it operates

i have a script which takes input from user, if user gives either Y/y then it should continue, else it should quit by displaying user cancelled. #!/bin/sh echo " Enter your choice to continue y/Y OR n/N to quit " read A if then echo " user requested to continue " ##some commands... (7 Replies)
Discussion started by: only4satish
7 Replies

4. Shell Programming and Scripting

Update LDIF User info based on Test User Certs ID's

Hi I need help.......... I have an Sun One Directory server LDIF file with 5000 user entries, I need to change the data to match Test ID's, so I can run a perf test. I'm way out of my league as I have not done any scripting for 10 years. There are four entries for each user in the file... (3 Replies)
Discussion started by: Macdaddy99
3 Replies

5. UNIX for Advanced & Expert Users

Determining if user is local-user in /etc/passwd or LDAP user

Besides doing some shell-script which loops through /etc/passwd, I was wondering if there was some command that would tell me, like an enhanced version of getent. The Operating system is Solaris 10 (recent-ish revision) using Sun DS for LDAP. (5 Replies)
Discussion started by: ckmehta
5 Replies

6. Shell Programming and Scripting

root user command in shell script execute as normal user

Hi All I have written one shell script for GPRS route add is given below named URL="" VBURL="" echo "Setting route for $URL for GPRS" URL_Address=`nslookup $URL|grep Address:|grep -v "#"|awk -F " " '{print $2}'|head -1` echo "Executing ... (3 Replies)
Discussion started by: mnmonu
3 Replies

7. UNIX for Dummies Questions & Answers

Difference between : Locked User Account & Disabled User Accounts in Linux ?

Thanks AVKlinux (3 Replies)
Discussion started by: avklinux
3 Replies

8. Shell Programming and Scripting

How do i change to super user then revert back to ordinary user ,using shell script?

Hi all, I am trying to eject the cdrom from a livecd after certain stage... Now assuming that it is possible to eject,please consider my issue!!! The OS boots into a regular user by i am unable to use the eject command to push out the drive... However if i try pfexec eject it... (3 Replies)
Discussion started by: wrapster
3 Replies

9. UNIX for Advanced & Expert Users

Other than root user .Normal user is unable to create files

Hi all, I am using Sun Solaris 9 .In this system normal users unable to create files from the command line.I added these users in bin,adm and even root group i found them unable to create a file. (1 Reply)
Discussion started by: mallesh
1 Replies
Login or Register to Ask a Question

Featured Tech Videos