working with do-while statements

so im haivng trouble writing this program, here is my task: "Develop a program that accepts several integers from the keyboard (range:1 to 25) Program must ask the user the number of values in advance and check for valid numbers. Invalid numbers are rejected but the user is given a chance to complete the count. (HINT: Think about saving the valid numbers into a file while the numbers are being entered. Once all the numbers have been entered, you can close the file and open it again for reading. "

Im having trouble figuring out how i can get the user to enter numbers (from 1-25) and only storing those numbers in a file and ignoring any numbers beyond the range of 1-25. so far im workign with this:

#include<iostream>
#include<fstream>
using namespace std;
void main()
{
int number;
ofstream outfile;
ifstream infile;

outfile.open("numbers.txt");
if (!outfile)
{
cout<<"File cannot be created\n";
exit(0);
}


do
{
system("cls");
cout<<"Enter your numbers: "<<endl;
cin>>number;
outfile<<number;


}while ((number < 1) || (number > 25));
}

i dont think im understanding this very well, help would be greatly appreciated
pseudo-code:


Ask user how many numbers they want to enter;
While the user has entered fewer than the specified number of valid numbers:
1. Ask user to enter a number;
2. if the number entered is within the valid range, then:
a. increment the count of valid numbers entered;
b. write the number to file;
else
output an error message
Last edited on
do
{
system("cls");
cout<<"Enter your numbers: "<<endl;
cin>>number;
outfile<<number;

}while ((number < 1) || (number > 25));

If i am correct, there is an Logic error in your While statement...
I also suggest you to use IF statement inside the do-While loop so that an integer not within the range is not written in Datafile..
Also Use Increment operator inside the If statement to count the valid numbers..
I'm going to write the code for you, but here is what you're doing wrong.
You need to ask the user how many numbers he wants to enter before the while loop, otherwise everytime it loops it is going to ask the user for a new number infinitely as long as he enters a number between 1-25 or it crashes.

You also need an if statement to check to make sure the number entered is between 1-25

You need a counter and the while loop the check the count of how many numbers entered versus how many they were supposed to enter.

Here's the 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

#include<iostream>
#include<fstream>
using namespace std;
void main()
{
int number, howMany;
int count = 0;
ofstream outfile;
ifstream infile;

outfile.open("numbers.txt");
if (!outfile)
{
cout<<"File cannot be created\n";
exit(0);
}

cout << "How many integers would you like to enter? ";
cin >> howMany;

cout << "Please enter an integer 1-25 only please: ";
do
{

system("cls");
cin>>number;

if(number >= 1 || number <= 25)
{
outfile << number << " ";
count++;
}
else
{
cout << "The number you entered was invalid.";
}


}while (count < howMany);

return 0;
}


When I compiled it I got 3 errors from your exit(0), system("cls") statements, but I'll let you fix those as I have to go to class.

That code should work out though, let me know if not.






Mate334 is correct...

You're using incorrect logic here...
}while ((number < 1) || (number > 25));

This will set up an infinite loop! Think of it this way... if a user enters 65, that's greater than 1 which will evalute to FALSE, but 65 is also greater than 25, which evaluates to true and makes the whole statement true!

When checking for ranges, you need to use logical AND...
}while ((number >= 1) && (number <= 25));

..this way if the user does enter 65, it's greater than 1 (TRUE) *AND* it's also greater than 25, so the whole statement evaluates to false.
Last edited on
Topic archived. No new replies allowed.