PDA

View Full Version : c++ error need help


katael
08-20-2006, 04:02 PM
k i was programming a clock for the members on diablo3.com and ive encountered an error in my code:...

#include <iostream.h>
#include <stdlib.h>


int maestroTime;
int kataelTime;
int darkTime;
int silverTime;
int elfenTime;
int profile;
int time;
int gmt;
int main()
{


cout<< "please enter your time\n";
cin>> time;
cout<< "please select profile:\n (1) Silver \n (2) Elfenlied \n (3) Darkjay& ThatDude \n (4) Maestro \n (5) katael \n";
cin>> profile;


if (profile==1)
{
gmt=time+800;
}
silverTime=gmt-800;
{
if (profile==2)
{
gmt=time-1100;
}
elfenTime=gmt+1100;
{
if (profile==3)
{
gmt=time+500;
}
}
darkTime=gmt-500;
{
if (profile==4)
{
gmt=time-700;
}
maestroTime=gmt+700;
if (profile==5);
{
gmt=time-100;
}
kataelTime=gmt+100;

if (gmt>2399)
{
gmt=gmt-2400;
}
if (gmt<0)
{
gmt=gmt+2400;
}
if (elfenTime>2399)
{
elfenTime=elfenTime-2400;
}
if (kataelTime>2399)
{
kataelTime=kataelTime-2400;
}
if (darkTime<0)
{
darkTime=darkTime+2400;
}
if (silverTime<0)
{
silverTime=silverTime+2400;
}
if (maestroTime>2399)
{
maestroTime=maestroTime-2400;
}

cout<<"gmt at the moment is: \t"<< gmt <<"\n";
cout<<"kataels time at the moment is: \t"<<kataelTime<<"\n";
cout<<"maestros time at the moment is: \t"<<maestroTime<<"\n";
cout<<"Our lord Silvers time at the moment is: \t"<<silverTime<<"\n";
cout<<"Elfenlieds time at the moment is: \t"<<elfenTime<<"\n";
cout<<"Darkjays and Thatdudes time at the moment is: \t"<<darkTime<<"\n";

system ("PAUSE");
return 0;
}





help needed, thanks...

Enlyth
08-20-2006, 04:27 PM
In c++, the use of #include <iostream.h> is deprecated, use <iostream> instead. Don't forget "using namespace std;" then.
You cannot name an int "time", because of the conflict with stuff in <ctime>. You forgot two brackets (the } type) at the end.
And try not to use so many global variables, declare them in main(); Also, the <stdlib.h> (or also <cstdlib>) is not needed in your program.

Edit:
C++ is one of my hobbies, so feel free to ask me anything at any time. :D

katael
08-20-2006, 04:46 PM
thanks for the help , one can see im still learning (from a book) im on my psp atm so ill try it out tommorow

Enlyth
08-20-2006, 04:58 PM
No problem. I have experience that while books are the best source for learning programming languages, you always need to know someone who already knows the language well enough. I came across many examples and techniques in books that were not clear enough, no matter how many times I read them. Then I asked someone and most of the time it was just something simple, written in a complicated way. :P

katael
08-20-2006, 05:28 PM
yeah the whole functions concept is a bit of a hurdle (learning out of: learn c++ in 21 days) havent had enough time to go through it intensively though

katael
08-21-2006, 06:53 AM
k ive gone through it and changed most errors, but the main error still remains: there is some glitch in the if-statements, that makes the program always carry out the last one

(
if (profile==5);
{
gmt=clockTime-100;
}
kataelTime=gmt+100;
)

i changed the variable "time" to "clockTime", should work right?
also, the stdlib.h is used in my program right at the end by the
"system ("PAUSE");" line at the end

thank you

Enlyth
08-21-2006, 07:43 AM
Sorry I don't have much time to look at it right now, but I did notice some unnecessary '{' brackets, that just affects the scope, I don't know why it would carry out something in an if which evaluated false. Try to rewrite it using a switch statement, it looks much nicer.

(i.e.)

switch (profile) {
case 1:
//do something here
break;
case 2:
//do something else
break;
///...etc

default:
//write something about not choosing the right option
break;
}

katael
08-21-2006, 09:24 AM
im not familiar with the switch statement but i tried it like this, wont compile though...

switch (profile){
case 1: (profile==1)
gmt=clockTime+800;
silverTime=gmt-800;
break;

case 2: (profile==2)
gmt=clockTime-1100;
elfenTime=gmt+1100;
break;

case 3: (profile==3)
gmt=clockTime+500;
darkTime=gmt-500;
break;

case 4 (profile==4)
gmt=clockTime-700;
maestroTime=gmt+700;
break;

case 5 (profile==5);
gmt=clockTime-100;
kataelTime=gmt+100;
break;

default (profile!=1,2,3,4,5)
cout<< "please enter a valid profile";
break;

}


does that look right? thanks again for all the help

edit: lol smilies turned on...

Enlyth
08-21-2006, 09:41 AM
You just need 'case x:', the x is what 'profile' is supposed to be equal. If you wanted to do something when profile would be 500, you would put case 500:. There is no need to put the (profile == y) after the case label. Same for the default, just use default: and then what it should do.

Example:

switch (x) {
case 5: //this means if x == 5
x = 3;
break;
}

Edit:
Oh yeah and I forgot, so you dont have to write so much, somevar = somevar + 3; is the same as somevar += 3;, same for * / - % & | ^ >> <<.

katael
08-21-2006, 11:29 AM
cool, big thanks it works perfectly now, ill make a version for that works out the time for every major poster on this site (500+ posts), email hornkarl@gmail.com if you want me to send you the application

Moonek
08-21-2006, 06:02 PM
i just look through the thread and i didn't understan one word of this

Encryption
08-21-2006, 06:04 PM
ditto i dont think most of us did i'm not good with programming^^sorry

katael
08-22-2006, 03:27 AM
dont worry its of course more...i mean less complicated than it looks... i've only been learning how to program for roughly a month though (about 2-3 hours a week, lol never any time)...its so much fun to program though^^

Enlyth
08-22-2006, 07:21 AM
Yeah it's fun, and it will get even more fun when you learn WinAPI programming. :)

katael
08-22-2006, 07:26 AM
umm whats that? can you mabye roughly explain/describe it to me please?

Obsolete_hi
08-22-2006, 08:27 AM
Heh I am looking at this, and I wish I could understand what you guys are writing :P.

katael
08-22-2006, 08:30 AM
i guess if you would start learning, it quickly beginns to make sence...

Obsolete_hi
08-22-2006, 08:35 AM
Well I started to learn how to make program/bots for this game called Furcadia, but I got bored and quit the game :P

katael
08-22-2006, 08:40 AM
you mean like writing the scripts for actuall bots? thats quite cool... i prefer actually designing games to programming i suppose...id say they both more fun than actually playing them...i made a lot of cs maps...before i stopped

Obsolete_hi
08-22-2006, 09:17 AM
Dude thats awesome, I think it is harder to make maps and stuff because you have to add triggers and different things to them. Bots are easy because you just program the thing to do stuff. The one I had was programed to join the game furcadia and upload a dream, which is basically a map that you make with there editor, then it would enter the dream/map and greet people as the entered.

katael
08-22-2006, 09:26 AM
mabye i should check out furcadia... making maps in cs is easy...you can learn it in an afternoon...unreal editor...havent even tried to figure it out lol

Obsolete_hi
08-22-2006, 09:33 AM
No clue but the editors they have in Furcadia is awesome. It makes things easy like a point and click to lay down the backrounds. It also can get tough when adding the different triggers and stuff. Here is the url to it www.furcadia.com (http://www.furcadia.com).
It free at least I still think it is. It's basically an online chat game that is animated.

katael
08-22-2006, 09:36 AM
cool im busy checking it out, thanks

katael
08-22-2006, 09:49 AM
ummm jeeez...that game is...abstract...mabye to much so...ill try it again later

Enlyth
08-22-2006, 10:13 AM
API means application program interface. The Windows API provides you with all sorts of things for programming windows applications. For instance, you will need to use the Windows API when making programs with graphic user interfaces instead of just a console. It is documented on MSDN, but one should learn to write decent C++ programs before beginning with WinAPI programming.

You can even write to the console with the Windows API, you can try this simple test if you are curious:


#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <cstdlib>

void __stdcall WriteToConsole(const char* Message) {
DWORD BytesWritten;
while(*Message != '\0')
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), Message++, 1, &BytesWritten, NULL);
}
int main() {
WriteToConsole("Hello, this is a test.\n");
system("pause");
return 0;
}

katael
08-22-2006, 10:19 AM
thanks you have reawakened my interesst in programming and i picked up my book again... i still dont quite understand completely how to use functions though...