Solved

if statement problem

Posted on 2000-04-30
6
159 Views
Last Modified: 2010-04-02
this is my situation
i have 4 subprograms in my main : a, b, c , d

a= displays introduction screen
b=prompts for data
c=calculate
d=output + prompt if want to continue program if so, go back to subprogram b.

my problem is that when i do and if..statement

if (reply == "yes")
b();
it would go to subprogram b and quit.  this is because of it did not reset the marker in the main program.  what do i have to do so that it would go back to b, and continue through c, and d, untill the user decides to quit?
0
Comment
Question by:sarniscool
  • 3
  • 2
6 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2764382
You need a loop of some sort, like

int main ()
{
   bool done = false; // Indicate we are not done.

   a(); // you might want this in the loop so the user can
       // see the instrutions. again and again.
   while (!done)
   {
        b();
        c();
        done = d(); // D needs to return a value or set "done"
              // do that the loop eventuially ends.
   }
   return 0;
}

Hopefully that is enough to help you.  If not, post your code, or at least some of it (especially the D() stuff) and I'll provide more details.
0
 

Author Comment

by:sarniscool
ID: 2765030
The answer you give me is good, but is there another way to do it?  I want a way that won't clog up my main?  I want to keep the main clean,  I want the main to only call functions and declare variables.
0
 

Expert Comment

by:tgemini
ID: 2765375
You could do something like modify nietods code to look like

int main ()
{

    a();
    do
    {
        b();
        c();
    } while(!d());
    return 0;
}

Neither of those are exactly what I would call clogged up or messy mains, in fact in main is where most people would expect to find this kind of highest level looping etc. The only other way I can think of is to go something like:

int main()
{
    do_loop();
    return 0;
}

void do_loop()
{
    a();
    do
    {
        b();
        c();
    } while(!d());
    return;
}

However most people would say that is a fairly good example of excessive use of procedures.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 22

Expert Comment

by:nietod
ID: 2765506
>> The answer you give me is good, but
>> is there another way to do it?  
If you only want clarification or additional information, there is no need to reject an answer.  You should reject an answer only if you know it to be wrong.

I woudl agree very strongly with what, tgemini says.  The you can always move the loop--or any other code out of main and amke a call to the code.  But as he also says, thee is little advantage to this.  

Most texbooks suggest keeping main minimal, because students have a tendancy to place too much compexity in main.  They place a main loop there, then inside the loop they have a swtich statement, then inside each case of the switch statement they have if statements or more loops etc etc etc.  This is definitely bad, but has nothing to do with main() in particular.   Any function with that many levels of nested statements will be hard to read or write and prone to errors.   So any function with that sort of complexity should be broken into multiple functions.  

So what you should see in main() is the highest level of statements in the program.  Sort of a skeleton of the program.  If the program consists of a main loop, you probably would see that there, or a call ot the main loop.  You probably woudl also see the initialization code, or at least see a call to a procedure that does the initialization, etc.
0
 

Author Comment

by:sarniscool
ID: 2768001
sorry nietod, next time don't go straight to locking up my question. just leave a comment, and i will decide if it is the answer i am looking for.
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2768881
That is not the way EE works.  In most cases questions must be solved by a "dialog" between the client and expert.  This is bacause the client usually doesn't present all th necessary information, or the expert or client may have difficulty in understanding part of what the other is saying.  So if an expert locks a question, he/she is making a commitment to help the client, although the help may not be complete at the time.  If for some reason it ends up that the expert cannot help the client, the client can reject the answer at that time.  

We have experimented with not locking questions, and just posting comments (I'm a member of the EE advisory board) and the problem is that it causes disputes and confussion and other problems.  For example, what if the "first" expert answers, but then the client needs clarification on a point.  A 2nd expert might come along an make the clarification, then you have to figure out who gets the points.  It can make things messy.  Currently if an expert has a question locked, he is saying "I can handle the question, if you want to help, that is your choice, but you probably aren't going to get the points".

So what is happening with this question?
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now