• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

check this code

Hi experts

Can you improve following code for me. At least it should not do double check int(c1)==10. Don't tell me using "break".

   ifstream fin(filename);
   char c1;
   while (int(c1) != 10)
   {  c1 = fin.get();
      if (int(c1) == 10)
       ..........do something;
      else
       ..........do something else;
   }
0
learn
Asked:
learn
  • 5
  • 4
  • 2
  • +3
1 Solution
 
kellyjjCommented:
You could use a goto statement, but other than that I don't see any other way.  
0
 
learnAuthor Commented:
I think even "break" is better than "goto".
0
 
ozoCommented:
c1 seems to be uninitialized the first time through the loop.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jeseemCommented:
as the Qs is locked giving as comment
I suppose ur problem is that u shouldnot do c1=10 condition testing twice
so
use try catch logic

        ifstream fin("temp.txt");
                char c1;
      
         try
         {
               while (true)
               {  c1=fin.get();
                    if (int(c1) == 10)                                 
                          throw(anything);
                    else
                          //..........do something else for c1!=10;
               }
            }catch(that anything){//..........do something; for c1==10}
0
 
alexoCommented:
   bool flag = true;
    while (flag)
    {
        c1 = fin.get();
        if (int(c1) == 10)
        {
            // do something...
            flag = false;
        }
        else
            // do something else...
    }

If you like this solution better than the previous ones, reopen the question for it.

Never use exceptions for flow control!
0
 
nietodCommented:
Speaking as an assembly languange programer, the most efficient way is with the "break".  I find that "break" is good to use in code and does not make it hard to understand.  "Goto" is efficient too, but does potentially make it hard to understand.

Using a throw and catch is a bad idea.  That is very inefficient.  (several orders of magnitude slowe, you could do the test a 100 times and sill be faster.)

Alexo's answer appears to be what you want, but isn't.  If the test to be performed is complex and slow or cannot be repeated that is a good way (the best way) to do it.  However, in this case, where the text to be performed is fast, there is no advantage.  It takes just as long to test the bool for true as it does to test the character for 10.   Thus he introduced a new test with no advantage over the old test.

I would use a break or switch to a do loop (that tests at the bottom.).
0
 
learnAuthor Commented:
To nietod:

1. A book doesn't like "break", I should ask arther :-)
2. I think it still need doing double check even using do loop.

Cheers.
0
 
nietodCommented:
Yes, you will have to do the check twice with the do loop, the difference is that you don't have to initialize c1.  With a while loop, that is, with the check at the top, you need to initialize c1 with a "dummy" value, in order to get into the while loop.  If you don't c1 could be randomly initalized and could sometimes start out with a value of 10.  In that case, the while loop would never run.  With the do loop, the loop will always be entered so the code will run at least once.  Now this is not a speed efficiency thing.  with the while loop you will have an extra initialization and one extra test.  That is maybe 30 clock cycles..  On a 300Mhz processor that is 1 millionth of a second.  Do it doesn't save enough time to worry about, but it does save space as well and it makes the code a little safer (If someone were to later see that the initial value wasn't needed (but it is) and remove it....)

There is no way to avoid the double check unless you use do the break.  Alex's code apparently avoids it, but not really.  (Alex's mind was on his coming vacation.)  But in this case, the cost fo the double check is trivial.  As klittle as 2 clocks, but at most say 15 clock cycles.  Again a millionth of a second.  Even if you had to do the loop 1000 times it would be trivial.  Now if you had a test that took a long time to do, you could use Alex's approach then.  In that case the slow test would be performed only one time per loop.
0
 
alexoCommented:
>> Alex's mind was on his coming vacation.
Alex's mind was on the "requirement" not to use break.

My "solution" is a kludge.  By all means use break.

>> A book doesn't like "break"
Then the author of the book has a poor understanding of C/C++.  Get another book.
My personal recommendations are:
  For C: "The C programming language, 2nd edition" by Kernighan & Ritchie.
  For C++ (beginner): "C++ Primer" by Lippman.
  For C++ (advanced): "The C++ programming language, 3rd edition" by Stroustrup.
0
 
learnAuthor Commented:
To nietod:

I believe your last comment is the best one I can expected. I would like to pass the points to you.
Can I just reopen my question and you put something as the answer? Or just copy the last comment. In case somebody visit this question and get answer.

Cheers.
0
 
learnAuthor Commented:
To Alexo:

Thank you for your comments and you are very kind to suggest some books.

Cheers.
0
 
nietodCommented:
Let me add two more books, ones that you are probably not ready for, but will be before too long.  they are "Effective C++" and "More Effective C++"  They deal with some of the issues that have come up in the other question.  I don't think you would understand them yet, but start withthe C++ primer and then try them.  I believe they are the 2 most essential books in C++.  

If you want me to answer, you have to reject the current answer.  (Although as Alex pointed out, I broke the no break rule.)  
0
 
nietodCommented:
Feel free to e-mail me when you make the changes to the string class.
0
 
ozoCommented:
break may be the most reasonable way to do it.
Although, depending on how c1 is initialized, it may still be possible to rewrite it, perhaps as something like:

while( (c1 = fin.get()) != 10 ){
    ..........do something else;
}
........do something;

But we'd have to see how c1 was originally initialized to know whether that was equivalent,
or whether we'd have to rewite it some other way.
0
 
learnAuthor Commented:
To ozo and all experts who help me in this question:

Thank you very much. I didn't expected so many suggestions which let me know a lot. especialy, ozo suggested a very good alternative way, though too late :-)

Cheers to you all.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 4
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now