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
Solved

C++ For Loop

Posted on 2006-10-26
6
203 Views
Last Modified: 2010-04-01
Hi experts,


I have a vector, vectorOfRadios of Radio objects with each object containing one boolean member variable, called IsRadioOn. I loop through each Radio in vectorOfRadios starting at the first Radio and looking ahead to the next radio i.e. vectorOfRadios.at(n+1) and check if the radios are either of the following options and based on those options call different functions:

for (int n = 0; n < vectorOfRadios.size()-1; n++)
{
        bool bFirstIsOn = vectorOfRadios.at(n).IsRadioOn;
        bool bSecondIsOn = vectorOfRadios.at(n+1).IsRadioOn;

        for (int j = 0; j < size; ++j)
        {
            int x = 0;
            int y = 0;
            if ((bFirstIsOn == true) && (bSecondIsOn == true))
            {
              // A, B, C, D and E are functions that return an integer.
                 x = A();
                 y = B();
            }
            else if  ((bFirstIsOn == true && (bSecondIsOn == false))
            {
                x = A();
                y = D();
            }
            else if  ((bFirstIsOn == false) && (bSecondIsOn == true))
           {
               x = B();
               y = C();
            }
           else if  ((bFirstIsOn == false) && (bSecondIsOn == false))
           {
               x = B();
               y = D();
           }
        
            // Do other stuff here using variables x and y
            // ...
      }
}

Is there a more elegant method for doing the above?

Thank you,
j.
0
Comment
Question by:XPUSR
  • 3
6 Comments
 
LVL 19

Accepted Solution

by:
BrianGEFF719 earned 23 total points
ID: 17815878
if (bFirstIsOn == true)
 x = A();
else
 x = B();

if (bSecondIsOn == false)
 y = D();
else
{
 if(bFirstIsOn == true)
  y = B();
 else
  y = C();
}
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 17815888
or you could try with the conditional iif.

x = (bFirstIsOn) ? A() : B();
y = (!bSecondIsOn) ? D() :;
y = (!bSecondIsOn && bFirstIsOn) ? B() : C();

0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 17815899
Sorry try this:


x = (bFirstIsOn) ? A() : B();
y = (!bSecondIsOn) ? D() : 0;
y = (bSecondIsOn && bFirstIsOn) ? B() : C();


-Brian
0
 
LVL 4

Assisted Solution

by:Raymun
Raymun earned 22 total points
ID: 17953688
An alternative:

if( bSecondIsOn ) {
      if( bFirstIsOn ) {
            x = A();
            y = B();
      }
      else {
            x = B();
            y = C();
      }
}
else {
      y = D();
}

Both are correct. BrianGeFF719's is more concise but mine is more versatile.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Title # Comments Views Activity
Fully specialized class template function 21 142
Dialogbox API leak? 18 98
White board coding practice 3 90
passing data from one form to another form in c++ 27 82
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

860 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