what is wrong with this code? cant figure it out.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • README
    New Member
    • Nov 2008
    • 9

    what is wrong with this code? cant figure it out.

    //what i needed to do was to read the file number.txt, and check for frequencies by using arrays.

    Code:
    #include<iostream>
    #include<fstream>
    #include<ctime>
    #include<cstdlib>
    using namespace std;
    int MAX=20;//Global variable
    void createDataFile();//Function prototype
    int main(){
    
    //Call function;
    createDataFile();
    
    //Declare Variables
    int a(0);
    int read;
    
    ifstream dataFile("numbers.txt");//open stream
    if(dataFile.fail())        //check if stream failed to open
            cout<<"In fail state"<<endl;
    //Create array
    int freq_array[20]={0};
    for(int i=MAX-1; i>=0; i--)
       cout << i << ": " << freq_array[i] << endl;
    
    dataFile.get(read);
    
    
    }
    
    /*
    This function generates a file called numbers.txt with random
    numbers from 0-MAX
    */
    void createDataFile(){
    //dataFile is the file handle for “numbers.txt”
    ofstream dataFile("numbers.txt");
    //Seed random number generator rand() function
    srand(time(0));
    //Write 150 random number to the file numbers.txt
    for (int i = 0; i<150; i++)
    dataFile << (rand() % MAX) <<'\n';
    }
    Last edited by JosAH; Nov 3 '08, 05:36 PM. Reason: added [code] ... [/code] tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    What does your code do and what is it supposed to do instead? We aren't psychic,
    sorry for that.

    kind regards,

    Jos

    Comment

    • README
      New Member
      • Nov 2008
      • 9

      #3
      Originally posted by JosAH
      What does your code do and what is it supposed to do instead? We aren't psychic,
      sorry for that.

      kind regards,

      Jos

      oh it justs creates a file called numbers.txt, inserts it with 150 random numbers. then i have to check each number's frequency by using array. for example, a number 3 in the number.txt corresponds to the element in array [2]. and thus array[2] gets x++. and so on

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Please tell us what the problem is?
        ... are you getting compile errors?
        ... is the program misbehaving?

        Whatever is going on, please describe the symptoms.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by README
          oh it justs creates a file called numbers.txt, inserts it with 150 random numbers. then i have to check each number's frequency by using array. for example, a number 3 in the number.txt corresponds to the element in array [2]. and thus array[2] gets x++. and so on
          Did you implement what you just described? For your information: we are not
          going to do it for you.

          kind regards,

          Jos

          Comment

          • archonmagnus
            New Member
            • Jun 2007
            • 113

            #6
            Are you getting a compilation error, or just unexpected program behaviour? At first glance, I see to the output of the "get" iostream method as an integer type. If you read a reference for get(), you'll see that the expected variable type should be "char".

            Comment

            Working...