So our last project (holy shit, this is my last week, and i'm rocking a pitiful 75%, hopefully i can boost it with the last exam, and the final. Can't wait to be done.)
Anyhow, we're making a program that takes user input, and receives their name, hours worked per week, and hourly wage, to calculate their net pay, after taking out their FICA, Federal, and State taxes.
Anyhow, he wants us to throw in some exclusions, so he wants it to make someone pick a number above 5.50, and below 200 for their hourly rate. He also wants us to have a prompt if they pick a number above 60 for weekly hours worked. So, i was thinking i would make a loop to ask a yes or no question if they entered a number greater than 60.
I made strings for answer, yes, and no, and then i was thinking something like this for the actual code:
[code]
cout << "Hours worked: ";
cin >> GetHrs;
if(GetHrs > 60){
cout << GetHrs << " is above 60. User override needed to continue. Enter 'yes' or 'no'.";
cin >> answer
if(answer == yes){
cout << "User override of " << GetHrs << " confirmed. ";
}
else{
cout << "Enter a new number for hours worked: ";
cin >> GetHrs;
}
else
{} //continue onto rest of program
} //closes if/else statement
[/code]
Would something like that work? I thought using strings would allow me to use yes and no as answers, and i think i just need it to run the if part only when it sees the number for GetHrs is greater than 60. Otherwise it can just continue on as if nothing happened.
Thanks everyone. You all are likely the greatest contributor to my (hopefully) passing. One exam left tomorrow, then final next Tuesday.
If a user enters more than 60, chooses no and enters more than 60 again, he won't need an override anymore.
You should put it in a loop, like
[code]cout << "Hours worked: " << flush; //the flush is important!
while(true)
{
//... get input
if(input_valid)
{
break;
}
//... ask for override
if(override)
{
break;
}
//... ask again (only ask, the loop-body will get input again)
}[/code]
Whenever you don't flush a stream with std::endl, you need std::flush for the buffer to be guaranteed to be shown.
[QUOTE=LuaChobo;43060914]So apparently I didn't have the SDK on this computer, so trying to install it I keep getting Fatal Errors
[img]http://puu.sh/5Bjw2.png[/img]
All it says on the internet is to try the web installer version, So i did that, same shit.
fucks sake god dammit[/QUOTE]
Check the event logs for a more detailed error description/code and google that.
[QUOTE=ZeekyHBomb;43087862]If a user enters more than 60, chooses no and enters more than 60 again, he won't need an override anymore.
You should put it in a loop, like
[code]cout << "Hours worked: " << flush; //the flush is important!
while(true)
{
//... get input
if(input_valid)
{
break;
}
//... ask for override
if(override)
{
break;
}
//... ask again (only ask, the loop-body will get input again)
}[/code]
Whenever you don't flush a stream with std::endl, you need std::flush for the buffer to be guaranteed to be shown.[/QUOTE]
Awesome. Thanks.
I'll give this a shot between study sections. Thanks as always!
just to make sure I've got the idea down, for the override would i want to start with something like if(GetHrs <= 60) then move down past the break and at the override have something like
[code]
cout << "Confirm you would like to enter a value of more than 60 for hours work. Enter 'yes' or 'no'." << endl;
cin >> answer
if(answer = yes)
{
break;
}
[/code]
[QUOTE=JohnStamosFan;43092748]Awesome. Thanks.
I'll give this a shot between study sections. Thanks as always!
just to make sure I've got the idea down, for the override would i want to start with something like if(GetHrs <= 60) then move down past the break and at the override have something like
[code]
cout << "Confirm you would like to enter a value of more than 60 for hours work. Enter 'yes' or 'no'." << endl;
cin >> answer
if(answer = yes)
{
break;
}
[/code][/QUOTE]
Your if statement should be more like this:
[code]if(answer == "yes")[/code]
[QUOTE=Chris220;43093186]Your if statement should be more like this:
[code]if(answer == "yes")[/code][/QUOTE]
I always forget to do that goddamned double = sign. Thanks!
If I wanted to store data in an array of objects in c#, how would I do that?
Ex:
[CODE]
NPC[1].Xpos = 50;
NPC[1].Ypos = 30;
NPC[2].Xpos = 50;
[/CODE]
Could anyone just point me in the right direction or anything? I've looked into DataTables and Classes but they either don't work as I would like, or I'm not doing it right.
Depends on what you want to do, i don't really understand the question, but i think you mean this.
[code]
// Some class variable example
SomeClass SC = new SomeClass();
SC.SomeClassField = Value;
...
// Initialize array of SomeClass with length ArrayLength
SomeClass[] SomeClassArray = new SomeClass[ArrayLength];
// Initialize and set field of SomeClass at index Index, arrays are zero based so index can
// be from 0 to ArrayLength - 1, everything else and you get exception
SomeClassArray[Index] = new SomeClass();
SomeClassArray[Index].SomeClassField = Value;
...
for (int i = 0; i < SomeClassArray.Length; i++) {
SomeClassArray[i] = new SomeClass();
// Initialize every SomeClass in SomeClassArray
}
[/code]
[editline]7th December 2013[/editline]
[code]
// This will create SomeClassArray with length 3
SomeClass SC = new SomeClass();
SC.SomeClassField = Value;
SomeClass[] SomeClassArray = new SomeClass[]{ new SomeClass(), SC, new SomeClass() };
// To get variable SC from SomeClassArray you do
SomeClass SC2 = SomeClassArray[1];
[/code]
[QUOTE=cartman300;43093994]Depends on what you want to do, i don't really understand the question, but i think you mean this.
[code]
// Some class variable example
SomeClass SC = new SomeClass();
SC.SomeClassField = Value;
...
// Initialize array of SomeClass with length ArrayLength
SomeClass[] SomeClassArray = new SomeClass[ArrayLength];
// Initialize and set field of SomeClass at index Index, arrays are zero based so index can
// be from 0 to ArrayLength - 1, everything else and you get exception
SomeClassArray[Index] = new SomeClass();
SomeClassArray[Index].SomeClassField = Value;
...
for (int i = 0; i < SomeClassArray.Length; i++) {
SomeClassArray[i] = new SomeClass();
// Initialize every SomeClass in SomeClassArray
}
[/code]
[editline]7th December 2013[/editline]
[code]
// This will create SomeClassArray with length 3
SomeClass SC = new SomeClass();
SC.SomeClassField = Value;
SomeClass[] SomeClassArray = new SomeClass[]{ new SomeClass(), SC, new SomeClass() };
// To get variable SC from SomeClassArray you do
SomeClass SC2 = SomeClassArray[1];
[/code][/QUOTE]
I think that'll do the trick, thanks!
[QUOTE=Tamschi;43083948]In the Logic class you could do [code]public event Action Updated;
protected void OnUpdated()
{
if (Updated != null) Updated();
}[/code]
Then you can use it like this in the form:
[code]_logic = new Logic();
_logic.Updated += Update; // Update is a void() method accessing _logic.[/code]
[/QUOTE]
Thanks! I think I'll stick with this, as I still don't understand what lambda or delegate syntax means.
[editline]7th December 2013[/editline]
Actually I still don't understand your example.
Can you provide some simple method in the logic class???
I don't understand the connection between the call in the form and the void method in the logic class.
Does anyone have any good resources for a rather experienced Lua coder moving into C#?
My question isn't about any actual code or a programming problem I'm having, but it's still sort of related.
I'm learning programming in school and it's going fairly well, but I'd like to do things in my free time too. I've mainly been using C++, but I've tried to dabble in Python too. The problem is I have no idea what to do. I'm looking for simple (beginner-level) projects to do in both C++ and Python. Everything I find online is way above my skill level (Project Euler most of all, I'm not that interested in complex algorithms or high-level math in general).
Does anyone have any tips/suggestions?
I have a problem with OpenAL (lwjgl).
I use this to update the listener location:
[CODE]
public static void update(Vector3f listenerPos){
AL10.alListener3f(AL10.AL_POSITION, listenerPos.x, listenerPos.y, listenerPos.z);
}
[/CODE]
If I update the listener position in a loop with update(new Vector3f(0,0,++i)); everything works as expected. That is, the sound sounds like it's going farther away.
But if I move the source position instead, the sounds doesn't move.
[CODE]
public static void playASound(int sound, Vector3f position) {
AL10.alSource3f(source.get(sound), AL10.AL_POSITION, position.x, position.y, position.z);
AL10.alSourcePlay(source.get(sound));
}
[/CODE]
What is going on? I'm probably missing something obvoius, aren't I?
[QUOTE=war_man333;43098654]Thanks! I think I'll stick with this, as I still don't understand what lambda or delegate syntax means.
[editline]7th December 2013[/editline]
Actually I still don't understand your example.
Can you provide some simple method in the logic class???
I don't understand the connection between the call in the form and the void method in the logic class.[/QUOTE]
The event holds a reference back to the form. (It's implicit, adding an instance method, or a lambda depending on instance fields, creates a delegate (an object containing a method reference and optionally a target) that has the current instance ([I]this[/I] at the location creating the delegate) as target.)
This means that you can call [I]OnUpdate()[/I] in Logic after making changes and [I]Update()[/I] will run in the form instance.
Events also keep their targets alive, so as long as logic isn't collected form1 also isn't. This won't stop them from being collected at the same time though, even if the form holds a reference to a logic instance.
All this is only for single-threaded programs though, if you have multiple threads you need to give the callback (what is run because of the event) to the Form's dispatcher first or any UI access will result in an exception.
Is such magic actually used anywhere?
[code]
local tab = {}
setmetatable( tab, { __call = function( self, ... ) print( tostring(self) .. " ::", unpack( {...} ) )end } )
print( tab )
tab( "Lua" )
tab( "table", "calling" )
[/code]
[code]
Process started >>>
table: 003E9090
table: 003E9090 :: Lua
table: 003E9090 :: table calling
<<< Process finished. (Exit code 0)
[/code]
Anyone here familiar with polymorphism in C++?
For example if I wanted to make a scene class that only handles polymorphed classes that inherit from both sf::Drawable AND as::Tickable, how would I access both draw() and tick() functions from each class I add to it?
Like this:
[cpp]
class AnimatedSprite : public sf::Drawable, public as::Tickable {
draw();
tick();
}
class Light : public sf::Drawable, public as::Tickable {
draw();
tick();
}
scene->add( new AnimatedSprite() );
scene->add( new Light() );
scene->tick();
scene->draw();
[/cpp]
Do I just need to make a new class called Node that inherits from both, and then make AnimatedSprite and Light inherit from node instead of their more obvious classes?
If you're going to make a lot of classes that use both sf::Drawable and as::Tickable, why not make an in-between class that inherits both of those, and have your other classes inherit from that single class? You say you want to call this single class Node? Surely there's some significance in that name, as it specifies what having these two classes makes something, a node. That should definitely be its own type. Not only does having a Node class allow you to call both sf::Drawable's and as::Tickable's functions from the same type, but also allows you to add other functions to Node if you need to.
Man, I ain't used C++ in a while. Multiple inheritance looks weird as hell.
Haha yeah you're right. That's the solution I was thinking about too, but while I walking to my friends house at night in about a foot of snow. I realized a better solution to my problem.
Scene will take and store void*'s, and when it needs to tick or draw an object: it just casts it to a drawable or a tickable and runs the function.
I just feel like creating a node class is counter-intuitive to how sfml is designed is all, since it collapses a bunch of the sfml objects together into an ambiguous class.
How do you parse several lines in a single text file in C++? I have absolutely no idea how to do this.....
[QUOTE=SteelSliver;43109528]How do you parse several lines in a single text file in C++? I have absolutely no idea how to do this.....[/QUOTE]
[cpp]std::ifstream file(name);
std::string text;
while(!file.eof()){
std::getline(file, text);
std::cout<<text<<std::endl;
}[/cpp]
[QUOTE=Naelstrom;43109509]Haha yeah you're right. That's the solution I was thinking about too, but while I walking to my friends house at night in about a foot of snow. I realized a better solution to my problem.
Scene will take and store void*'s, and when it needs to tick or draw an object: it just casts it to a drawable or a tickable and runs the function.
I just feel like creating a node class is counter-intuitive to how sfml is designed is all, since it collapses a bunch of the sfml objects together into an ambiguous class.[/QUOTE]
Can't say I'm a fan of using void pointers and casts, a very C approach, when classes are right there, available for you in C++. You may somewhat improve readability if the cast and the function are there on the same line, but your solution is so convoluted that someone like me reading through your code would be wondering why you did what you did. Most people use IDE's too. If someone wanted to know what Node inherited from, they could just click on Node and go to the class definition and find out that it uses SFML classes. One of the things I like about SFML is that it keeps C++ in mind, instead of a commonly used alternative, SDL, which uses C. When you use a C solution to a C++ problem, you're actually somewhat defeating the purpose of using SFML.
Unrelated, but I'm jealous of that snow you got. I got the same storm but all we got from it here was rain.
[QUOTE=WTF Nuke;43109565][cpp]std::ifstream file(name);
std::string text;
while(!file.eof()){
std::getline(file, text);
std::cout<<text<<std::endl;
}[/cpp][/QUOTE]
Thanks for that, although that's the code I already have. Stupidly enough, I didn't realize it read all the lines of the text file one by one. Maybe I should have looked a little further before asking.....
[QUOTE=Tamschi;43104505]The event holds a reference back to the form. (It's implicit, adding an instance method, or a lambda depending on instance fields, creates a delegate (an object containing a method reference and optionally a target) that has the current instance ([I]this[/I] at the location creating the delegate) as target.)
This means that you can call [I]OnUpdate()[/I] in Logic after making changes and [I]Update()[/I] will run in the form instance.
Events also keep their targets alive, so as long as logic isn't collected form1 also isn't. This won't stop them from being collected at the same time though, even if the form holds a reference to a logic instance.
All this is only for single-threaded programs though, if you have multiple threads you need to give the callback (what is run because of the event) to the Form's dispatcher first or any UI access will result in an exception.[/QUOTE]
Where exactly in the form would you add
[code]lInst.Updated += Update;[/code]
I still don't see how the event being changed in the logic class is supposed to activate something in the form. It's like they're not connected in my program.
I made a breakpoint in the logic class, and yes indeed it runs OnUpdate() every 5 seconds but [b]how is this supposed to communicate with the form? [/b] Nothing is happening right now.
Sorry for being dense but I'm a lot better with examples, not elaborated theory.
[QUOTE=AlphaGunman;43101575]My question isn't about any actual code or a programming problem I'm having, but it's still sort of related.
I'm learning programming in school and it's going fairly well, but I'd like to do things in my free time too. I've mainly been using C++, but I've tried to dabble in Python too. The problem is I have no idea what to do. I'm looking for simple (beginner-level) projects to do in both C++ and Python. Everything I find online is way above my skill level (Project Euler most of all, I'm not that interested in complex algorithms or high-level math in general).
Does anyone have any tips/suggestions?[/QUOTE]
I haven't tried this myself, but perhaps [url=https://github.com/karan/Projects]this list[/url] contains some cool problems.
[QUOTE=aurum481;43106070]Is such magic actually used anywhere?
[code]
local tab = {}
setmetatable( tab, { __call = function( self, ... ) print( tostring(self) .. " ::", unpack( {...} ) )end } )
print( tab )
tab( "Lua" )
tab( "table", "calling" )
[/code]
[code]
Process started >>>
table: 003E9090
table: 003E9090 :: Lua
table: 003E9090 :: table calling
<<< Process finished. (Exit code 0)
[/code][/QUOTE]
You mean the __call metafunction? I've seen it used, for example as a 'constructor'.
[QUOTE=elevate;43109582]Can't say I'm a fan of using void pointers and casts, a very C approach, when classes are right there, available for you in C++. You may somewhat improve readability if the cast and the function are there on the same line, but your solution is so convoluted that someone like me reading through your code would be wondering why you did what you did. Most people use IDE's too. If someone wanted to know what Node inherited from, they could just click on Node and go to the class definition and find out that it uses SFML classes. One of the things I like about SFML is that it keeps C++ in mind, instead of a commonly used alternative, SDL, which uses C. When you use a C solution to a C++ problem, you're actually somewhat defeating the purpose of using SFML.
Unrelated, but I'm jealous of that snow you got. I got the same storm but all we got from it here was rain.[/QUOTE]
I think I'm having trouble following what you say because I also have to worry about the default sfml classes like sf::Text and sf::VertexArray, which I don't want to create wrapper classes for, but I still want to be able to add to the scene. Using a void* pointer gives me the flexibility I need in order to handle this properly. And it makes sense for the scene to contain a multitude of different classes.
So what I'm going to do is have the scene take void*'s and a boolean. The boolean indicates if it needs to be ticked or not. The scene will keep track of what needs to be drawn and ticked and will do so properly when asked.
You're right about it not being the most elegant solution, but it does take a huge workload off of me because I don't need to make all those wrapper classes and I'm a lazy asshole.
[editline]asdf[/editline]
Oh and thanks, I really appreciate discussing with someone a solution to the problem as it helps me more fully understand my decisions.
And about the snow, its pretty, but it really sucks when you actually want to go somewhere or do something. 4 wheel drive and snow boots can't save you here.
[QUOTE=Naelstrom;43113303]I think I'm having trouble following what you say because I also have to worry about the default sfml classes like sf::Text and sf::VertexArray, which I don't want to create wrapper classes for, but I still want to be able to add to the scene. Using a void* pointer gives me the flexibility I need in order to handle this properly. And it makes sense for the scene to contain a multitude of different classes.
So what I'm going to do is have the scene take void*'s and a boolean. The boolean indicates if it needs to be ticked or not. The scene will keep track of what needs to be drawn and ticked and will do so properly when asked.
You're right about it not being the most elegant solution, but it does take a huge workload off of me because I don't need to make all those wrapper classes and I'm a lazy asshole.
[editline]asdf[/editline]
Oh and thanks, I really appreciate discussing with someone a solution to the problem as it helps me more fully understand my decisions.
And about the snow, its pretty, but it really sucks when you actually want to go somewhere or do something. 4 wheel drive and snow boots can't save you here.[/QUOTE]
Is `scene` a managed type? If it is, why not overload `add()` to accept any of `sf::Drawable`, `as::Tickable` or `Node`, and just store them in three separate data structures. This may become an issue if you need the order of addition to be respected in the respective methods (that is, if you first add a Tickable, then add a Node, then another Tickable, you might [I]expect[/I] them to be drawn in that order, but if they are in separate containers they might not be.) If this [I]isn't[/I] a problem, then I think this is a cleaner solution than void pointers, which should be avoided like the plague in C++ code.
Here's what I mean:
[code]
class Node : public sf::Drawable, public sf::Tickable {
...
};
class AnimatedSprite : public Node {
draw();
tick();
}
class Light : public Node {
draw();
tick();
}
class Scene {
vector<Node*> nodes;
vector<sf::Drawable*> drawables;
vector<as::Tickable*> tickables;
add(sf::Node *node) {
nodes.push_back(node);
}
add(sf::Drawable *drawable) {
drawables.push_back(drawable);
}
add(sf::Tickable *tickable) {
tickables.push_back(tickable);
}
tick() {
for (auto tickable : tickables) {
tickable->tick();
}
for (auto node : nodes) {
node->tick();
}
}
draw() {
for (auto drawable : drawables) {
drawable->draw();
}
for (auto node : nodes) {
node->draw();
}
}
};
// This will be tick'd and drawn
scene->add( new AnimatedSprite() );
// This will be tick'd and drawn
scene->add( new Light() );
// This will only be drawn
scene->add( new sf::Text() );
// WARNING: The order of addition is irrelevant here
scene->tick();
scene->draw();
[/code]
(Take this as pseudocode, because bare pointers in containers is probably not the best)
The boolean flag you were planning on passing to the scene is now unneeded, because the scene will only draw an object if it is drawable and tick an object when it is tickable.
Oh right duh, that's perfect. Thanks Rayjingstorm.
[editline]asf[/editline]
[url=http://farmpolice.com/content/videos/0d04cf4e.webm]It works great.[/url]
[cpp]-- Intro state
function STATE:onInit()
print( "State init!")
self.sprite = AnimatedSprite( "t_foob" )
self.sprite:setPos( Vector( 100, 100 ) )
end
function STATE:onExit()
print( "State exit!")
self.sprite:remove()
end
function STATE:onTick( dt )
print( "State ticking by ", dt:asSeconds() )
Scene.tick( dt )
end[/cpp]
Alright, i got this typed in, but it's not working as expected, and i imagine that's because i've got some important stuff out of place.
[code]
cout << "Hours worked: " << flush;
while(true)
{
cin >> GetHrs;
if(GetHrs <= 60)
{
break;
}
cout << "Hours worker is greater than 60." << endl << "Need user override. Type 'yes' or 'no'. " << endl;
cin >> answer;
if( answer == no)
{
break;
}
//... ask again (only ask, the loop-body will get input again)
cout << "Hours worked: ";
}
[/code]
Did i enter that right? Or did i completely misunderstand the comment prompts?
I think you want if(answer == yes), so it breaks out of the loop when the user typed yes.
Also, if the first message ("Hours worked: ") is the same as the message asking for input again, you can just move a single cout (instead of two, doing the same) to the top of the loop-body.
[QUOTE=war_man333;43110471]Where exactly in the form would you add
[code]lInst.Updated += Update;[/code]
I still don't see how the event being changed in the logic class is supposed to activate something in the form. It's like they're not connected in my program.
I made a breakpoint in the logic class, and yes indeed it runs OnUpdate() every 5 seconds but [b]how is this supposed to communicate with the form? [/b] Nothing is happening right now.
Sorry for being dense but I'm a lot better with examples, not elaborated theory.[/QUOTE]
You put that line into the form, after you create the logic instance or get a reference to it or whatever (only run it once near the start). This line gives a reference to the form to [I]lInst[/I], the process is called subscribing to the event.
If you set a breakpoint in the forms [I]Update[/I] method or step into [I]Updated();[/I], you will see that OnUpdate calls this method [U]through the event's delegate field[/U] (part of what the event is a shorthand for).
Where do the 5 second intervals originate from currently? It would help if you posted more of your program. (I should be able to give you a good example if you post your logic class.)
If [I]lInst[/I] lives longer than the form, make sure to remove the subscription with -= when the latter closes or it will linger in memory and continue to receive notifications.
Sorry, you need to Log In to post a reply to this thread.