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?
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
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.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
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 ifletter 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!
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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;
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
>>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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
// ....
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" ;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287