hi, every body..

I have two program I couldn’t solve them
So, can any body help me. please!!

1-Write a program that accepts a character and count number of occurrences in a file.
The file should have some text, and the program count how many times the inputted character repeated in the file and prints the result on the screen.
(Hint: you have to use continue statement in your solution )

Sample input:
Input.txt
I am a student in the university

Sample output:
Enter the character :a
The letter a repeated 2 times(s)


2-Write a program that accepts a character and search it in a file.
The file should have some text, and the program search for the first occurrence of the character and print it’s location on the screen…
(Hint: use break statement to stop the loop)

Sample input:
Input.txt
I am a student in the university

Sample output:
Enter the character to search :a
the first occurrence of a character is at location 3

I am waiting the replies ..

{ thanks all}

Recommended Answers

All 10 Replies

I don't see what the problem is. Why can't you write these programs? There must be some specific information you're lacking. What do you need to figure out how to do in order to make progress towards a solution?

I don't see what the problem is. Why can't you write these programs? There must be some specific information you're lacking. What do you need to figure out how to do in order to make progress towards a solution?

I want to know how to solve these programs..the method..

thanks for your reply..

Can you for example open a text file and print the contents of the text file to the screen?

If you can do that much, then you're prepared for the next step.

If you can't, then we need to clear that bit up for you before progressing with the searching bit of the assignment.

In other words, show us how far you can get on your own.

Can you for example open a text file and print the contents of the text file to the screen?

If you can do that much, then you're prepared for the next step.

If you can't, then we need to clear that bit up for you before progressing with the searching bit of the assignment.

In other words, show us how far you can get on your own.

Can you for example open a text file and print the contents of the text file to the screen?

yes,Ican do that..

show us how far you can get on your own.

The code for first program AND the other program like the first one BUT the algorithm(indentation )is different and we must use the break statement :

#include<iostream>
#include<fstream>
using namespace std;
int main( )
{
int count=0;
char letter;

ifstream infile;
infile .open("Input.txt");
if(! infile)
{
cout<<"cannot open the input file."<<endl;
return 1;
}
infile>>letter;
cout<<"Enter the character:";
while(! infile.eof ( ))
{
............................... 
...............................
.............................
continue;
count++;
}

infile.close( );
return 0;

}

My question is what should be the algorithm(indentation ) in the blank space in two programs????

{thanks all}

lines 16 and 17 need to be reversed because they are in the wrong order. You have to display the question on the screen before you expect someone to enter the answer on the keyboard.

who gave you that code snippet? I sure hope it was not your instructor, because if it was you are in for a lot of problems. Line 18 is not the correct way to write that kind of loop. For the first problem it should be like this:

while( infile >> letter )
{

}

>>(Hint: you have to use continue statement in your solution )
Ridiculous -- no you don't. That problem can be easily solved without using the continue or break statements. All you have to do inside that loop is check if letter is the correct character and increment the counter if it is.

And the second problem is just as stupid as the first. My thoughts: you have an idot for a teacher!

commented: I wish I had an iDot for a teacher, although I have a iPod? +9

It's probably just a contrived way of getting the students to use the keyword, nothing more.

if ( condition ) { doStuff; }

Slightly obfuscated as

if ( !condition ) continue;
doStuff;

Thanks all for your replies..

hi..

are this correct soultion:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
	int count=0;
	char letter;
	string sentense;

	ifstream infile;
	infile.open("Input.txt");
	if (! infile)
	{
		cout<<"cannot open the input file."<<endl;
		return 1;
	}
	
	cout<<"Enter the character:";
	cin>>letter;
	while(! infile.eof())
	{

	infile>>sentense;
		continue;
		count+=letter;
		cout<<"The letter a repeated"<<count<<"times(s)"<<endl;
	}

	infile.close();
	return 0;
}

>>are this correct soultion
No. lines 21-28 are incorrect because lines 26 and 27 will never get executed and the while statement at line 21 is wrong.

// ....
while(! infile.eof())
    {

    infile>>sentense;
        continue;
        count+=letter;
        cout<<"The letter a repeated"<<count<<"times(s)"<<endl;
    }
// ...

the continue does nothing (other than perhaps satisfy your teacher) and the statements following it in the loop would be silently ignored by the compiler during code generation. you should change it to
if( ! (infile>>sentense) ) break ; // or continue as you are checking in the while again!
count += letter is incorrect; should be ++count.
you have also omitted to check if the char you read == the letter you are looking for; read a char instead of a string and make a check: is it what you are looking for

you do not need two differerent programs; finding the first occurance and counting the number of occurrances can be done in one pass.

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

int main()
{
  const char* const file_name = "input.txt" ;
  ifstream infile( file_name );
  if( !infile )
  {
    cerr << "cannot open the input file." << endl;
    return 1;
  }

  infile >> noskipws ;
  char letter, ch ;
  cout << "Enter the character:"; cin >> letter;
  int first = -1, nfound = 0 ;

  for( int pos = 0 ; infile >> ch ; ++pos )
    if( ch == letter )
    {
      ++nfound ;
      if( first < 0 ) first = pos ;
    }

  if( nfound>0 )
    cout << "first at: " << first << " total: " << nfound << '\n' ;
  else
    cout << "none found\n" ;
}
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.