开发者

How to save a sentence in a text file in c++?

This is what I have, this program runs ok as it stands, the only problem I am having is that it will not save the approriate sentence in File3.txt. I included the entire program for understanding, what is it that I am not doing correctly that is preventing the sentence under the case option 'r' to not save? Thanks.

void ascii (int number);
bool raffle (int number);
const int cArray=5;




int main () 
{       

    int va开发者_开发技巧lue;

    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;



}


It seems you forget outfile.open() ..

UPDATE: Ok, got the problem here (a subtle one). You shouldn't declare ofstream in the switch case statement. Instead declare it like this:

    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;

And always return value if you declare int main () Full version of the modified code is here.


Hello to all who have helped me try to solve this problem. I found out that the switch and fstream were incompatible, so I changed my cases to if() and else if() statements, and instead of having my (raffle(value==1) i changed it to (raffle(value==true) and it works! So once again, thank you all for your replies, esp you Hoang Long! -Maria :D


Here is the modified code ::

#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;
}

The problem I think was with declaring a stream in side only one of the case branches..which makes an unsafe variable / stream. The code is working for me in Linux

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜