|
That's good.
As for the prototype, this is OK:
void Gen2Rand (int*, int*); . No need for specifying variable names in prototype. Just let the compiler know what data type to expect for that function. Cheers!
|
|
|
|
|
IMHO, I would say that it is good practice to specify the parameter names in the function prototype, especially when the parameters are the same type. This is not for the compiler's sake but for your own sanity when you come to call the function. It will help you to get your parameters in the right order without having to analyze your code.
Tony
|
|
|
|
|
Oh yes, that's right,
Guess I'm just used to leaving my parameter names out
|
|
|
|
|
I figured it would. You might consider formatting your code (e.g., using spaces instead of tabs) so that errors such as mismatched braces would be more obvious. For example:
#define SENT 4 //"Quit" menu choice
void DisplayMenu (void)
{
printf("MENU OF OPERATIONS\n");
printf("1. Addition.\n");
printf("2. Subtraction.\n");
printf("3. Multiplication.\n");
printf("4. Quit.\n\n");
}
int GetMenuChoice (void)
{
int c;
do
{
printf ("Enter the number of the operation to try (1-4):\n");
scanf ("%d", &c);
if (c<1 || c>SENT)
printf("\aInput value is out of range.\n");
} while (c < 1 || c > SENT);
return (c);
}
void Gen2Rand (int*r1p, int*r2p)
{
int r1;
int r2;
r1 = 2 + rand() % 11;
r2 = 2 + rand() % 11;
*r1p = r1;
*r2p = r2;
}
void DrillOneProb (int c, int r1, int r2)
{
int CorAns,
Reply;
switch (c)
{
case 1:
printf("+");
CorAns = r1 + r2;
break;
case 2:
printf("-");
CorAns = r1 - r2;
break;
default:
printf("x");
CorAns = r1 * r2;
break;
}
printf(" %d, ?", Reply);
scanf ("%d", &Reply);
if (Reply == CorAns)
printf("Yes, that is correct. Good Job!");
else
{
printf("No, the correct answer is: %d", CorAns);
printf("\n\n");
}
}
int main (void)
{
int c;
int r1,
r2;
DisplayMenu();
c = GetMenuChoice();
while (c >= 1 && c < SENT)
{
Gen2Rand (&r1, &r2);
DrillOneProb (c, r1,r2);
DisplayMenu();
c = GetMenuChoice();
printf("Program Complete\n");
}
return (0);
}
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Thanks again. I will make a notation in regards to the braces. I did make some other changes because the execute did not look like the sample given. But now works perfectly.
|
|
|
|
|
OK, actually this program works fine, but only when run from Visual Studio. However, when I click on the .exe or launch it from windows command prompt, I get an error: "Unhandled exception at 0x775e59c3 in Materials.exe: 0xC0000005: Access violation reading location 0x54a075d8" and Windows shuts the program down.
The Requirement
The program is to read from a text file a list of tabulated data of a given material: Temperature, Density, Viscosity... etc.
For example:
Temperature Density Viscosity ... ...
50 0.2 0.3 ... ...
100 0.25 0.33 ... ...
The aim of the program is to read these values (several) of them, store to memory and do some sorts of interpolations.
I created a structure, each holding the properties of the material at a given temperature. I then dynamically create an array of structures based on the number of data. If I had 100 readings, I create 100 arrays to structure.
.h file
struct Material {
float temperature;
float density;
float viscosity;
};
typedef Material* MATERIAL;
The above go into the header file
.cpp file
MATERIAL* g_ptrMaterial;
void ReadFile (char* filePath)
{
vector<string> Tokens;
int i = 0;
string val;
char c;
ifstream file(filePath);
if (!file.fail())
{
g_numberOfRows = getNumberOfRows(file);
g_ptrMaterial = new MATERIAL[g_numberOfRows];
getline(file, g_fileHeader);
while (getline(file, val))
{
g_ptrMaterial[i] = (MATERIAL) malloc(sizeof(MATERIAL));
Tokens = GetTokens(val);
if (!Tokens.empty())
{
g_ptrMaterial[i]->temperature = convertToFloat(Tokens.at(0));
g_ptrMaterial[i]->density = convertToFloat(Tokens.at(1));
g_ptrMaterial[i]->viscosity = convertToFloat(Tokens.at(2));
i++;
}
}
}
else
{
cerr << "FILE NOT FOUND!";
exit(1);
}
}
vector<string > GetTokens (string val)
{
stringstream ss(val);
string temp;
vector<std::string > Tokens;
while (ss >> temp)
{
Tokens.push_back(temp);
}
return Tokens;
}
Debugging
What I did was to attach my debugger to the executable process [Tools -> Attach to Process] as it runs. I noticed that the error is triggered in the GetTokens function. It reads the first row fine i.e (50, 0.2, 0.3), but when it comes to the 2nd row it gets stuck just when about returning Tokens from the GetTokens function. What could be the problem? I guess, I'm out of my depth on this one ...
|
|
|
|
|
I did not find anything blatantly wrong with your code. The only changes I made to get it to compile in my environment were to change float to double and convertToFloat() to atof() .
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Is your application multi-threaded?
This problem can occur in such application due to some conflicts between threads.
When you single step through code, the conflict may not occur.
|
|
|
|
|
I didn't explicity create any extra threads, so I'll say it's single-threaded.
|
|
|
|
|
Exactly what does filepath contain?
When running from Visual Studio, the current directory is the one containing the .sln and .vcproj files. If your filepath only contains the name ("myfile.txt"), then it must exist in that directory.
When you run outside of visual studio, the current directory is the one containing the .exe file (normally the debug or release directory) and the file would then have to exist in that folder.
It's best to specify a full path name ("c:\\mydocs\\mydata\\myfile.txt") to avoid these problems.
Hope that helps.
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
|
|
|
|
|
Thanks Karl. The filePath is the absolute path to the text file I'm accessing. Accessing the file doesn't seem to give issues both in debug and release. A section of the code checks for file failure:
if(!file.fail())
{
}
|
|
|
|
|
Good advice but I do not think that is the cause of the problem. If he was using file (the ifstream object) without first checking to see if it was valid (e.g., bad path), then an exception would be understandable.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Problem solved and in 3 ways!
1. The "Keep it Simple Stupid" Advice works! Instead of creating an array of MATERIAL i.e Material*, I simply created an array of Material, and accessed each Material by the . notation rather than the ->.
i.e.
g_ptrMaterials = new Material [g_numberOfRows];
Material mat;
mat.temperature = 200.00;
2. One can stuff the structures into a vector:
vector<Material> vctr;
Material mat;
mat.temperature = 200.00;
mat.density = bla bla;
vctr.push_back(mat);
3. Lastly I modified the initial code. I don't know why it works ...
MATERIAL* arrMaterial = new MATERIAL[g_numberOfRows];
g_ptrMaterial = &arrMaterial[0];
arrMaterial[i] = new Material;
Perhaps the problem was mixing the malloc and new allocators? I suspected the problem was with the memory allocation, when the compiler continuously indicated errors in xmemory, dbgheap, and what not. It wasn't a v e r y pleasant experience. Anyway, I went for the first option, but one can go for any of the 3 options I suppose, without any harm.
Thanks guys
|
|
|
|
|
hi.
i work in Microsoft Visual Studio 2008 . i working in C# enviroment and creat a website project.
i want open picture with open dialog .please tell me who i can load picture with open dialog?
i am beginner .
please explain step to step.
whether i need a componet as toolbar?
please help me.
i need help body.
thank you
|
|
|
|
|
You might want to ask your question here.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Hi!
Got Windows Vista Business as the os. Ran msdev.exe with compatibility mode for Windows 98 (with others it failed, some sketchy errors appeared).
I get this error : "The project directory no longer exists. Please choose a different directory" as i try to create a new project. The funny thing is, that I created a project earlier the day without any problems. So I tried to change directories, didn't help. Searched google for the problem - found out that "Run as administrator" should fix the problem.. it didn't.
Has anyone else run into that problem?
|
|
|
|
|
How do you measure the performance of your C++ perf-critical code ?
Are you relying on automatic profilers or do you resort, for particular reasons, to compile time methods ?
If today, profiling is still an issue to you, then I would announce to you the availablity of easy profiler at Code Project along with source code that you may give it a try for your needs.
URL :
Easy Profiler - Compile-time Profiler for Unmanaged C++[^]
Use the article-forum to raise any issue and I will be glad to help.
Easy Profiler : Now open source !
http://www.codeproject.com/KB/cpp/easyprofiler.aspx
|
|
|
|
|
hi every body........
this is eswar....
i have a small doubt about barcode genaration.
is it possible to generate barcode application using vc++6.0
thanks in advance.....
|
|
|
|
|
This might be a lame answer: Isn't barcodes just some kind of a font like this? Then I guess you could write the same code you usually use to draw text, but with the barcode font instead.
|
|
|
|
|
You mean you want an application that generates barcodes OR you want to genereate a "barcode application", which could both mean that it can create OR read barcodes (thorough some input device like a webcam, a scanner or a barcode reader connected to the comp). Both should be possible. To generate barcodes you can either use a barcode font as someone already suggested, or you can try to check out how barcodes work (there are loads of infoes about it on the net, Google is your friend...) and write your own method of generation, also there are some libraries i believe around the net for such a purpose. Reading barcodes thorough a webcam or such input device is a much more problematic task. Good luck.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Sometimes you just have to hate coding to do it well. <
modified on Wednesday, November 4, 2009 10:40 AM
|
|
|
|
|
If you couldn't use MFC integrated database-classes, and you had to create a simple application that communicated with a database, how would you do that?
Would you use for instance SOCI, and make your non-GUI classes in STL? I mean, not use CString, but std::string, and when the GUI needs it, convert the std::string to CString and vica-versa?
Or would you make all the classes in MFC and add some kind of extra database layer that converts the database-output from SOCI (which then would be a std::string for instance) and make it a CString for your business-object class and then pass that class to the GUI?
Please tell if I'm not explaining this well enough.
|
|
|
|
|
Jan Sommer wrote: ...how would you do that?
I would look here. I would also look to see if the database supported Automation.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Thanks for your reply David
I'm looking for something more concrete then ODBC - That's certainly what I'm going to use since the application is going to be cross platform (and the database is an MS SQL Server). But how would you create the classes responsible for holding the database-data which the UI uses?
|
|
|
|
|
Hello,
I have a problem in writing a program with C++. I have been studying C# and Java but C++ is a way different to me, so I need your help.
My task is to make program which: reads an input from a text box than returns the nubers from that input which has two digits and their sum is 9.
For example:
Input: 12 231 81 53 522 11 63
Output: 81 63
I need it a ssimple as possible so I could understand it.
Thank you in advance.
|
|
|
|
|
Well I'm no math mastermind, but I think a way to solve your problem would be something like this...
if(input > 9 && input < 100) { //if the input is above 9 and under 100 then we pretty sure only have 2 digits
int n;
n = input / 9;
if(n*9 == input) {
//The sum is 9
}
}
this only checks ONE input - you got to figure out a way to split the input by spaces and loop through it.
Again, I have no clue if this is the simplest solution, but it should work
|
|
|
|