Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在c++;?_C++ - Fatal编程技术网

C++ 如何在c++;?

C++ 如何在c++;?,c++,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 &

这就是我所拥有的,这个程序运行正常,唯一的问题是它不会在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;



}
void ascii(整数);
布尔抽奖(整数);
常数int cArray=5;
int main()
{       
int值;
字符选项;
而(1)
{

cout您似乎忘记了
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值;
流输出文件(“G:/File3.txt”,ios::out);
字符选项='r';
开关(选件)
{
案例“r”:
案例“R”:
如果(!outfile.is_open())
{

cout这是修改后的代码:

#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;
}
#包括
#包括
#包括
使用名称空间std;
无效ascii(整数);
布尔抽奖(整数);
常数int cArray=5;
int main()
{
int值;
字符选项;
而(1)
{

cout向所有帮助我解决这个问题的人问好。我发现switch和fstream不兼容,所以我将我的case改为if()和else if()语句,而不是我的(raffle(value==1)改为(raffle(value==true),它工作了!再次感谢大家的回复,尤其是您Hoang Long!
-玛丽亚:D

你确认代码到达你打开文件的位置了吗?你确认文件确实打开了吗?文件确实存在吗?文件中有什么?文件中没有任何内容,而文件确实存在,我应该做的是在该文件中放一个字母。如果所选的数字确实存在在数组中,那么这个句子应该放进去,如果不是的话,等等。我是不是应该先打开文件,让它放进去,然后再打印到文件中?文件中没有任何内容,而且文件确实存在,我应该做的是在该文件中放一个这样的句子。如果选择的数字确实存在,我会在数组中,那么这个句子应该被放入,如果不是的话,等等。我应该先打开文件,然后再将它打印到文件中吗?是的,你必须先打开它。看看这里:嗯,我添加了outfile.open,但它仍然不起作用。对不起,我的快速判断。问题不会从后面出现m open(),因为您已经声明了文件名。这是因为当您使用break时,它不会在while(1)中中断。并且主函数在某些情况下没有返回值。您可能希望在此处查看修改后的版本:。我删除了大小写“e”,因为它会在联机编译器上引发错误。我仍然不明白它为什么会引起问题。