Write a program that initially asks the user to enter a positive integer number.
ID: 3622501 • Letter: W
Question
Write a program that initially asks the user to enter a positive integer number.
After the acquisition of the number, it displays the following:
A[scii] R[affle] E[xit]
Please select an option:
Then, if the user selects:
- a (lowercase or uppercase): the program calls a user-defined function void ascii (int number) that displays whether the ASCII argument corresponds to digit, an uppercase letter, a lowercase letter or none of the above (THIS I HAVE EXECUTED CORRECTLY).
- r (lowercase or uppercase): the program calls a user-defined function bool raffle (int number) that takes the number input by the user as a parameter, generates an array of 5 random numbers ranging from 0-100 and displays then in a row(THIS I HAVE DONE CORRECTLY). If the number passed as argument is present in the array, then it displays the position of the array where the number is found and the function returns true. If the number is NOT present, then it displays "number is not present" and the function returns false. Subsequently, the main() function saves the string "The number XX is present in the array" on the file, where 'XX' is the actual number, OR SAVES THE STRING "The number XX is not present in the array" (****This is the part i need help with*****).
the file name is c:/FileHW7.txt
-e (lowercase or uppercase): the program exits.
-------------------------------------------------------------------------------------------------------
//My program runs great, only thing i am confused about is fixing: if(raffle(number)==false) so it can say on the file "The number "XX" is not present in the array"
#include <iostream>
#include <iomanip>
#include <ctime>
#include <fstream>
//function prototypes
void ascii (int number);
bool raffle (int number);
using namespace std;
int main()
{
//Variable Declaration
int number;
char select;
//opens File
ofstream output("c:/FileHW7.txt", ios::out);
//User is asked to input positive integer
cout<<endl;
cout<<"Please enter a positive integer number: ";
cin>>number;
cout<<endl;
//User is then given three option to choose from
cout<<"A[scii] R[affle] E[xit]";
cout<<endl;
cout<<"Please select an option:";
cin>>select;
cout<<endl;
switch (select)
{//start of the switch
case 'a':
case 'A':
ascii(number);
break;
case 'r':
case 'R':
if(raffle(number)==true);//If raffle returns a true value, writes that value to FileHW7.txt
output<<"The number "<<number<<" is present in the array";
output.close();
break;
//THIS IS THE PART THAT DOES NOT WANT TO SHOW UP ON THE FILE c:/FileHW7.txt when you open it (below)
if(raffle(number)==false);//If raffle returns a false value, writes that value to FileHW7.txt
output<<"The number "<<number<<" is not present in the array";
output.close();
break;
case 'e':
case 'E':
return 0; //exits the program
}//End of switch
}//End of main()
//if A[scii] option is chosen, this operation will determine if integer number is upper, lower, or digit
void ascii (int number)
{
if(number>47 && number<58)
cout<<"The number corresponds to a digit"<<endl<<endl;
else
if(number>64 && number<90)
cout<<"The number corresponds to a uppercase letter"<<endl<<endl;
else
if(number>96 && number<123)
cout<<"The number corresponds to a lowercase letter"<<endl<<endl;
else
cout<<"The number does not correspond to uppercase letter, lowercase letter, or digit"<<endl<<endl;
}//End of void ascii(int number)
//if R[affle] option is chosen, this operation generates an array of 5 random numbers (0-100)
bool raffle(int number)
{//Start of the raffle function
int array[6];
int count=0;
srand(time(NULL));
for(int i=0; i<5; i++)
{
array[i]=rand() % 101;
cout<<setw(8)<<array[i];
}
cout<<endl;
//This operation scans the array given, and finds out the position of the number, resulting in given a true value
for(int i=0; i<5; i++)
{
if(number==array[i])
{
cout<<endl;
cout<<"The number can be found in position "<<i+1<<" of the array"<<endl<<endl;//output
return true;
}
}
//If the number is not present in array, it returns false
for(int i=0; i<5; i++)
{
if(number!=array[i])
{
cout<<endl;
cout<<"The number was not found in the array"<<endl<<endl;//output
return false;
}
}
}//End of raffle operation
Explanation / Answer
case 'r':
case 'R':
if(raffle(number)==true);//If raffle returns a true value, writes that value to FileHW7.txt
output<<"The number "< output.close();
break; //REMOVE THIS
//THIS IS THE PART THAT DOES NOT WANT TO SHOW UP ON THE FILE c:/FileHW7.txt HOW TO FIX????
if(raffle(number)==false);//If raffle returns a false value, writes that value to FileHW7.txt
output<<"The number "< output.close();
break;
You have an errant break statement. This causes the code after it to not be executed (ever). Just remove the break and it will work fine.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.