2016-04-03 12 views
-2

Ben 50 isimleri ile iki metin dosyaları var bu oldukça basit atama var, ben metin dosyası için kullanıcı sorguyuyorum "kız isimleri" veya "erkek ismi" ve bir isim, daha sonra program, kullanıcı girişinin seçtikleri dosyada olup olmadığını kontrol eder. Sanırım onu ​​aldım ama ikinci dosyayı açmaya çalışırken kötü bir erişim kodu alıyorum. Kodumla ilgili sorun olduğunu düşündüğüm bellek ayırma ile ilgili sorunları okudum, ancak cevaplar benim anlama düzeyimin ötesindeydi, bu yüzden sıkışmış durumdayım.ifstream dizisi ile hatalı erişim kodu .open() üye işlevini kullanarak C++

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

int main() { 

    // Declare Variables 

    ifstream inFile[1]; // male and female file streams 
    int inFileArrayVar = 0; // allows switching from female and male file streams 
    string gender; 
    string name[50]; 
    string inputName; 
    int popular = 0; 
    bool inputCondition = 1; 
    bool progLoop = 1; 

    // Welcome Message 
    cout << "*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o" << '\n' 
    << " WELCOME TO THE GENDER BINARY MACHINE" << '\n' 
    << "*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o*o" << '\n' << '\n' 

    << "Enter a name and see if it is one of the most popular Canadian female or male names in 2015. " << '\n' << '\n'; 

    // Open Files 

    inFile[0].open("GirlsNamesCanada2015.txt"); 
    inFile[1].open("BoysNamesCanada2015.txt"); 

    // User Query 

    while (inputCondition == 1){ 

    cout << "Choose to query either the male or female database by entering 'f' or 'm' " << '\n'; 
    cin >> gender; 

     if (gender == "f") { 
      cout << "You have selected the female name database. Enter the name you'd like to search: " << '\n'; 
      inFileArrayVar = 0; 
      inputCondition = 0; 
     } 
     else if (gender == "m"){ 
      cout << "You have selected the male name database. Enter the name you'd like to search: " << '\n'; 
      inFileArrayVar = 1; 
      inputCondition = 0; 
     } 
     else 
      inputCondition = 1; 
    } 

    while (progLoop == 1){ 

     cin >> inputName; 

    for (int i = 0; i < 50; i++) { 
     inFile[inFileArrayVar] >> name[i]; 
     if (inputName.compare(name[i]) == 0) { 
      popular = 1; 
      cout << inputName << " was one of the most popular names in 2015." << '\n' << '\n'; 
      i = 50; 
     } 
    } 
      if (popular == 0){ 
       cout << inputName << " was not one of the most popular names is 2015." << '\n' << '\n'; 
      } 

    cout << "If you want to contine, enter either 'f' or 'm', otherwise enter any other character. " << '\n'; 
     cin >> gender; 


     if (gender == "f"){ 
      cout << "You have selected the female name database. Enter the name you'd like to search: " << '\n'; 
      inFileArrayVar = 0; 
     } 
     else if (gender == "m"){ 
      inFileArrayVar = 1; 
      cout << "You have selected the male name database. Enter the name you'd like to search: " << '\n'; 
     } 
     else 
      progLoop = 0; 


    } 

    cout << "merci d'avoir choisi le GENDER BINARY MACHINE ******* pce out ******** " << '\n' << '\n'; 


    return 0; 
} 

cevap

2
ifstream inFile[1]; // male and female file streams 

Burada, bir eleman içeren bir dizi ilan etti. Bu dizinin bir öğesi vardır. Burada [1] bu ne anlama geliyor.

inFile[0].open("GirlsNamesCanada2015.txt"); 
inFile[1].open("BoysNamesCanada2015.txt"); 

Ve sonra dizinin, bu bir eleman dizideki birinci ve ikinci eleman eleman # 0 ve eleman 1. erişmeyi deneyin. Ne yanlış gidebilir ki? Bir dizi bildirmek zaman

:

type array[n]; 

Bu dizi elemanları elemanları # n-1 arasında # 0 içerdiği anlamına gelir.

+0

ah! çok açık! teşekkür ederim –

İlgili konular