"while" function

am i using the "while" function correctly? i'm still trying to work out the problems but i just want to make sure my "while" function is placed properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cstring>
#include <list>
using namespace std;

int main()
  
  
    {
    int students;
    while(students >= 1 || students <= 25)endl;
    cout << "Enter in the number of students in the class: \n";
    cin >> students;
    cout << "Enter in a number that is no less than 1 and no more than 25: ";
    cin.getline(names, 25); 
    cin.get(); 
    cin.ignore(25,'\n')
    int count = 0
    while (count++ <= 25)
    {cout<< count << endl;
    if (count == 1)
    break; 
}
system("pause");
return 0;
}
The first thing to address is that you should clean up your code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cstring>
#include <list>
using namespace std;

int main()
{
    int students;
    while(students >= 1 || students <= 25)endl;
		cout << "Enter in the number of students in the class: \n";
		cin >> students;
		cout << "Enter in a number that is no less than 1 and no more than 25: ";
		cin.getline(names, 25); 
		cin.get(); 
		cin.ignore(25,'\n')
		int count = 0
    while (count++ <= 25)
    {
		cout<< count << endl;
		if (count == 1)
		break; 
    }
    system("pause");
    return 0;
}


You also need to include the brackets in your first while loop, as it is looping nothing.

If you can do that it would be significantly easier to help you with the rest of your errors.

Edit: By the way I just indented the things I assumed needed indented -- I'm not sure what you really wanted in the while statement.
Last edited on
i wanted the students names to be limited by 25 while looping it which is what i thought the while function did.
while is not a "function", it's a control structure.
It works like this:

loop through the code contained within the brackets while the conditional expression evaluates as true.

1
2
3
4
bool flag = true;
while(flag == true) {
	//do something.
}


If, in the example above, flag should for any reason no longer be true, the loop with terminate.

I'm not sure what the second while loop is for. From my expeirence, you can just use that one while loop opin there for the assignment,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include <iostream>
#include <cstring>
#include <list>
using namespace std;

int main()
{

    int students;
cout << "Enter in the number of students in the class: \n";
		cin >> students;

    while(students >= 1 || students <= 25)endl;
		
		cout << "Enter in a number that is no less than 1 and no more than 25: ";
		cin.getline(names, 25); 
		cin.get(); 
		cin.ignore(25,'\n')
		student++;
    while (count++ <= 25)
    {
		cout<< count << endl;
		if (count == 1)
		break; 
    }
    system("pause");
    return 0;
}
Last edited on
Topic archived. No new replies allowed.