473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

declaration syntax error

3
Hey everyone, i have been doing c for only a short time and i am having a small problem with compiling my code below which is a presentation question.

Here is the question:
Write a C program that will be able to ceive different types of numbers and write them in appropriate files. We shall use three files file_even which is a word document, file_odd which is a text document and file_prime which is an excel file.

Using the integers from 1 to 500,
• Write all even files with their halves in file_even. Every even number should be on its own line and a number is separated from its half with a tab.
• Write all odd numbers with their squares in file_odd. Every odd number should be on its own line and it is separated from its square with a tab
• Write all prime numbers and their thirds (rounded to 2 decimall places) in file_prime. Every prime number should be on its own line and a prime number and its third should be in separate cells (equivalent of tabs)



and here is my code:

Expand|Select|Wrap|Line Numbers
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3.  
  4.  
  5. /* A doc file file_even*/
  6. FILE *file_even;
  7. FILE *file_odd;
  8. FILE *file_prime;
  9.  
  10. /* declaring the arrays to store the values to be written to files */
  11. int i;
  12. int odd_num[];
  13. int even_num[];
  14. int prime_num[];
  15. float even_halves[];
  16. int odd_squares[];
  17. float prime_thirds[];
  18.  
  19. /* declaring the functions to be used */
  20. float half(int i);
  21. int square(int i);
  22. float third(int i);
  23.  
  24.  
  25. int main(void){
  26. /* working with the files */
  27.  
  28.  
  29. /* odd numbers */
  30. int odd;
  31. for(odd=1;odd<=500;odd++){
  32. if(odd%2==1){odd_num[i]=odd;i++;}
  33.  
  34. /*even numbers */
  35. int even;
  36. for(even=1;even<=500;even++){
  37. if(even%2==1){even_num[i]=even;i++;}
  38.  
  39. /*prime numbers */
  40. int j,i=1,c=0;
  41. while(i<=500){
  42. for(j=1;j<=500;j++){
  43. if(j%i==0){c++;}
  44. if(c==2){prime_num[a]=j;a++;}
  45. }
  46. }
  47.  
  48. for(i=0;i<500;i++){
  49. even_halves[i]=half(even_num[i]);
  50. odd_squares[i]=square(odd_num[i]);
  51. prime_thirds[i]=third(prime_num[i]);
  52. }
  53. /* storing the even numbers in a file */ 
  54. file_even=fopen("file_even.doc","a");
  55. for(i=0;i<500;i++){
  56. fprintf(file_even,"%d\t%f\n",even_num[i],even_halves);
  57. }
  58. fclose(file_even);
  59.  
  60. /* storing the odd numbers in a file */
  61. file_even=fopen("file_odd.txt","a");
  62. for(i=0;i<500;i++){
  63. fprintf(file_odd,"%d\t%f\n",odd_num[i],odd_squares[i]);
  64. }
  65. fclose(file_odd);
  66.  
  67. /* storing the prime numbers */
  68. file_even=fopen("file_prime.xls","a");
  69. for(i=0;i<500;i++){
  70. fprintf(file_prime,"%d\t%f\n",prime_num[i],prime_thirds[i]);
  71. }
  72. fclose(file_prime);
  73.  
  74.  
  75. }
  76.  
  77. float half(int i){
  78. return (i/2.0);
  79. }
  80.  
  81. int square(int i){
  82. return (i*i);
  83. }
  84.  
  85. float third(int i){
  86. return (i/3.0);
  87. }


Any one wanna tell me where the problem might be ? because it is only faced with that one error
Feb 19 '10 #1
4 1928
donbock
2,426 Expert 2GB
@naxic
I probably missed it, but I couldn't find where you described "that one error". I can't help you if I don't know what's wrong.

By the way, your code would be much easier to read if you enclosed it in CODE tags.

Reconsider the description of your output file formats. It is way too hard to format your output for Word (*.doc) and Excel (*.xls). You are going to generate three text files. The text file destined for Excel will be a tab-delimited text file. I don't know what you intend to do for the text file destined for Word.

General comment: rather than cycle through the integers 1-500 three times, why not do it once. Consider the following pseudocode:
Expand|Select|Wrap|Line Numbers
  1. Open the three output files.
  2. Begin loop for each integer from 1 to 500 {
  3.    if (the number is even) {
  4.       write the required even information to the even file.
  5.       }
  6.    if (the number is odd) {
  7.       write the required odd information to the odd file.
  8.       }
  9.    if (the number is prime) {
  10.       write the required prime information to the prime file.
  11.       }
  12.    }
  13. Close the three output files.
Feb 20 '10 #2
naxic
3
thanks for your post.
actaully that one error is a "declaration syntax error " pointed to the start of the function definitions just after the main. and how hard would it be to write to the .doc and .xls formats ?
Feb 22 '10 #3
Banfa
9,065 Expert Mod 8TB
When posting wrror messages please copy and paste them into your post tranlating the line numbers so they match the line numbers in the posted code.

Are you sure that all your { and } are matched I think the compiler thinks your functions following main are inside main (which isn't allowed). Talking of which format your braces properly it will greatly help diagnose this problem, particularly having the { and } on the same line is very poor practice. Also use proper indentation (but perhaps you did and it just got destroyed in the forum post).

It is very hard to write .doc and .xls formats because they are closed formats, Microsoft does not advertise what they are so you would need to reverse enginner the format (or read and understand the code of a program that has already reversed engineered them). It is much easier to write text data in a format that Excel or Word can use (and other office programs for that matter). I personally would use comma separated (CSV) data for Excel and either plain text or rich text format (RTF) for word.
Feb 22 '10 #4
naxic
3
thanks alot for that bit of information Banfa. I will post the error line next time and details, i actually got it working. only that i had to really struggle with altering the formats, it was a pain but i finally just used the rtf for word and comma seperation for excel. Thanks alot guys, this was really helpful.
Feb 24 '10 #5

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

Similar topics

83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
16
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following...
3
by: Michael Sgier | last post by:
Hello with the original code below I get the error: "forward declaration of `struct CPlayer'" class CPlayer; // what does this do? Instantiate the class CPlayer? // what's a forward...
7
by: Jacob Schmidt | last post by:
Could anyone correct the error in my logic here?: #include <stdio.h> #include <stdlib.h> main () { const char message1 = {"\nCurved portion of graph -- D.G.A.C.\ \n A B C"}; const char...
2
by: Nils Emil P. Larsen | last post by:
Hello I have read about a C shared library which I want to use in my C program. (It's a library to encode/decode packets from/to a serial bus running with the SNAP-protocol). Unfortunatly...
3
by: Wild Wind | last post by:
Hello, I made a post relating to this issue a while back, but I haven't received any answer, so here I am again. I am writing a mixed C++ dll which uses the following declaration: typedef...
4
by: Alan Foxmore | last post by:
Hi everyone, I'm new to C# and I was hoping I could get some clarification on the syntax for jagged and multidimensional arrays. Here is my question: The following syntax is correct for...
2
by: srikar | last post by:
Hi I am having a problem using typedef what does these declaration mean? typedef gtb(string_t, tt_type_enum) tt_type_table( &stringless, &stringequals ); typedef gtb(string_t,tt_qual_enum)...
10
by: fred.zakity | last post by:
Can someone please explain this function pointer declaration: float (* getptr (char opcode) ) (int, int); I'm confused by the syntax - I have not seen a function pointer with the extra...
11
by: mdh | last post by:
I decided to make a single file containing all the repetitive functions in K&R so that I could concentrate on the new discussions. This went along just fine, and with each new function, added the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.