Change line 4 to:
string str = "ABCD";
P1.Inverte(str);
Change line 4 to:
string str = "ABCD";
P1.Inverte(str);
Not a bad first effort but there are a few things: main()
always returns an int, and is not void.
You need to specify that you want &arayvalues[n]
on line #10.
Your second loop should be to 10 unless you want to add only the first 5 values together. Give yourself a small challenge and see if you can do it all in one loop.
Finally, just a stylistic thing but n = n+1
is usually abbreviated n++
(same for i obviously). If you want your code to be C89 compliant you should move line 12 up after line 6, at the top of the block with the others.
Here's an example of using ADO.NET (it's using VB.NET (viz AD's post) on an ASP.NET page but just ignore the ASP.NET part). You could use it with C++/CLI but that will require picking up some new syntax. If you have the fancier versions of VS (Professional or Team System or their equivalents in 2010) you can use VSTO to hook directly into Excel, but I don't know much more about it than that.
Hopefully that gives you some ideas.
Are you allocating memory within Vector3? It could be an issue with the copy constructor/operator= but that's just hazarding a guess.
Excel can generate plain text files in CSV (comma separated values) format. You can use tabs or spaces also (though you must choose one of the three I believe).
There are libraries out there that can give you more bells and whistles but you might not need that.
You have your break on line 22. Regardless of what happens with the choices you will always break out of the loop. So if the user chooses 1 or 2 you'll hit the break and exit out of the (outer) while loop.
My interpretation of what you are trying to do is that if the user selects 2 the loop will cycle back for more input. Therefore, if you move the break on 22 between lines 20 and 21, the code will then break out of the (outer) while loop (after lines 6-19 has taken place) and your function will return.
I think I might have been ambiguous in saying break before, I don't mean the inner while used to read in the file. For that one you can move what's in line 10 (minus the ; ) right into the loop condition instead of the .eof() call.
This is the word on .eof http://www.daniweb.com/forums/post155265-18.html
Move the break on 22 up into the if statement otherwise it will break regardless of good input or not.
Put in a cin.ignore()
before the cin if it's holding onto the newline character. Here's a good reference on cin.clear()
so you can see under the hood a bit (http://www.cplusplus.com/reference/iostream/ios/clear/).
AHA! I was missing a semicolon on a header file. Darn JAVA programming ;P
I don't quite understand what you mean by "What does setupPlayerInfo belong with". You mean what class? It is part of PlayerInfo.h/PlayerInfo.cpp
It didn't have a PlayerInfo::
in front of it, that's all.
I'll look into coding the do-while loop. Would it look something like this?:
Do (setupPlayerInfo()) {
// code here
cin.clear(); // needed for clearing the input to avoid an infinite loop?
} while (choice != 1)
It'd be within setupPlayerInfo, actually. cin.clear() is used when you've taken in bad input in the sense of looking to read into an integer and someone types characters. Wrap the loop around lines 9 through 46 and at the end of case one, set a flag to exit the loop.
bool breakflag = false;
do{
//get input here
if (choice == 1)
{
//all the stuff from case 1
break;
}
}while(true); //loop until break is hit
There will be some more rearranging necessary but that's the gist of it.
You've probably omitted a semicolon after one of your class declarations in the header file. See if that fixes both errors, otherwise you can post back with your new error message. What does setupPlayerInfo belong with?
Also lines 40 and 44 set up recursive calls to the function. This is not the best approach. Wrap your data entry portion in a do while loop.
There are other minor issues like with .eof() but let's get the heart of the problem figured out.
Let me clarify that I quoted the post for the sake of quoting the post, not as a reflection that the code was correct, but good catch NP-complete. However, try not to give the OP something they can compile and turn in for a grade.
:( i tried but i doesn't work
this is my code
int num,count = 1; cout<<"Please enter a number: "; cin >> num; while(count > num){ cout << " " << count; count = num + 2; } cout <<"end";
Evstevemd is right in that it shouldn't be count > num. When you have a loop statement like that, each time through, it's asking that mathematical statement like a question.
You have it as count > num so it is skipping over the loop as he said (as things in the parentheses that are false cause it to stop).
You want to ask "is count less than num?" ( count < num
) if that's true, then that's the signal to keep going. Once you've reached the end of the range and it's false, the loop will stop.
change this line
int num,count = 1;
to
int num; int count = 1;
They are identical.
umm...couple of ways, first is the easiest.
//option # 2 string path = "AMBER/data/runx/amhsk_run*.root"; string runNumber; cin >> runNumber; path.replace( path.find('*'),1,runNumber) ;
I think OP wants to replace both x'es. It doesn't change your code much but you'd need another find statement (and you'd have to adjust your replace call if runNumber was multiple digits, unless I misinterpreted it).
I would imagine it would be TFile y(ss.str());
. If TFile takes a null terminated string then use ss.str().c_str()
Where is getData()
defined (or declared for that matter). There may be a mismatch somewhere.
Why do we have these unresolved externals?
Would you rather your program run without the appropriate functions? ;)
Use a stringstream:
#include<sstream>
//...
stringstream ss;
string first = "AMBER/data/run";
string middle = "/amhsk_run";
string last = ".root";
ss<<first<<x<<middle<<x<<last;
Use the ss.str()
to get the string back.
Try putting it in a winforms project by itself as the code for a button click handler and see if it works. Otherwise there could be something in your code that's preventing it (unless for some reason it won't let you go to 0 due to window thickness but that's just idle speculation).
I put the code you posted into a button click method and when I clicked it, the window went to the lower right hand corner.
I try to do This->Top = 0; no avail.
What happens when you do that?
What's the error message that you are getting?
Here's the correct code for the 2nd program
int num; cout<<"Please enter a number: "; cin>>num; for(int i=1;i<num;i=i+2) { cout <<" "<< num; } cout <<"end"; return 0; }
Please don't give away the answer if the OP is working towards it. Your code is also incorrect. Test it out.
And to tackle the problem of getting the string size, you could get the string class to return the character array of the string, and then use strlen to find the length of the string
Or use the .length()
member of your string.
Unmanaged arrays can't hold managed objects (and vice versa) due to garbage collection issues. If you are going to exchange data between the two it has to be marshaled.
Your best bet is to have a class member that's a cli::array and initialize it in the constructor of your ref class. You can pass the number of objects in the array,obtained from wherever, into the constructor.
Try opening file like this:
fstream outStream("test.txt", ios::in|ios::out)
That gives better numbers from the seekg function but I think the OP needs to have the append setting on. I suppose the position could be reset after the read is made.
This fares no better in returning the proper string using his original -11.
I think I'll bow out of this one.
Yes, you are declaring the labels array locally within the scope of the constructor. Place the array<Label ^>^ labels;
with the other private variables of the form, that way it's local to the whole class.
I didn't understand that was a missing piece I was focusing on the logic of your method.
I've done some experiments with tellg to get the file position after the seekg and it keeps coming back with -1 all of the time. I recalled something about seekg being problematic with text files. I found http://msdn.microsoft.com/en-us/library/y2d6fx99.aspx (scroll down to the yellow box) saying that the offset/seekdirection form shouldn't be used with text files. I'm no expert in this area so I couldn't give you the final work on how true that is.
I would say (and this definitely is not efficient) but open the text file with ios::in, read line by line until the last entry,do your checks,close the file and open it back up again for output (and with append).
i have already opened the file with ios::app.So the get and set pointer is already at the end of the file.
Didn't put 2 and 2 together, apologies
*I want to open that file in append mode and then i want to print the last line of that file.
I understand the immediate task at hand. I'm trying to get a handle on the big picture. I meant that I don't understand the constraints that are preventing you from opening the file without the append and seek back from the end (since maybe there is another way).
I'm not entirely clear on what you need done. If you want the last line you should probably use ios::end
instead of ios::cur
FunctionX (http://www.functionx.com/vccli/index.htm) has some decent tutorials for learning the C++/CLI language (the dialect of C++ used with the .NET library) but their winforms tutorials lag behind a version or two.
It seems to be far easier to find C# references for this. I looked for a bit and couldn't find the walkthroughs on MSDN for VC++ or VC# but they usually have videos to show you a basic winforms program. If you can figure it out using C# it doesn't take too much effort to translate it back to C++/CLI.
Play around with it on your own and pop textbox and the button up on the form. Double-click on the button to generate an event handler. That code will execute every time you hit the button. Access the text portion of your textbox via YourTextBox::Text
and set it equal to "Hello World." There are some nuances with the strings (look up "handles" to get some idea of the "^" operator) but you shouldn't have to worry about those to start.
It helps to use 2008 over 2010 because they totally scrapped the Intellisense for C++/CLI in 2010 (at least in the express edition but I'm fairly certain in the full versions as well).
I wish I could have given you a better tutorial to follow but post back if you have any difficulties.
EDIT: LOL at AD ^^^^^^^ but there aren't many good ones for the winforms in C++.
What do you mean by register?
How is it declared?
What's not transferring over from the C# properly? In doing this I realize it would be nice for C++/CLI to have an "is" keyword.
I still think it might be easier to do it label by label (you would only need one event handler). If you have the Tag free (it looked like you were using it for something before) you could tag the label with its position in the array and use that to compare to the sender. That way you wouldn't need the position information.
I think it could work your way I'm just not coming up with it, though.
Where is itemCurrent coming from in your code above? Seems to me you could match your sender to the desired control and run another static method when that is true.
All there in lines 2-5 above. The ith element of the vector is accessed via vec[i]
assuming your vector is named vec (so if you have declared vector vec<int>;
earlier.
To get the Y value, int y = e->Location.Y;
Do you need to know the location and change the label simultaneously? I think it's much easier to handle the color change with a mouse_enter
and mouse_leave
event of the label rather than the form. However, in doing so the eventargs of the mouse_enter does not have position information any longer.
Sure. Here's an example in a for loop (vec.size() returns an element of size_t, which is usually defined as an unsigned int, so we'll make the loop variable size_t as well).
So you've done your while loop and vec has N elements. vectors store this value within the data structure.
int sum = 0; //again change for double or whatever your values are
for (size_t i = 0;i<vec.size();i++) //vec.size() accesses that property = N
{
sum+=vec[i];
}
You could use an iterator with the vector also but this is less involved.
Use vec.size()
to get the number of entries. Your vec is a vector<int>
so your storage is already taken care of (if you're doing doubles or something else just change the type of vector).
Store them in a std::vector and use a while loop to get the input:
int temp;
while(cin >> temp)
{
vec.push_back(temp);
}
and have the user push ctrl-z (or ctrl-d on *nix) when they are done.
There are a few options and probably some others that I have forgotten about.
The easiest is to keep the program like you have it (although you have a mismatch in the if/else pair which needs to be fixed) and after you've stepped through the char array with the while loop, compare x to 10, if the 2 are equal it's a valid number. Check the condition of your if statement because it's incorrect, you want numbers that sit in between 46 and 58 rather than anything >46 OR <58.
Another option is to change number to an integer and use the fail bit of the input stream (see second code sample on this site http://www.cs.utk.edu/cs102/lectures/cinLecture.html). If any alpha characters are put in the stream will fail and you can check for that and loop back for more input.
There's also strtol (include <cstdlib>) which can be used on your char array input and the function will return a value if there's a successful conversion. See http://www.cplusplus.com/reference/clibrary/cstdlib/strtol/
Rather than use itoa which is non-standard try nbaztec's stringstream method:
(portions omitted)
#include <sstream>
//...
std::stringstream ss;
for
{
for
{
for
{
ss.str("");
ss<<i<<","<<j<<","<<k;
std::cout<<ss.str()<<std::endl;
}
}
}
f1.eof()
is a method and needs the parentheses. I know with text files that using .eof() can read the last line of the file twice, but I'm not sure how it behaves with binary.
You're missing a parens on 196 also.
Look at line 221 again, check() would have to be a static method for that to work.
The delimiter needs to be a character not a string, so try putting ':'
instead of ":"
on line 36.
It doesn't seem as if you've included <iostream>
for cin and cout. Even after including that you will also need to qualify the names. You can do this one of three ways.
First, qualify as std::cin
and std::cout
at each use of cin/cout, or alternatively use the statement using namespace std;
at the top of your code, or lastly (as a compromise) you could put using std::cout;
and using std::cin;
at the top just to qualify those two names only.
Also, these are a bad idea:
gets() //will not limit the number of characters you can enter, you can go way over the end of your char arrays into other memory
fflush(stdin); //undefined behavior, meant to be used with stdout
goto //reform your code into loops if you want this behavior
Reread Banfa's post as he has given you the answer. Specifying map[1][1]
gives you a pointer to the row on line 28 in the code above, so it is of type int *
. You need the third dimension in there for "map" to resolve down to an int.
Have the constructor in your serial port class take in a TextBox (and have a private TextBox field to hold the object). When you instantiate your serial port class in Form1, pass in the TextBox from your form.
This sounds like a situation that calls for a backgroundworker object http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx. That being said, part of the problem you were having above is that you need to pass in a System::Windows::Forms::Form
instead of a Form1.
Myself, it seems much more feasible that he would want you to check if it were empty. Then you return -1 and the output of the numbers stops in printQueue. Based on the instructors own driver code returning a -1 stops the output process dead in its tracks, so why should the program not output a full queue? I could be missing something but that seems fishy to me.
I modified the dequeue method to this (in psuedocode so I don't take away your fun)
IF QUEUE IS NOT EMPTY
{
ELEMENT = QUEUE[0]
SHIFT ELEMENTS DOWN
DECREMENT LENGTH
}
RETURN ELEMENT;
You'll need to do the initialization of element and other housekeeping too
My output:
Queue properties:
isEmpty = 1
length = 0
size = 10
first element = -1
Queue properties:
isEmpty = 0
length = 5
size = 10
first element = 10
Queue elements:
10
13
32
3
16
Queue properties:
isEmpty = 1
length = 0
size = 10
first element = -1
Queue elements:
The queue is empty!
Queue properties:
isEmpty = 0
length = 10
size = 10
first element = 0
Queue elements:
0
10
20
30
40
50
60
70
80
90
The extraneous -1 from the listing ending in 16 has also disappeared.
Read over this portion again. Your if statement in the block is not doing any of these steps.
// TODO:
// This method removes and returns the first element
// in the queue. You should first check that the
// queue is empty. Adjust the length of the queue
// after dequeuing the element.
//
How do you check if the queue is empty? Use the isEmpty() method. Only if your queue is empty return -1 so that your printQueue function doesn't print anything.
I was asking you to trace through the printQueue so you could see that your dequeue method is in error. There's no need to change the driver. The if condition within the dequeue method will always be true for length the same as size so any queue of length 10 will return a -1 regardless of how many members are in it.
Add in a set of braces between line 71 and 72 and between 73 and 74. This isn't tha main problem but your enqueue was happening regardless of the length.
Trace through your portion that prints out the queue in the driver. After the queue is cleared and the 10 elements are added, you have size = 10 and length = 10. Therefore, length+1 >= size
and so dequeue returns -1. In your printQueue function this is taken as a signal to break.
Try making the changes around that and post back if something else arises.