How do I nest a while loop inside a do..loop without using if statement?

Recommended Answers

All 4 Replies

I'm not certain I'm following you, here; you should be able to nest any sort of loop inside of any other loop, and there's no need for an if() statement.

Can you explain just what you're trying to accomplish? I can hazard a few guesses as to what you might want, but it would make things much easier if you could explain your intentions, rather than asking for a specific mechanism.

On a hunch, it sounds like what you want is to have a loop which gets broken somewhere other than the loop conditional. If so, then you would indeed want (not necessarily need, but it would be a very limited case otherwise) to use an if() statement controlling a break statement. But as I say, that's just a guess - without knowing your specific intentions, I can't give a specific answer.

I have figured that much. I'm glad you back. Why on earth would this not go ans execute on the main function? Unfortunately it give no errors. It just keeps asking me the gender.

// Assignment 2 Question 5a
#include <iostream>
using namespace std;
// The required function inputAndValidate should be inserted here.
void inputAndValidate (char& sex, int& yearMark, int& examMark){

do { cout << "Please enter gender: " << endl;
cin >> sex;}
while (sex != 'm' || sex != 'f');


}

int main( )
{
int yearMark, examMark;
char sex;
inputAndValidate(sex, yearMark, examMark);
cout << "This student is ";
if (sex == 'm')
cout << "male and has a year mark of";
else
cout << "female and has a year mark of ";
cout << yearMark << " and an exam mark of "
<< examMark << endl;
return 0;
}

The conditional of the while() loop is incorrect. You want and AND operator (&&) there, not OR.

The conditional of the while() loop is incorrect. You want and AND operator (&&) there, not OR.

Schoil-R-LEA you're a beauty! I cant believe I it it was just that one minor thing. thank you.

By the way there was a question I asked a moment ago, do you mind responding to that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.