473,498 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

9 New Member
//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
5 1414
JosAH
11,448 Recognized Expert MVP
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
README
9 New Member
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
donbock
2,426 Recognized Expert Top Contributor
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
JosAH
11,448 Recognized Expert MVP
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
113 New Member
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

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

Similar topics

220
18799
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
125
14533
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
3
1792
by: John | last post by:
I am trying to implement a wrapper on the map container with no luck. Seems like I'm messing up something const/non-const but cant figure out the error...would appreciate ur comments... ...
6
2020
by: Simon Mansfield | last post by:
Im trying to make a C program that takes in a date (birthday) and tells the user how many days it has been since that date. So far I have got this... It compiles ok but then crashes, with no idea...
2
5038
by: Aliasgar Pocketwala | last post by:
Hi, I have an asp.net application that is using the wrong culture. I cant figure out where does it get its setting from. My web application runs as a specific user since so that I can give it...
9
2205
by: AFN | last post by:
I was just dropped into someone else's code (isn't that always so fun?). I can't figure out why a custom validation control's server event function is executing. There is nothing (that I see)...
6
1584
by: amrhi | last post by:
Whats wrong with my code , i cant access to next page. Looks like the text field cant be read by login.php Thanks <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"...
5
1813
by: Smokey Grindel | last post by:
Anyone able to figure whats wrong here? It is saying it cant convert from itself to itself? I am confused on this error that the designer is throwing... One or more errors encountered while...
5
1865
by: chevon1920 | last post by:
I am trying to do my assignment but I cant figure out how to get 8 data points per line to print to a file. Here is the assignment 1. Program asks the user to enter an odd number as a BASE,...
0
7124
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6998
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5460
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4904
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
651
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.