Solved

check this code

Posted on 1998-08-05
15
212 Views
Last Modified: 2010-04-02
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
Comment
Question by:learn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +3
15 Comments
 
LVL 2

Expert Comment

by:kellyjj
ID: 1169499
You could use a goto statement, but other than that I don't see any other way.  
0
 

Author Comment

by:learn
ID: 1169500
I think even "break" is better than "goto".
0
 
LVL 84

Expert Comment

by:ozo
ID: 1169501
c1 seems to be uninitialized the first time through the loop.

0
Industry Leaders: 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!

 

Expert Comment

by:jeseem
ID: 1169502
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
 
LVL 11

Expert Comment

by:alexo
ID: 1169503
   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
 
LVL 22

Expert Comment

by:nietod
ID: 1169504
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
 

Author Comment

by:learn
ID: 1169505
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
 
LVL 22

Expert Comment

by:nietod
ID: 1169506
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
 
LVL 11

Expert Comment

by:alexo
ID: 1169507
>> 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
 

Author Comment

by:learn
ID: 1169508
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
 

Author Comment

by:learn
ID: 1169509
To Alexo:

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

Cheers.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169510
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
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 1169511
Feel free to e-mail me when you make the changes to the string class.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1169512
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
 

Author Comment

by:learn
ID: 1169513
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question