void ascii (int number);
bool raffle (int number);
constint cArray=5;
int main ()
{
int value;
char option;
while (1)
{
cout <<"Enter a positive integer number: " <<endl;
cin >>value;
cout <<endl;
cout <<"A[scii]""\t\tR[affle]""\t\tE[xit]" <<endl;
cout <<"Please select an option: " <<endl;
cin >>option;
cout<<endl;
switch (option)
{
case'a':
case'A':
ascii(value);
break;
case'r':
case'R':
ofstream outfile("G:/File3.txt", ios::out);
if(!outfile)
{
cout<<"File could not be opened"<<endl;
exit(1);
}
if (raffle(value)==1)
{
outfile<<"The number "<<value<<"is present in the array."<<endl;
}
else
{
outfile<<"The number "<<value<<"is not present in the array."<<endl;
}
outfile.close();
break;
case'e':
case'E':
return 0;
break;
}
}
}
void ascii (int value)
{
if (48 <= value && value <= 57)
{
cout <<"The number you have entered corresponds to a digit in the ASCII table." <<endl;
}
elseif(65 <= value && value <= 90)
{
cout <<"The number you have entered corresponds to an uppercase letter in the ASCII table." <<endl;
}
elseif (97 <= value && value <= 122)
{
cout <<"The number you have entered corresponds to a lowercase letter in the ASCII table." <<endl;
}
else
{
cout <<"The number you have entered corresponds to none of the above." << endl;
}
}
bool raffle (int value)
{
int random[cArray];
srand(time(NULL));
for (int i=0; i<5; i++)
{
random[i]= 0+rand()%(100+1-0);
cout<<random[i]<<" "<<endl;
}
for (int j=0; j<5; j++)
{
if (value == random[j])
{
cout << "\n" <<j<<endl;
returntrue;
}
}
cout << "Number not present."<<endl;
returnfalse;
}
My problem is that under the switch option R, I need the program to save a sentence to a file. Everything else runs good, except it won't save the proper sentence to file3. Any suggestions would be greatly appreciated, and sorry for the long code! Thank you!
I took this program and ran it with Code::Blocks and a MinGW compiler, and it was spitting out some garbage about the switch logic and fstream being incompatible. Replace your switch with if () and elseif() and that should fix this problem. Also change the "raffle(value) == 1" to "raffle(value) == true" for readability.