very simple do/while statement

I'm trying to make a program where people enter how many lines they want and the loop will print that many, what I have so far is: #include <iostream.h>
#include <windows.h>

int main()
{
int count = 0;
int lines;

cout<<"Enter a number to display that many lines"<<endl;
cin>> lines;

do
{
count++;
cout<<endl;
cout<<"test";

}while(count = lines);

system("PAUSE");
return 0;
}



but it is looping indeffinatly, thanks for any help!
count = lines should be count == lines
You should #include<iostream> (without '.h') and then use the std namespace
you don't need windows.h
system("PAUSE"); is bad: http://www.cplusplus.com/forum/articles/7312/ http://www.cplusplus.com/forum/articles/11153/
closed account (z05DSL3A)
}while(count < lines);
you will always get an infinite loop with that program, I actually did same exact same thing when I first started learning c++. In you while statement you have "count = lines". The operator "=" sets the value of a variable to another. So the condition you set in the while statement reads "count takes the value of lines", since that is always true you have an infinite loop. To check if one var. is the same as another do this "count == lines" which checks if count is the same as lines but doesn't change it's value, however in your program this isn't going to work, I'll let you see if you can figure out why.

Cheers and Good luck!
closed account (z05DSL3A)
Thinking about it it would also be better to use a while loop or a for loop, not a do...while loop, the do...while will output a line even if the user enters 0 as input.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
    int count = 0;
    int lines;

    cout<<"Enter a number to display that many lines"<<endl;
    cin>> lines;
    
    cout << "With a while loop: " << endl;
    while(count < lines)
    {
        cout << "test" << endl;
        count++;
    }
    //--------------
    cout << "With a for loop: " << endl;
    for(int i(0); i < lines; ++i)
    {
        cout << "test 2" << endl;
    }

    return 0;
}

Last edited on
Yea my teacher taught us to use a while loop for when you did not know how many times to loop around for ex in game a while loop might be used to keep checking if you are still alive etc. But the for loop should be used when you know how many times to loop, and in this case you do know, the user tells you when he inputs lines.
closed account (S6k9GNh0)
Death in games are normally event driven >.>. The for loop can be used in many ways. In an emulator, it's often used with for(;;) for infinite and optimized looping. while(1) isn't used because most compilers check to see if 0 is true or false which loses obvious wasteful CPU. In these cases, it shouldn't matter since in the end the opcodes given should be the same (I've been trying to find the documentation to back this up unsuccessfuly).

Though often or not, the for loop is standard when an incrementing index variable is needed. The while loop is common for having a variable checked for true or false before code is execute and the do until loop is used when your code needs to be executed at least once before the given boolean is checked.
Last edited on
For while(1) (I assume you meant 1, not 0), the compiler should automatically change it into true, then realize that it is a constant, so there should be no difference.
Thanx for all the help guys, I get what to do now =) btw our teachers made us use windows.h and iostream.h and system("PAUSE");
Tell your teacher that iostream.h is not a standard header.
If you want to prove it, get a copy of the ISO/IEC 14882 document
closed account (S6k9GNh0)
And using system is a very bad practice. Most of what you most likely will use, will not require windows.h plus it is very system independant.
Topic archived. No new replies allowed.