これは私が持っているものです。このプログラムは正常に動作します。唯一の問題は、File3.txtに適切な文章が保存されないことです。私は理解のためのプログラム全体を含んだ、それは私が正しくしていないことは、ケースオプション 'r'の下で文を保存しないようにしているのですか?ありがとう。C++で文章をテキストファイルに保存する方法は?
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;
}
あなたはファイルを開く時点になることを確認しましたか?ファイルが実際に開かれているか、実際にファイルが存在しているのか、それとも何であるのでしょうか? –
ファイルには何もないので、ファイルが存在します。選択された数字が実際に配列に存在していれば、その文は入れられないと思われます。ファイルを最初に開いてファイルを入れてからファイルに出力します? – Maria