By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,827 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

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

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

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<fstream>
  3. #include<ctime>
  4. #include<cstdlib>
  5. using namespace std;
  6. int MAX=20;//Global variable
  7. void createDataFile();//Function prototype
  8. int main(){
  9.  
  10. //Call function;
  11. createDataFile();
  12.  
  13. //Declare Variables
  14. int a(0);
  15. int read;
  16.  
  17. ifstream dataFile("numbers.txt");//open stream
  18. if(dataFile.fail())        //check if stream failed to open
  19.         cout<<"In fail state"<<endl;
  20. //Create array
  21. int freq_array[20]={0};
  22. for(int i=MAX-1; i>=0; i--)
  23.    cout << i << ": " << freq_array[i] << endl;
  24.  
  25. dataFile.get(read);
  26.  
  27.  
  28. }
  29.  
  30. /*
  31. This function generates a file called numbers.txt with random
  32. numbers from 0-MAX
  33. */
  34. void createDataFile(){
  35. //dataFile is the file handle for “numbers.txt”
  36. ofstream dataFile("numbers.txt");
  37. //Seed random number generator rand() function
  38. srand(time(0));
  39. //Write 150 random number to the file numbers.txt
  40. for (int i = 0; i<150; i++)
  41. dataFile << (rand() % MAX) <<'\n';
  42. }
Nov 3 '08 #1
Share this Question
Share on Google+
5 Replies


Expert 10K+
P: 11,448
What does your code do and what is it supposed to do instead? We aren't psychic,
sorry for that.

kind regards,

Jos
Nov 3 '08 #2

P: 9
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
Nov 3 '08 #3

Expert 100+
P: 2,415
Please tell us what the problem is?
... are you getting compile errors?
... is the program misbehaving?

Whatever is going on, please describe the symptoms.
Nov 3 '08 #4

Expert 10K+
P: 11,448
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
Nov 3 '08 #5

archonmagnus
100+
P: 113
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".
Nov 4 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.