OMG ! c++ problem Created 15 years ago2009-02-24 13:10:55 UTC by Striker Striker

Created 15 years ago2009-02-24 13:10:55 UTC by Striker Striker

Posted 15 years ago2009-02-24 13:10:55 UTC Post #263202
Well after a long break I decided I'd take devcpp for more practice as a beginner, before going to a new level ( matrices- at a first look I don't think they'll be to hard but ... anyway)

So I made this program (it's just a test because it will be part of a another program)

[quote]#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
int x;
cout<<"Input a number to specify the delay between the algorithm analyze processes   (2,3 or 4 seconds)"<<endl;
cin>>x;
if(x!2||3||4){
              cout<<"================== ERROR !!! ==============================="<<endl;
              }
if(x==2){
         Sleep(2000);
         }
if(x==3){
         Sleep(3000);
         }
 if(x==4){
         Sleep(4000);
         }
cout<<"TEST"<<endl;
system("pause");
return main();
}[/quote]

And when I try to compile it says
expected `)' before '!' token
Take the c++ book. Search. Find. I see that "!" is the correct symbol for "NOT" . Also, "||" is the correct symbol for "OR". What did I do wrong ?
Striker StrikerI forgot to check the oil pressure
Posted 15 years ago2009-02-24 13:16:33 UTC Post #263203
You have to format it something like this:
if(x != 2 || x != 3 || x != 4)
ChickenFist ChickenFist<Witty Title>
Posted 15 years ago2009-02-24 13:24:59 UTC Post #263205
Ah man, I'm dum .I should've remembered this from Gmod Wiring X( . Thank you very much.

Anyway the correct expression is
if(x<2||x>4)
for what I want, I just discovered .
I think I'll be asking more questions in this thread because the last one I did has to be bumped.
Striker StrikerI forgot to check the oil pressure
Posted 15 years ago2009-02-24 20:33:59 UTC Post #263222
@vs49688: main should return an int. And I wouldn't call your code any less complex; it's roughly the same amount of lines and a similar construction.

@The_(c)Striker: in your code, those if statements can be replaced by else-if statements. Those checks logically belong 'together' in a group, so why not link them together? Only if A is not true, go and check B, C & D. If A is true, then forget about B, C & D.
Oh, and you could link the delay to the input: if the user entered something between 2 and 4, then wait for (1000 * user-input) milliseconds.

Also, it's better to just place a breakpoint at the end of a program, rather than a system("pause") call. Right now, your resulting program will always wait for user input, whether you're running it from your IDE or not.
Posted 15 years ago2009-02-24 22:26:27 UTC Post #263230
Good C++ requires the main function to return an int. It is outright bad practice to have a main function return a void.
Penguinboy PenguinboyHaha, I died again!
Posted 15 years ago2009-02-24 23:24:13 UTC Post #263231
It's not entirely neccessary for main to return int, it's just good practice. There are some cases where you may want to return something else and in programs such as your one, it's perfectly valid to use void however it's not good practice. If you for example, if you want to create a wrapper for your program or make a script that chains several programs you'll probably want to be making use of exit codes to track errors or halt the script when one occurs. Even if YOU don't want to create a wrapper or make a script like the example I gave, someone else might want to use your program in one.

Also, don't get bother getting comfortable with the Windows API unless you absolutely need to use it. You should make as much use of the standard library as possible and not resort to using platform specific functions/APIs/features unless it's imperative. It might not matter with little test apps, but if you actually release something useful that people will want to download, cross-platform compatibility is a very good thing. There'll be significantly less hassle porting something that isn't riddled with platform-specific code.
m0p m0pIllogical.
Posted 15 years ago2009-02-25 13:29:16 UTC Post #263273
void main is not legal C++.

Not that I'm into language standards or something, and it's (imo) a rather small detail, but oh well. :)

Good points, m0p. I haven't seen return codes being used frequently, but they do come in handy sometimes.
Posted 15 years ago2009-02-25 13:49:37 UTC Post #263275
Oh, and you could link the delay to the input: if the user entered something between 2 and 4, then wait for (1000 * user-input) milliseconds.
I tried using a variable to do that and didn't work. I did something like

int x;
and then at the sleep thing I did :
Sleep(x);
The compiler gave me an error.
Also, don't get bother getting comfortable with the Windows API unless you absolutely need to use it. You should make as much use of the standard library as possible and not resort to using platform specific functions/APIs/features unless it's imperative. It might not matter with little test apps, but if you actually release something useful that people will want to download, cross-platform compatibility is a very good thing. There'll be significantly less hassle porting something that isn't riddled with platform-specific code.
You know, I'm constantly bothered by this problem. From my initiation I thought about this, because I know system commands are directly linked to windows. I just don't know yet libraries(or does iostream include such commands ?) with commands that could replace the system ones.
But what about the windows.h library ? That's a library which is compiled into the program, and it will work on another platform, like Linux wouldn't it ?
Anyway, I have a long way to go with c++. I think when I'll make my first 2D game I'll be like "I can die in peace now !"
Striker StrikerI forgot to check the oil pressure
Posted 15 years ago2009-02-25 15:43:12 UTC Post #263277
int x;
and then at the sleep thing I did :
Sleep(x);
The compiler gave me an error.
Sleep() requires the time to be an DWORD, so you have to convert it.

int x;
Sleep(static_cast<DWORD>(x));
should work. Note that Sleep() is a Windows-only function.
But what about the windows.h library ? That's a library which is compiled into the program, and it will work on another platform, like Linux wouldn't it ?
Well, No. windows.h contains call to Windows API functions, which wouldn't work on any other platform. An easy and platform independent way to (almost) replace the system() command in your program would be to use
cout << "Press ENTER to continue."; getchar();
ChickenFist ChickenFist<Witty Title>
Posted 15 years ago2009-02-25 16:33:58 UTC Post #263278
thank you very much for the Sleep(static_cast<DWORD>(x)); tip !
Striker StrikerI forgot to check the oil pressure
Posted 15 years ago2009-02-25 17:18:44 UTC Post #263280
I think we should do some more discussing of the return value of the main int, then errupt in a horrible flameware which ends up spamming the topic with dodge C++ code...

Oh wait, wrong forum.. :D
Posted 15 years ago2009-02-25 19:10:09 UTC Post #263285
If you'd like this to be compilable for *nix as well, you can add this to the top of your file:
#ifdef WIN32
    #include <windows.h>
    #define Sleep(x) Sleep((x)*1000)
#else
    #include <unistd.h>
    #define Sleep(x) sleep((x))
#endif
You'll have to specify the time in seconds instead of milliseconds though. Not that this little app needs to be cross-platform anyway, but just for future reference.
Posted 15 years ago2009-02-26 17:00:53 UTC Post #263344
I'm not prepared for cross-platform programming yet. I need to have a solid base of c++ knowledge first(and, as I know, wine can emulate windows apps).Thanks for the attention,though, Caboose.

Ok, the program I succeded to make, thanks to ChickenFist, was an auto-sequence-writer for a "while" pseudo-code we currently learn at school(I promised this to my informatics teacher when I saw how much takes him to write these sequences on the blackboard).
And I noticed something strance for 0+1+2+3+.....+36 . Try it out yourself(well, use [n(n+1)]\2 if you don't already know this)

O.K. Yesterday I discovered the Beep function ! I had fun with it, modifying the pitch and time it plays. And I made a little program ! :

[quote]#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
int x,i,f,g,speed;
x=0;
Beep(900,50);
Beep(900,50);
Beep(100,50);
Beep(900,100);
Beep(900,100);
Beep(100,100);
Beep(900,50);
Beep(900,50);
Beep(900,50);
cout<<"What the fuck was that ? Wanna' hear some more ? :D "<<endl;
cout<<"You know the while instruction can come in handy here :P" <<endl<<"Have a ...hmm... listen !"<<endl;
Sleep(2000);
cout<<" "<<endl;
cout<<"Type here a number for how fast you want the pitch sounds cycle to be(in miliseconds !)"<<endl;
cin>>speed;
cout<<" "<<endl;
cout<<"Type the number of times the beep will play ( each time the pitch increases) "<<endl;
cin>>i;
cout<<" "<<endl;
cout<<"Type the pitch multiplier ( pitch starts at 0 , so the first sound won't be played because 0*multiplier=0 "<<endl;
cout<<"Also pitch*multiplier is measured in Hz(hertz) . So for example, if you have the multiplier at 40 and the pitch is at the second cycle, the result will be 2*40=80 Hz "<<endl;
cin>>g;
cout<<" "<<endl;
cout<<"Ready ! Press any key to continue with the cycle ! "<<endl;
cout<<" "<<endl;
system("pause");

while(x<=i){
f=x*g;
Beep(static_cast<DWORD>(f),static_cast<DWORD>(speed));
cout<<"Beep number "<<x<<" "<<endl;
x=x+1;
}
system("pause");
system("CLS");

return main();
}
[/quote]
I recommend you to use it in class if you want to get over somebody's nerves :D :D :P

And today I discovered how to play a sound with playsound function !!!
I had to hardly google every minute ...
Something like this : [quote]#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
cout<<"This program will be a test program for sounds !"<<endl;
PlaySound(TEXT("test/sunetul.wav"), NULL, SND_FILENAME);
system("pause");
return 0;
}
[/quote]
But at first I didn't know I had to include a library, and had to make a project instead of a simple source file ( dev-c++). Well , for this PlaySound function I want to ask you a couple of things :

How can I adjust the volume for it ? Is there any pitch option ? How can cout(or do something else) some things while a sound runs in the backround ?

Thank you.
Striker StrikerI forgot to check the oil pressure
Posted 15 years ago2009-02-26 22:35:56 UTC Post #263384
Stop learning useless windows functions and start learning programming for real. Start by getting in-depth knowledge of object oriented programming - make your own classes and instantiate your own objects, and get comfortable with it.

A task we had in programming class while doing basic OOP was make your own simple card deck - start with a Card object with appropriate properties and methods, then move onto a Deck object, consisting of, perhaps, an array of Card objects.

I thought this was a pretty good way to get the hang of what you could do with OOP without the program getting too complicated. Mind, I'm not very good with C++, and I don't think I ever will be. Fucking annoying code. Anyway, if you want my old deck classes for reference or something, I'll poast.

Edit: Oh, and when posting code, use
[pre]
[/pre].
Posted 15 years ago2009-02-27 08:47:12 UTC Post #263395
What Elof said. Also, by cross-platform programming I mean making full use of the capabilities of the C++ standard library. That IS the solid base of C++ knowledge. Really, don't bother learning the Windows API until you're comfortable with the language. It will teach you bad, lazy practices and it'll turn you into a sloppy programmer who can't work with anything that doesn't use Windows' API.
m0p m0pIllogical.
Posted 15 years ago2009-02-27 12:06:04 UTC Post #263400
Well, until now I just used system functions , sleep and playsound ( altough I already downloaded fmod, and I'm trying to learn how to use it in my program)
I'm still very far away from knowing how to make a window ...

It's just that, I think ... I have to learn a lot of basic things before getting into the real bussiness and this gives me the feel that I'm going nowhere with this ( I frequently think at complicated programs when I read from my c++ book, and that's why I have this feel- especially because I've done nothing similar with a real program yet :( )

I have problems understanding for. What the fuck are they talking about in this book ? What advantages ? It says that for is more clear than while ... I'm sorry, but I can understand better while than for .
At a point I had difficulties ( and still have minor ones) understanding a program that verifies number for primeness.
Striker StrikerI forgot to check the oil pressure
Posted 15 years ago2009-02-27 12:47:09 UTC Post #263402
for(do something once; if this is true continue looping; do this after every loop) do this every loop
for(int i=0; i < 2; i++) cout << i;
i is undefined
define i and set it to 0
i is less than 2, continue looping
output i (which is equal to 0)
increase i by 1
i is less than 2, continue looping
output i (which is equal to 1)
increase i by 1
i is NOT less than 2, stop looping
i is undefined
Once you have gotten used to for you will probably agree that for is more clear than while.
I had problems with it too, when I was learning JavaScript (for works the same in JS as in C++) some six years ago.
Oskar Potatis Oskar Potatis🦔
Posted 15 years ago2009-02-27 13:01:36 UTC Post #263403
for is like a simplified version of while. You can accomplish for loops easily with a while loop - consider for-loops like a shortcut.

int i = 0;
while(i < 10) {
i++;
cout << "i has value: " << i;
}
[/pre] is EXACTLY the same as [pre]
for(int i = 0; i < 10; i++) {
cout << "i has value: " << i;
}
Basically, for is a while-loop with built-in variable declaration (int i = 0), conditional (i < 10), and action to perform each loop (i++).
Posted 15 years ago2009-02-27 13:18:13 UTC Post #263404
can I do more than one action between the brackets ? because I didn't see this written somewhere ...
Striker StrikerI forgot to check the oil pressure
Posted 15 years ago2009-02-27 13:45:07 UTC Post #263405
Well, until now I just used system functions , sleep and playsound ( altough I already downloaded fmod, and I'm trying to learn how to use it in my program)
I'm still very far away from knowing how to make a window ...
I don't know how I can make this any clearer.. learn C++ before learning Windows API. Forget spawning windows and using audio. Learn C++. If you don't understand the syntax of for-loops, then you've got a bit of a way to go. Get some grasp on OOP, learn all the ins and outs of the syntax and the standard library so you can make the most of it. Don't even bother including Windows headers.

It might be fun to make silly test programs that play sounds or spawn windows that don't do anything but you won't learn C++ that way. Pick a simple project that wouldn't possibly need to use anything outside the standard library, like a primality tester. That's what I used to teach myself C.

Start your projects out small and simple then build on them gradually, fixing problems yourself with the help of a reference manual (I'd recommend you get a copy of The C++ Programming Language by Bjarne Stroustrup, I wouldn't recommend trying to read it cover to cover though). Refrain from reading tutorials and reading other peoples code. The only use for tutorials is to give you a basic idea of concepts like types, pointers, variables, loops and the general syntax, you shouldn't follow them to the word, I find learning from tutorials/dummies guides to be detrimental once you have a grasp of the syntax and understand what all of the important features can do.

Edit: If you're too impatient to teach yourself, pay for a proper course. It's the most effective way to learn, if a little expensive. You'll get exercises, have things explained to you clearly and get help when you don't understand something. It's better than taking the half-assed "LOL WINDOWS API SHORTCUTS" route or following badly written tutorials that don't really explain things clearly, and second only to teaching yourself with a reference manual (but that takes a bit of patience and time).
m0p m0pIllogical.
Posted 15 years ago2009-02-27 13:54:47 UTC Post #263406
can I do more than one action between the brackets ?
If you mean the { and the }, then yes. (The word brackets have different meaning in different locations.)
Oskar Potatis Oskar Potatis🦔
Posted 15 years ago2009-02-27 14:19:59 UTC Post #263407
You seemed clearer now m0p :) .
Thanks potatis_invalid ( I know there are lots of thanks and thing like this in this thread, but I'll keep this thread for reference ;) )

Can I use a goto label with "{","}" ?
Example :
label:{
actions;
actions;
actions;
}
Now, I'm not talking about using GOTO instead of if,while,for. I need this for some cases in a switch.I have lots of conditions, and if some of them are negative, I want to jump over several lines of code. I'm asking this because I don't know calling functions yet, only making a simple "int main()" program :) ( and besides, I consider this easier than defining some functions ...)
Striker StrikerI forgot to check the oil pressure
Posted 15 years ago2009-02-27 19:28:30 UTC Post #263425
Don't use goto.
Ever.
Especially when you're still learning :/
You should attend a class on programming and C++ instead of doing everything the wrong way like you are now.
Penguinboy PenguinboyHaha, I died again!
You must be logged in to post a response.