这就是我所拥有的,这个程序运行正常,我唯一的问题是它不会保存File3.txt中的approriate句子。我包括了整个程序以便理解,我没有正确做什么是阻止案例选项'r'下的句子不能保存?感谢。
void ascii (int number);
bool raffle (int number);
const int 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;
}
else if(65 <= value && value <= 90)
{
cout <<"The number you have entered corresponds to an uppercase letter in the ASCII table." <<endl;
}
else if (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;
return true;
}
}
cout << "Number not present."<<endl;
return false;
}
答案 0 :(得分:1)
您似乎忘了outfile.open()
..
更新: 好的,在这里遇到了问题(一个微妙的问题)。您不应该在switch case语句中声明ofstream。而是声明它:
int value;
ofstream outfile("G:/File3.txt", ios::out);
char option = 'r';
switch (option)
{
case 'r':
case 'R':
if(!outfile.is_open())
{
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;
}
break;
case 'e' :
case 'E' :
break;
}
outfile.close();
return 0;
如果声明int main(),则始终返回值 修改后代码的完整版本为here。
答案 1 :(得分:0)
这是修改后的代码::
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
void ascii (int number);
bool raffle (int number);
const int 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;
ofstream outfile("./File3.txt", ios::out);
switch (tolower(option))
{
case 'a':
ascii(value);
break;
case 'r':
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':
return 0;
break;
}
}
return 0;
}
void ascii (int value)
{
if (48 <= value && value <= 57)
{
cout <<"The number you have entered corresponds to a digit in the ASCII table." <<endl;
}
else if(65 <= value && value <= 90)
{
cout <<"The number you have entered corresponds to an uppercase letter in the ASCII table." <<endl;
}
else if (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;
return true;
}
}
cout << "Number not present."<<endl;
return false;
}
我认为问题在于只在一个案例分支中声明一个流...它会产生一个不安全的变量/流。 该代码在Linux中为我工作
答案 2 :(得分:0)
欢迎所有帮助我尝试解决此问题的人。我发现switch和fstream是不兼容的,所以我把我的情况改为if()和if if()语句,而不是让我的(抽奖(值== 1)我改变它(抽奖(值==)真的)它的确有效!再次感谢大家的回复,特别是你的Hoang Long! -Maria:D