With school out and summer in full swing I decided to learn C++ in order to keep myself busy. I've been playing around and learning a lot. So far I've been able to make a program that solves quadratic equations! Alas, I've encountered a problem I can't figure out for the life of me. I don't understand why this WHILE loop is not working. When I compile it everything is fine. But when ever I go to run the program no text shows up. I just stare at a blank command line screen. Here is the code:

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main (int nNumberofArgs, char* pszArgs[])
{
//declare exit or continue variable
int iexit = 1;
//begining loop
    while (iexit == 1);
    {
    cout << "This part is working \n" ;
    cout << "Type 1 to continue execution of program: " ;
    cin  >> iexit;
    } 
return 0;
}

Whats going on? I'm almost sure the problem is staring me in the face and I just can't see it. Am I using the WHILE loop wrong?

-Kurtis

You have a semicolon in the line

while (iexit == 1);

This is equivalent to the following:

while (iexit == 1) {}

So you have an infinite loop that does nothing.

so what do I change in order to make it so that while loop displays that text inside the brackets and if the iexit varible is changed to anything else other than a 1 the program stops. Do I just remove the semi-colon?

>Do I just remove the semi-colon?
Are you incapable of experimenting? It seems sort of silly that you're trying to be a programmer yet you can't even make such a simple decision as removing a single semicolon without running to us for permission. Here's a piece of advice. Rember it well, because it will determine whether you remain ignorant, or ascend the staircase to greatness.

Don't be afraid to try new and crazy things. Don't fear experimentation; that's where innovation lies. Anyone can be a cog in the machine and write boilerplate code, but a real programmer invents. A real programmer takes existing code and makes it better. A real programmer thinks that perfection just isn't good enough. Do you want to be one of those faceless code monkeys who lives a miserable life just punching keys on the keyboard? Or do you want to design and build the future?

Dude.....chill. It's just a little hobby I decided to take up. And it's just a simple question. Sorry.

Very kind of you, narue, as usual... (you know what I mean)

The real meaning behind my semicolon question was "is that how the while loops work, you don't put the semi-colon after the while command." Ofcourse I could have just deleted the semi-colon (which I did) and see if it worked. But then I wouldn't have learned anything, which is what I'm trying to do. I wouldn't understand what I did wrong, just that the solution was to delete the semi-colon. What I was really looking for was some kind of an explanation. It's like handing me the answer and not telling me how to get it. That won't help me in the future.

What I was really looking for was some kind of an explanation.

This is an empty loop body.

while(condition);

>But then I wouldn't have learned anything
You would have learned that it worked, and from there you would have gleaned insight into the real meaning behind your question. And the problem is...?

>What I was really looking for was some kind of an explanation.
Loops and conditionals consist of a condition followed by the statement that meets the condition. By "statement", I mean either a simple statement followed by a semicolon:

statement;

Or a compound statement consisting of zero or more simple statements enclosed in braces:

{
  statement;
  statement;
  statement;
}

Now, because a simple statement can be empty and consist solely of a semicolon. So this loop:

while ( 1 );

Is equivalent to this:

while ( 1 )
  ;

Which is equivalent to this:

while ( 1 ) {
  ;
}

Which is obviously an infinite loop, even though the first example may not be so obvious.

>It's like handing me the answer and not telling me how to get it.
Quite the contrary, I'm afraid. By giving you the answer to a yes-or-no question, you can figure out the explanation on your own with only a little extra work. In the process, you would learn far more than if I simply told you why it was wrong and showed you how to fix it. In my opinion, what you wanted is closer to handing you the answer and not telling you how to get it, while what I gave you actually encourages you to learn for yourself.

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.