Use the proper format specifier in a printf statement:
printf("%.4f", myFloatVar);
This will display 4 positions past the decimal. See printf doc for more details.
Use the proper format specifier in a printf statement:
printf("%.4f", myFloatVar);
This will display 4 positions past the decimal. See printf doc for more details.
A couple of things: first, you are mixing your class definition and method implementations together. You need to separate these into a header file for the class definition, and a code file for the method implementations. A short example (using your "booking" class):
New booking.h file:
#include <string>
using namespace std;
class booking
{
public:
booking(); /* constructor */
float calTicket(int numpass, string ctype);
};
New booking.cpp file:
#include "booking.h"
float booking::calTicket(int numpass, string ctype)
{
// Stuff the method is supposed to do...
}
Once you have that all separated, you should be a lot better off (code will be much more readable, too). Also, you don't need the "Friend" declarations. In your code, you just need to include the proper header, then just declare an instance of the class you want to use.
In your loop in main(), you are using "i" as the index to the array as you insert items. How could you use "numCars" in the same way, but yet at the end of the loop, have it contain the index of the last item in the array?
Also, your input code is missing the code to ask the user for mileage.
What sort of syntax error? Also, the class listings for "dog" and "showdog" may help.
Code looks better, but there are still a couple of trouble spots - first, now, in your while loop, you are not incrementing "i" at all! In your assignments to the array, you will overwrite the same item (actually, since "i" is declared, but never initialized, there's some uncertainty there - always initialize your variables!) Second, in your display loop, you are still iterating over 10 elements, when you may not have that many. As you enter each item into the array, increment a counter so you know how many you loaded.
Now, to tackle the virtual stuff. In your code, you are using 2 arrays. The idea is that you can use one array of type "Dog", and put both "Dog" elements and "ShowDog" elements in it, since "ShowDog" is derived from "Dog".
Then, you iterate through the array, calling the "displayDog" method on each, and the correct method gets called for each element, due to declaring the "displayDog" method as virtual in the base class.
A couple of things to look out for:
1) In your loop, you are iterating 10 times. However, your data file may not have 10 "dog" items in it. You might be better off reading until EOF.
2) Your "for" loop increments your variable ("i"), but then inside the loop you are incrementing it as well. This will cause trouble.
3) when you assign a new item to "dogList" - do you really want to be assigning it to a spot in the array specified by "NUM_DOGS"? Your assignment to "showDogs" looks a bit better (of course, after you fix the incrementing issue with "i").
You're pretty close - just a bit of cleanup and you should be in good shape.
This site may help:
http://www.webmonkey.com/reference/Color_Charts
After the "#" sign, each set of 2 digits represents the Red, Green and Blue components of the color.
I take it that when you say "publish", you are using the publish command from the "Build" menu... if that's the case, then yes, when you get the output package from the Publish process, and then install it, it does install it into a subfolder of <SYSTEMDRIVE>\Documents and Settings\User\Local Settings\Apps\2.0. That is the folder you are seeing when you get the value from the "Application.StartupPath" property.
If you just copy your EXE (and any required supporting files) from your "Debug" (or "Release") folder to another location on your machine, the "Application.StartupPath" property will show you the correct location.
I'm not sure what you're after. You can load XML in from a file stream. You can put XML into a string. You can read XML into a string from a file stream. Perhaps some more detail about what you're trying to accomplish will help us in guiding you to a solution.
To answer the question about adding the ".cpp.o" rule: When I ran your makefile as is, I was seeing the compilation of the .cpp files (trying to make the output .o files) working, but it was not picking up the $(COMPILERFLAGS) variable. That is, this rule in your makefile:
$(BINARY): $(OBJS)
$(CPPCOMPILER) $(COMPILERFLAGS) $(INCLUDES) -o $(BINARY)
when compiling the .cpp sources, was producing this output:
g++ -c -o /root/workspace/source/Main.o /root/workspace/source/Main.cpp
Where is the "-W -Wall"? Where is the "include" directive? It was not in the output of the command make was executing. This is why you were getting the build errors - it didn't know where to find the header files. That's because make was executing a default rule for the .cpp files, and not using the directives in the rule for building the executable. Once I added the ".cpp.o" rule, everything built - almost. The .cpp built and made the correct .o files, but when it was time to make the executable, I got this output:
g++ -W -Wall -c -I/root/workspace/makedemo/header -o output /root/workspace/source/Main.o /root/workspace/source/TestFile.o
That's because I had to put a "-c" into the COMPILERFLAGS variable, and now that it's trying to build the executable, the "-c" is trying to output object code rather than perform the link. This required the removal of the $(COMPILERFLAGS) variable from the rule that was building the executable.
Some of the macros for make: "$<" is the name of the file that caused the rule to be executed, …
In my sample, I put the loading of "about:blank" in Form_Load() to give the control time to navigate (and more important), initialize the "Body" member of the WebControl. If you put the "webcontrol1.Navigate" in the same routine that you try to load the control with your content, you will hang (I was seeing the same thing). Another sample I saw on the Internet (it was in VB.NET, though), was performing "DoEvents" to yield control back to the main loop to allow the webcontrol to intialize. I tried the same thing with a Thread.Sleep(200) call, but it didn't help.
All I can say is try to separate the "about:blank" call into a separate routine (perhaps even the Form's constructor) to give it time to load and initialize the "Body" element, so in a later routine, you can supply your content to the control and be OK.
You can use the WebBroswer control to display text. Drag a WebBrowser control onto your form. Put this in your Form_Load():
webBrowser1.Navigate("about:blank");
Then, use this code to fill it:
bool bError = true;
while (bError)
{
try
{
webBrowser1.Document.Body.InnerHtml = "<HTML><BODY><H1>Hi!</H1</BODY></HTML>";
bError = false;
}
catch (Exception exp)
{
;
}
}
The navigate to "about:blank" needs some time to complete; the loop and try..catch will help if the navigation is not complete. If you try too soon to put text into the control, you may get a NullReferenceException, since the "Body" member will still be NULL.
Hope this helps!
Try this makefile:
TOP_DIR = /root/workspace
S_DIR = $(TOP_DIR)/source
H_DIR = $(TOP_DIR)/makedemo/header
HEADERS = $(shell ls $(H_DIR)/*.H)
SOURCES = $(shell ls $(S_DIR)/*.cpp)
COMPILERFLAGS = -W -Wall -c
DEBUGFLAGS = -g
CPPCOMPILER = g++
INCLUDES = -I$(H_DIR)
OBJS = $(SOURCES:.cpp=.o)
BINARY = output
all: $(BINARY)
$(BINARY): $(OBJS)
$(CPPCOMPILER) -o $(BINARY) $(OBJS)
.cpp.o: $(SOURCES) $(HEADERS)
cd $(S_DIR); $(CPPCOMPILER) $(COMPILERFLAGS) $(INCLUDES) $<
depend:
makedepend $(INCLUDES) -f- -- $(SOURCES) > .depend_file
clean:
rm -rf $(S_DIR)/*.o .depend_file $(BINARY) *~
#DO NOT DELETE
Your original makefile was not pointing to the proper include file, so it was not picking up your headers. That was the source of your errors. Here, I've suggested some changes that make the file somewhat cleaner (at least to me). See if they work for you.
I made the changes to the top, by adding some variables for the directories, since I'd have to go to them (for example, in the "clean" rule).
The other major addition is the ".cpp.o" rule to build the objects prior to linking the executable. I found that the one rule in the original Makefile was not picking up the stuff in COMPILERFLAGS, so I knew it was hitting a default rule for the .cpp items.
The *.o files will, by default, be dropped in the current folder where make is being run (in your example, /root/workspace/makedemo), but then the variables for the object files that have the paths pre-pended would fail during the link step. I fixed that by performing a "cd …
I'd do a separate array.
Well, checking your boxes for user input will be a chore. Having the controls in an array would help with that.
:icon_eek: There's a lot of stuff flying around here.
OK, here's some code to get the grid of TextBoxes displayed, and a button that shows some basic interaction:
Put this at the top of your form's class:
TextBox[,] tb = null;
Put this into your Form_Load():
tb = new TextBox[9,9];
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
tb[i, j] = new TextBox();
tb[i, j].Visible = true;
// These numbers are just a bit wider and taller than the text
// box we're adding. Feel free to goof with them to get the
// look you want
tb[i, j].Location = new Point(i * 41, j * 35) ;
tb[i, j].Width = 40;
this.Controls.Add(tb[i, j]);
}
}
If you don't have a Form_Load(), you can get one by double-clicking on any blank area inside the form on the designer.
Drag a button onto the form, all the way to the right. Double click it and put this code into the button1_click() method (or whatever it's named):
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
tb[i, j].Text = ((i * 9) + j).ToString();
}
}
Some points:
The declaration of the array of text boxes must be global - you'll be accessing them later, so you can't declare it inside a method (like Form1()).
The button shows how you can …
I agree, you could represent the suits with an enum; but since he was looking for a class/inheritance feature, I suggested that one. Certainly your approach works as well.
Perhaps you could define a base class, called "Card" that has the rank values contained within it, as cards of all suits have a rank value. Then, you could define 4 derived classes, "SpadeCard", "HeartCard", "DiamondCard" and "ClubCard".
Good luck!
Please post your updated makefile and I'll take a look.
Your shell commands are off. Try this:
HEADERS = $(shell ls /root/workspace/makedemo/header/*.h)
SOURCES = $(shell ls /root/workspace/source/*.cpp)
Even though it's wordy, the compiler is trying to tell you something. Namely, that std::string variables can't be used in a strncmp (or other related) function. You have 2 alternatives (actually, it's C++, so you probably have 9 or 10 options, but 2 will do).
1) use the "copy" method on the std::string to copy a char * out of the string and into a variable (one for each string) so you can use the strncmp function :icon_eek: .
2) the better option, use the substr method on the strings you're comparing to specify the portion of the strings to compare. Then you can just use:
if (strA.substr(0,1) == strB.substr(0,1))
{
// true case
}
There are a number of things to look at in your code:
1) validAcctNumber - pretty close, though you should just remove passing of "size" and use your MAX_ACCOUNT constant.
2) checkAccountUsed - other than zeroing out the account amount, this is almost a duplicate of the validAcctNumber function. The purpose of this function (based on its name) is to determine if the account that is passed in is in use. You need to have a way to tell if the account in question is in use. You could initialize the array of checking accounts to -1.0. That way, once you set up a new account and put a zero into it, unused accounts will have a --1.0 (assuming, of course, that bank accounts can't have a negative balance ;) )
3) in "depositCheck", you gather the amount from the user, but you don't store it in the array holding the checking balance. And of course, when you report the balance of the account, you're just reporting back what they deposited, not the balance. You should add the deposit amount into the array, then report the new balance.
Those are a few things to look at. Once you get that working, the other functions are very similar.
On line 52, you are calling your function "checkAccountUsed(MAX_ACCOUNT, chk)" - I think you want to pass "val" instead of "MAX_ACCOUNT". If you look at the "if" statement in your "checkAccountUsed" function, it says if the passed in account is less than MAX_ACCOUNT - which, of course, it isn't, so the function immediately returns false.
Also, your next function, "depositCheck" also calls "checkAccountUsed", so you may want to eliminate the first call to the function, since your deposit function does it. You probably want to pass "val" to the "depositCheck" function as well.
You are technically already running the program, so you really only need to check to see if the user doesn't want to run it:
if (answer == 'n')
return 0;
Otherwise, execution will continue.
Some things I'm reading seem to indicate that you cannot use the IDE to create a DataSet that is hooked to a remote SQL Server database. I don't happen to have VS 2008 Express, so I can't test that (the previous example I gave was with the full version of VS2008).
However, in code you should be able to get to the database. Try this code in a test project:
At the top of the module, put:
using System.Data.SqlClient;
In a button_click routine somewhere, put this code:
SqlConnection oConn = new SqlConnection("Server=DBServer;Database=DB;user id=youruser;password=yourpw");
SqlDataReader myReader = null;
int count = 0;
try
{
oConn.Open();
}
catch (SqlException ex1)
{
MessageBox.Show(ex1.Message, "Exception: Open");
return;
}
SqlCommand cmd = new SqlCommand("Select * from tb_table", oConn);
try
{
myReader = cmd.ExecuteReader();
}
catch (SqlException ex2)
{
MessageBox.Show(ex2.Message, "Exception: Execute");
return;
}
while (myReader.Read())
{
count++;
}
myReader.Close();
oConn.Close();
MessageBox.Show("Got " + count.ToString() + " record(s)", "Success!");
Of course, replace the various components (DBServer, DB, youruser, yourpw and tb_table) with values that work in your environment. The success case should be a message box reporting the number of records in the table.
First, look at your "tok_vals" variable - or what you're assigning to it - use backslashes.
Your counter routine is a bit off. You will never get to count the occurrences of the last item in your phrase (assuming it's unique), since the inner loop will not even execute when the outer loop is positioned on the last item.
You may be better served by building an array of integers, used to store the count of each word, having the same dimension as your word list. As you read off each item from the phrase, search the word list for an entry. If you find it, add one to the corresponding location in the integer array, otherwise, add a new word entry, and set the corresponding entry in the integer array to 1. Then, just rip through the arrays at the end, printing the results.
You can add an event handler for the button by using the following syntax:
this.buttonDec.Click += new System.EventHandler(this.buttonDec_Click);
Then, define your handler function somewhere in your form code:
private void buttonDec_Click(object sender, EventArgs e)
{
...
}
However, since you are not wanting (I think!) to control the push button via arrays, you can safely drop a button on the form using the Designer and you should be OK, avoiding all the above stuff. You would only need an array of buttons if you plan on having a button for each set of TextBoxes.
Actually, you can use control arrays, but you will have to build the TextBoxes (or any other controls) manually, rather than using the designer (unless someone out there knows a way!).
Here's some code that will construct 6 TextBoxes, put them on the form and make sure they're visible:
At the top of your form class:
TextBox[] tb;
In your Form_Load():
tb = new TextBox[6];
for (int i = 0; i < 6; i++)
{
tb[i] = new TextBox();
tb[i].Visible = true;
tb[i].Top = 100 + (i * 50);
tb[i].Width = 200;
this.Controls.Add(tb[i]);
}
You can now interact with the TextBoxes in the array. This should get you started. Good luck!
If you don't mind including <arpa/inet.h>, you can use the htonl() function:
int newval = htonl(origval);
The "h" on the front is for "host" and the "n" is for network, "l" on the end for "long" (there is also a version for 16-bit integers). Hence, "Host to network Long". Network format is big endian.
On platforms that are already big endian (like Solaris), it's defined as a NULL macro.
Couple o' things:
1) Variables aren't initialized, then they are used (examined) later in the code. You will get unexpected results with this.
2) Your very first comparison ("if (WEIGHT <=100 && WEIGHT >= 200)") is always false. You can't have a weight that is less than 100 and greater than 200 a the same time. This is causing you to drop all the way to the end of your code.
3) In each of your blocks, you are checking the value of an uninitialized variable, before you check the weight of the person to see if they fit in the range.
4) The range checks don't have an "if" in front of them. Also, they have semicolons at the end - that will mess things up.
Try taking a look at those items and see how it goes.
Unless I misunderstand the function, "mouse_event" is used to generate mouse events. If you want to find the mouse's current position, you would need to trap the message as it arrives.
What does your main loop look like?
If you'll notice, you specify "this.Controls". Those controls contain all of the controls on your form, not just the TextBox-es. You need to check the type of each control as you iterate over it, to make sure it's a TextBox.
Something like:
foreach (Control c in this.Controls)
{
if (c.GetType().ToString() == "System.Windows.Forms.TextBox")
{
((TextBox)c).Text = "";
}
}
Just asking, but did you stop and restart Visual Studio after you did the install?
OK, perhaps you don't have the SQL Native Client installed. You can check the Control Panel to see if it is installed (once you open Control Panel, go to "Add or Remove Programs" or "Programs and Features" if you're in Vista).
If it is not, you can download it from here.
Once you have it installed, you should some additional choices for setting up your connection.
Take a look at line 63. There seems to be an extra character on the line ;)
GetASyncKeyState just tells you whether a key is down. You need to get the mouse coordinates, and determine if they fall within your rectangle. For that, you will need to trap one of the mouse messages, like WM_LBUTTONDOWN or WM_LBUTTONUP (both left-mouse-button messages). The lParam value of the message will indicate the X and Y positions of the mouse in the client area. See this link for some help.
You can select "SQL Server" to connect to a local or remote SQL Server instance. From the "Data" menu, select "Add New Data Source", select "Database", press "Next", then press "New Connection". In the "Choose Data Source" box that comes up, select "SQL Server". Once you do that, the combobox below will fill with 2 choices - the .NET provider for SQL Server (default) or the .NET provider for OLD DB. Select the .NET Provider for SQL Server, then press "Continue". Now, you can select the database you want to use, and it will build the DataSet and place it into your project.
The line you added to your initialization:
AppDomain.CurrentDomain.SetData("DataDirectory", "Path-to-Root-Folder");
the "Path-to-Root-Folder" value should be replaced with the actual path to where your project is residing. Say, for example, that is "C:\Projects\CardGame", the statement would look like:
AppDomain.CurrentDomain.SetData("DataDirectory", "C:\\Projects\\CardGame");
Again, you can remove this statement when you deploy, because one of the default locations the program will search for the database is in the same folder as the .EXE. Or, you can load the value for the path from your App.Config file.
You can do it by creating public properties or methods on your new form (in your example, Form2). You would then show the form afterward.
Form2 myForm = new Form2();
myForm.setFilename = sFile; // or, myForm.setFilename(sFile);
myForm.Show();
In the property/method, you could also call the "FromFile" method on the Image object to load the image data from the file and set it into the PictureBox on your form:
// In Form2
public void setFilename(string sFile)
{
MyPicBox.Image = Image.FromFile(sFile);
}
Of course, you would want to check (with exceptions) to ensure it found the file, etc.
Wow. I had the same thing happen to me - the "bin" folder version of the database vanished, and the only one left is the one in the "root" folder.
Not the greatest solution, but I added this line to my "Form1()" method (it would work in any other initialization location):
AppDomain.CurrentDomain.SetData("DataDirectory", "Path-to-Root-Folder");
The "Path-to-Root-Folder" value could be set up to be read from a config file at runtime, or the statement could be eliminated in the final version, since the "DataDirectory" portion of the connection string will check the .EXE location for the database by default.
I stopped/restarted the dev environment and ensured that it now works consistently.
I can't be 100% sure, as I'm not sure where you're getting your data, but you don't seem to be doing anything with the "paramCollection" data structure. Shouldn't that be set somewhere in one of the parameters that is going into the report?
OK, there's an issue with how VS2005 organizes the databases you add to a project. If you'll notice, there will be 2 databases in your project - one at the "root" level of your project, and another in the "bin" folder of your project.
In order to get my copy to work, I did this:
In the Solution Explorer, click once on the database, and look down in the "Properties" section, for the parm "CopyToOutputDirectory" and set it to "Do Not Copy". Then, in the Server Explorer, right-click on the database and select "Modify Connection...". Hit the "Browse..." button and select the copy of the DB in the "bin" folder. You may see that that version of the database already has your record in it.
Anyway, it's because there are 2 versions of your database, and the Server Explorer is looking at the "root" level, and your program is working with the one in the "bin" folder.
Let me know if that works for you. Here's a link to an article that helps.
Hint: the code you use to add a digit to an already-existing user has a problem - but I'll leave that for you to work on ;)
Ah, but you can! Using the Controls.Find() method, you can search for an instance of a control on a form. So, if you name your text boxes "txt001", "txt002", etc., you can run code like this:
Controls[] x = this.Controls.Find(sControlName, true);
to get an array of controls that match the string in "sControlName". Assuming only one control matches your string, code like this:
TextBox z = (TextBox)x[0];
will give you a variable, "z", that can be handled like any TextBox.
First, you should always wrap your database code in try/catch blocks. So many things can go wrong with database code, such as connection problems, column-width issues, format conversions, etc. Opening a connection and running a command/SQL against a connection should always be in a try/catch.
You have "creditvalue" defined as an INT in the code, but in your INSERT statement, you have it wrapped in single-quotes. How is it defined in your table?
Also, you close, then open the connection to the database, right before your INSERT - shouldn't cause a problem, but is unnecessary.
-Mike
Not much to go on here, but reaching for some things to check:
- Is the database (access?) installed on the new machine?
- If you are indeed using ODBC, have you put the necessary ODBC entries onto the new machine (via the Control Panel | Administrative Tools | Data Sources (ODBC))?
- Are there libraries you need to include with your application that are not on the new machine?
Some error messages, sample code, etc., would be helpful.
Any thoughts? Would a while work here with the for loops? Could I take advantage of the fact that encrypt1 & 2 will always be set at four characters?
You could, but shouldn't. If they throw a different length encrypt phrase at you, the code breaks.
You'll need 2 for loops (and another while loop) inside the main while loop. The first to check each char of the phrase to see if it is in the encrypt1 phrase. If so, the second for loop will check each "popped" char from the stack to see if it is in encrypt2 (there will be a while loop to read the chars from the stack):
while chars in phrase
for chars in encrypt1
is encrypt.char equal phrase.char
while chars in stack
for chars in encrypt2
is stack.char equal encrypt2 char
Obviously, you'll need to put in the checks to know when to put chars onto the stack and when to stop popping chars off the stack - exercise left to the requester! :)
In your "shuffle" function, try moving the random number seed function "srand" out of the loop. You're re-seeding the generator with the same number on each pass, since you're in and out of that routine within a second.
You may have a trailing empty line at the bottom of your input file. Make sure the last line of the file doesn't have a CR/LF on it, and that should take care of the issue.
You could also check to see if your Arabic number is zero, and if so, skip the conversion.
Generally speaking, if you're not sure of the contents of the file, or if it may contain undesired characters, you should consider reading a line of the file into a string variable or char array, and then cleaning it up (removing leading/trailing spaces, CR/LF, etc.). Then you can look at converting the string/char data to a form that your program can use.