I haven't touched C(++) in a long time, and I need a lot of help...

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
The last time I touched C++ was 20 years ago. It was Borland, and I was learning to write console programs for DOS. I tried their Builder product, but saw that VC++ was now free, so Ive loaded it. I have forgotten a great
deal, and have a large knowledge void relative to Windows programming with VC++. A GREAT MANY QUESTIONS have occurred in the first ten minutes of effort. My ultimate goal is to compute some values by year, and store them in a CSV file.
Ive described myself as NO EXPERT with VC++, so please dont offer code that youve written that does a great many things I dont need and would not grasp well enough to feel comfortable using. If, on the other hand, you can answer my questions
in a very basic "KISS" manner, you will be demonstrating your expertise at responding in a manner consistent with what Im seeking.

My questions follow:
1. I needed to include a couple of header files (math.h and stdio.h). I thought I could simply "Add" them by right clicking on "Header Files" and typing inthe names, just as I typed them in parentheses. That didnt work.
In the end, I had to go to "Main.cpp" and enter:
#include <stdio.h><br/>
#include <math.h> .
Why is that? Doesnt VC++ know that if I type header file names under the "Header Files" list, it should try to match those names with existing header files? Should I now simply remove those names from the "Header Files"
list and recognize that what Ive typed under "Main.cpp" is the code that is actually loading the header files?
2. As I mentioned, I need to write some pairs of years and volumes to a csv file. Ill load it into a spreadsheet later and plot it, so all the plots in the discussion Im writing look the same. I anticipate saving roughly five or six data
pairs. Im attempting to learn (or "re-learn") a little about structures and how to use such data types in VC++, so Ive borrowed some code I found on line. Im trying to adapt it to make it work for my software, although using
this level of complexity is clearly overkill for my purposes. Still, it may help me to re-learn/learn how to use C(++) in a Windows environment with VC++.

Ive got the following under the "Main.cpp" (actually "Friedman.cpp" section:
// Friedman.cpp : main project file.<br/>
<br/>
#include "stdafx.h"<br/>
#include "Form1.h"<br/>
#include <stdio.h><br/>
#include <math.h><br/>
<br/>
struct SizeAll{ <br/>
int year; <br/>
double R; <br/>
}; <br/>
<br/>
int write_to_file(int count, struct SizeAll *data, char const *fileName) <br/>
{ <br/>
FILE *f = fopen(fileName, "w"); <br/>
if (f == NULL) return -1; <br/>
while (count-- > 0) { <br/>
// you might want to check for out-of-disk-space here, too <br/>
fprintf(f, "%d,%d", data->year, data->R); <br/>
++data; <br/>
} <br/>
fclose(f); <br/>
return 0; <br/>
} <br/>
<br/>
using namespace Friedman;<br/>
<br/>
[STAThreadAttribute]<br/>
int main(array<System::String ^> ^args)<br/>
{<br/>
// Enabling Windows XP visual effects before any controls are created<br/>
Application::EnableVisualStyles();<br/>
Application::SetCompatibleTextRenderingDefault(false); <br/>
<br/>
// Create the main window and run it<br/>
Application::Run(gcnew Form1());<br/>
return 0;<br/>
}
The segment of the previous "Main.cpp" that Ive added is limited to:
#include <stdio.h><br/>
#include <math.h><br/>
<br/>
struct SizeAll{ <br/>
int year; <br/>
double R; <br/>
}; <br/>
<br/>
int write_to_file(int count, struct SizeAll *data, char const *fileName) <br/>
{ <br/>
FILE *f = fopen(fileName, "w"); <br/>
if (f == NULL) return -1; <br/>
while (count-- > 0) { <br/>
// you might want to check for out-of-disk-space here, too <br/>
fprintf(f, "%d,%d", data->year, data->R); <br/>
++data; <br/>
} <br/>
fclose(f); <br/>
return 0; <br/>
}
<a>Under Form1.h I have a single button, containing the following code:
#pragma endregion<br/>
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {<br/>
double R;<br/>
int counter,time;<br/>
struct SizeAll Universe;<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
for ( counter = 1 ; counter < 1001 ; counter++ )
<br/>
R = R + 1;<br/>
if (counter ==100)<br/>
write_to_file(1, Universe, "Friedout.csv");<br/>
<br/>
}<br/>
};<br/>
}
<a>I doubt that Im using this "borrowed code" correctly. Im simply trying to get it to output a single pair of CSV values to a file, to see if I can even get this software to compile. Somehow, eventually, I need to write a total of
five CSV values to the file, but one step at a time.
The errors produced during the build are:
visual studio 2010projectsfriedmanfriedmanForm1.h(84): error C2079: Universe uses undefined struct Friedman::Form1::button1_Click::SizeAll
visual studio 2010projectsfriedmanfriedmanForm1.h(94): error C3861: write_to_file: identifier not found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
<a>HOW DO I ELIMINATE THESE ERRORS???
3. Finally, how do I specify the directory into which to write the CSV file? Id probably like to put it on my desktop under XP, so I can easily find the file and load it into the spreadsheet. Ill need to write over the contents of the
file each time, so I may also need to first erase the file before adding data to it.
And the last, most important point: THANK YOU for any help that you may elect to offer.<a>

View the full article
 
Back
Top