[英]How to save a sentence in a text file in c++?
这就是我所拥有的,该程序可以正常运行,我唯一的问题是它不会在File3.txt中保存适当的句子。 我包括了整个程序以进行理解,这是什么原因导致我无法正确执行阻止“ 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;
}
看来您忘记了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(),则始终返回值。修改后的代码的完整版本在此处 。
这是修改后的代码::
#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;
}
我认为的问题是只在一个case分支中声明了一个流。这使得变量/流不安全。 该代码在Linux中为我工作
您好,所有帮助我解决此问题的人。 我发现switch和fstream不兼容,因此我将大小写更改为if()和if()语句,而不是将我的(raffle(value == 1)更改为(raffle(value ==是),并且再次奏效,谢谢大家的回覆,尤其是Hoong Long!-Maria:D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.