By using this site, you agree to our Privacy Policy and our Terms of Use. Close

Forums - General - c++ question

Ugggh... seeing this made me cringe from my college days and all the programming classes I took. Most of what I did for programming was done in Visual Studios in VB, but mostly C#. I had to do a assignment like that and it was fairly simple to do. Just get the structure and right formula's in there and you'll be fine.

Then again I have my BS in CIS and graduated with a 3.6 GPA. I don't mind the C programming, it was the Oracle PL/SQL that I really hated. Couldn't even use a GUI to do that, I had to write them all out in notepad and then run it that way.



 


Get your Portable ID!

 

My pokemon brings all the nerds to the yard. And they're like, "You wanna trade cards?" Damn right, I wanna trade cards. I'll trade this, but not my charizard.

Around the Network

TheSource, I will send you the code later on... I'm at work right now but this is easy sh*t for me. I might even have some time to do this during "work hours" ;)



PSN ID: krik

Optimistic predictions for 2008 (Feb 5 2008): Wii = 20M, PS3 = 14M, X360 = 9.5M

 

Just a word of advice:

make sure to change any code sent to you to fit the coding style you use. Make sure your variable naming conventions are followed and comments are similar to those you regularly use.

As posted above this is pretty straight forward, the "toughest" part, to understand atleast, is the operator overloading, but once you understand how it works for one, you should be able to understand how it works for all of them.



I am a Gauntlet Adventurer.

I strive to improve my living conditions by hoarding gold, food, and sometimes keys and potions. I love adventure, fighting, and particularly winning - especially when there's a prize at stake. I occasionally get lost inside buildings and can't find the exit. I need food badly. What Video Game Character Are You?

Mega Man 9 Challenges: 74%

Waltz Tango Jitterbug Bust a move Headbanging
Bunny Hop Mr. Trigger Happy Double Trouble Mr. Perfect Invincible
Almost Invincible No Coffee Break Air Shoes Mega Diet Encore
Peacekeeper Conservationist Farewell To Arms Gamer's Day Daily Dose
Whomp Wiley! Truly Addicted! Truly Hardcore! Conqueror Vanquisher
Destroyer World Warrior Trusty Sidearm Pack Rat Valued Customer
Shop A Holic Last Man Standing Survivor Hard Rock Heavy Metal
Speed Metal Fantastic 9 Fully Unloaded Blue Bomber Eco Fighter
Marathon Fight Quick Draw G Quick Draw C Quick Draw S Quick Draw H
Quick Draw J Quick Draw P Quick Draw T Quick Draw M Quick Draw X

After that, read and understand the code. Otherwise you might get tricky follow-up questions...



Here's a couple of links that might help:

General operator overloading:
http://www.codersource.net/cpp_overloading.html

Stream operator overloading:
http://www.codersource.net/cpp_stream_operators.html

BTW, I hate cygwin. I just make my machine dual boot with windows/linux. I only use windows when I have too. It pretty easy to install either red hat or suse onto a windows box. I'd recommend doing that instead of using cygwin.



Around the Network
whatever said:
. . .
BTW, I hate cygwin. I just make my machine dual boot with windows/linux. I only use windows when I have too. It pretty easy to install either red hat or suse onto a windows box. I'd recommend doing that instead of using cygwin.

Another option, assuming you have at least 1gb of RAM (less might be ok, but wouldn't recommend it), is to setup vmware server (free version), or vmware player (also free), and run Linux and Windows at the same time.

 Cygwin is ok for some stuff, such as it's nice to have awk and sed, but....  not really the best for software development...



TheSource, here it is (I got a little break at work ;). Just comiple with:

g++ -o time -Wno-deprecated time.cpp

Now save the following code into "time.cpp"

#include "stdio.h"
#include "iostream"
#include "strstream"
#include "sstream"
#include "string"

using namespace std;
class Time{public: Time(unsigned long s = 0) { seconds_ = s; } Time(const char *timeStr) { parseFromTimeString(timeStr); } ~Time() {}
bool operator==(const Time& other) { return (seconds_ == other.seconds_); }
bool operator!=(const Time& other) { return (seconds_ != other.seconds_); } bool operator<(const Time& other) { return (seconds_ < other.seconds_); }
bool operator>(const Time& other) { return (seconds_ > other.seconds_); }
const Time& operator+(const Time& other) { seconds_ += other.seconds_; return *this; }
const Time& operator+=(const Time& other) { seconds_ += other.seconds_; return *this; }
const Time& operator-(const Time& other) { // make sure we don't go negative if (seconds_ >= other.seconds_) { seconds_ -= other.seconds_; } else seconds_ = 0; return *this; }
const Time& operator-=(const Time& other) { // make sure we don't go negative if (seconds_ >= other.seconds_) { seconds_ -= other.seconds_; } else seconds_ = 0; return *this; }
bool operator<<(const char* s) { return parseFromTimeString(s); }
bool operator>>(string& s) { return printToString(s); }private: bool printToString(string &s) { bool ret = false; int hour=seconds_/3600; int min=(seconds_%3600)/60; int sec=seconds_%60; stringstream os(ios_base::in | ios_base::out); os.fill('0'); os.width(2); os << hour << ":"; os.fill('0'); os.width(2); os << min << ":"; os.fill('0'); os.width(2); os << sec; os>>s; return ret; } bool parseFromTimeString(const char* s) { bool ret = false; istrstream d1(s); char c='\0'; int hour=0, min=0, sec=0; if (d1 >> hour && hour >= 0) { if (d1 >> c && c == ':') { if (d1 >> min && (min >= 0 && min < 60)) { if (d1 >> c && c == ':') { if (d1 >> sec && (sec >= 0 && sec < 60)) { seconds_ = hour*3600+min*60+sec; ret = true; } } } }
} return ret; }
private: unsigned long seconds_;};
int main(int argc, const char* argv[]){ Time t1,t2; string s; cout<<"Please enter time 1: "; cin>>s; while (! (t1< { cout << "Invalid time 1, please try again: "; cin>>s; } cout<<"Please enter time 2: "; cin>>s; while (! (t2< { cout << "Invalid time 2, please try again: "; cin>>s; } string time1, time2; t1>>time1; t2>>time2; if (t1 == t2) cout << "Times are equal" << endl; else if (t1 > t2) cout << "T1(" << time1 << ") is greater than T2(" << time2 << ")" << endl; else cout << "T1(" << time1 << ") is smaller than T2(" << time2 << ")" << endl;
t2-=t1; t2>>s;
}



PSN ID: krik

Optimistic predictions for 2008 (Feb 5 2008): Wii = 20M, PS3 = 14M, X360 = 9.5M

 

Not sure why it added an empty line between each line...

after compiling run with

./time

It will ask you to enter time (HH:MM:SS) and then so the equal/greater crap. It uses only c++ so you can remove

#include "stdio.h"

from the top.

 

Also at the end there is some test code for the "operator-". You can remove it or print it out. operator-/operator+/operator+= and operator-= are all implemented



PSN ID: krik

Optimistic predictions for 2008 (Feb 5 2008): Wii = 20M, PS3 = 14M, X360 = 9.5M

 

Thanks for the help everyone...

but guess what?

My professor cancelled class again...he still has the flu from Wendsday.

Fortunately, that is what I decided was going to happen at around 2 am after I email him whether he was feeling better...

But now I'm set for Monday

 

 



People are difficult to govern because they have too much knowledge.

When there are more laws, there are more criminals.

- Lao Tzu

TheSource said:

Thanks for the help everyone...

but guess what?

My professor cancelled class again...he still has the flu from Wendsday.

Fortunately, that is what I decided was going to happen at around 2 am after I email him whether he was feeling better...

But now I'm set for Monday

 

 


Try me code dude. I'm pretty sure you can use it ;)



PSN ID: krik

Optimistic predictions for 2008 (Feb 5 2008): Wii = 20M, PS3 = 14M, X360 = 9.5M