Solved

if statement problem

Posted on 2000-04-30
6
162 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

839 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