2010-12-13 14 views
0

これは私が持っているものです。このプログラムは正常に動作します。唯一の問題は、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; 



} 
+0

あなたはファイルを開く時点になることを確認しましたか?ファイルが実際に開かれているか、実際にファイルが存在しているのか、それとも何であるのでしょうか? –

+0

ファイルには何もないので、ファイルが存在します。選択された数字が実際に配列に存在していれば、その文は入れられないと思われます。ファイルを最初に開いてファイルを入れてからファイルに出力します? – Maria

答えて

1

UPDATE ..あなたはoutfile.open()を忘れているようだ: [OK]を、ここでの問題(微妙なもの)を得ました。 switch caseステートメントでofstreamを宣言すべきではありません。代わりに、このようにそれを宣言します。あなたが変更されたコードのint型のmain() フルバージョンがhereで宣言した場合

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; 

、常に値を返します。ここで

+0

何もありませんファイル内にファイルが存在していれば、そのファイルにその文章の1つを入れます。選択された数字が実際に配列に存在する場合、それがなければその文は入れられます。私はそれを入れるためにまずファイルを開いて、それをファイルに出力しますか? – Maria

+0

はい、最初に開く必要があります。ここで見て:http://www.cplusplus.com/doc/tutorial/files/ –

+0

うーん、私はoutfile.openを追加し、それでも動作しません。 – Maria

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

私は危険な変数/ストリームを作るbranches..which例片側のみにストリームを宣言したと思う問題です。 コードはLinuxの私のために働いています

0

こんにちは、この問題を解決しようとしてくれた皆さん。私はスイッチとfstreamが互換性がないことを知ったので、if()とelse if()文に変更し、私の(抽選(値== 1)本当に)それは動作します!再度、あなたのすべての返信、ありがとう、ホアンロングを特にありがとう! - マリア:D

関連する問題