Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 24th, 2005, 6:27 AM   #41
Broax
Hobbyist Programmer
 
Broax's Avatar
 
Join Date: Jan 2005
Location: Porto, Portugal
Posts: 142
Rep Power: 4 Broax is on a distinguished road
Send a message via MSN to Broax
Hmmmmm... This operatores section is really big and hard to memorize...

I mean, things like = and == or % or ! and so on are easy for one to memorize, but bitwise operators and priority operators (for example) are getting a bit confusing... Specialy (so far) the Logical operators... I can't seem to understand how to use && and ||, could someone care to explain it to me, please?
__________________
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
Broax is offline   Reply With Quote
Old Jan 24th, 2005, 6:53 AM   #42
Broax
Hobbyist Programmer
 
Broax's Avatar
 
Join Date: Jan 2005
Location: Porto, Portugal
Posts: 142
Rep Power: 4 Broax is on a distinguished road
Send a message via MSN to Broax
Sorry for the triple post, but...

Can someone tell me what I'm missing in this code? I've already fixed 5 errors or something like that, but those remain, and I don't get it... =/

#include <iostream>
using namespace std;

string str1;
int a;
int b;
int c;
float d;
float e;

int main();
{
    int a = 2;
    int b = 5;
    float d = 0.5;
    cout << "stupid little operators test by Broax...\n";
    a + b = c;
    c + d = e;
    e >= 4 ? cout << "testA" << : << "testeB" << endl;
    cout << "The result is: " << e << endl;
    
    return 0;
}

Once again I think this is self-explanatory... It's just supposed to output some basic text, and then the result of some basic math thingy...

Anyway, I'm having problems with all this new stuff I've learned and, most importantly... I CAN'T DEFINE VARIABLES!!!!!

I thought I was supposed (or that at least I could) declarate them in the beginning of the code, and then define the value whenever I felt like it (for example, a diferent value inside each function If I wish to)... What I mean is this:

int number;

int main()
number = 2;

Or.....

int number = 2;

int main()
bla bla bla

On the first example I'd define a global variable and on the second example I'd declare a local variable...

Apparently I got something wrong, so I'd appreciate some help on that...
__________________
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
Broax is offline   Reply With Quote
Old Jan 24th, 2005, 8:45 AM   #43
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
Red face

#include <iostream>
using namespace std;

***************************0************************
string str1;
int a;
int b;
int c;
float d;
float e;

int main();***************1*****************
{
    int a = 2;
    int b = 5;
    float d = 0.5;
    cout << "stupid little operators test by Broax...\n";
    a + b = c;*****************2*********************
    c + d = e;*****************3*********************
    e >= 4 ? cout << "testA" << : << "testeB" << endl;**************4*****************
    cout << "The result is: " << e << endl;
    
    return 0;
}

refer to the areas marked by asterisks and shit...

0. these variables must be declared in the function that they are used. put them inside the "int" main" part...major programming error.

1. there is no semicolon between int main() and the first brace ( { )...minor syntax error.

2. don't think of this sign here---> =
as the "equals" sign. think of it as the assignment operator. every time you think about an expression like this think this: "take what is on the RIGHT side and put that value into the LEFT side. example:

int x = 3 //put the value 3 into the variable x.

as soon as you think of it as this "right to left" idea, things get much easier. so when you tell the compiler to add two variables and then put a value into them it has no fucking idea what you're talking about. if "x" is a jar then you tell the program to put whatever is on the right side into the jar "x". from then on, when you say "x" it will see what you put into it. remember, in C++ "=" is the assaignment operator, "==" is what actually means "equals". point is ONLY PUT ONE THING ON THE LEFT SIDE.

3. same thing...major programming error (don't worry, the best way to learn is through errors, if you always did everything right then you're either a god or a lucky fool. the compiler can be a great teacher. listen to it.

4. i beleive you want to put an "if" statement here...major programming error.

try this finished code...
#include <iostream>
using namespace std;

int main()                    ***************1*****************
{

string str1;
int a;
int b;
int c;
float d;
float e = 6.768;
float f;

    int a = 2;
    int b = 5;
    float d = 0.5;
    cout << "stupid little operators test by Broax...\n"<<endl;
    c=a+b;              *****************2*********************
    f=d+e;               *****************3*********************
    if (f >= 4)
         { cout << "the condition is held TRUE so i am printing this                                                                                        shit"<<endl;
              }            **************4*****************
    cout << "The result is: " << e << endl;
    
    return 0;
}
the code is kinda dumb, but so am i after nine beers. that's about all i'm prepared to deal with.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.

Last edited by bl00dninja; Jan 24th, 2005 at 8:50 AM.
bl00dninja is offline   Reply With Quote
Old Jan 24th, 2005, 3:36 PM   #44
Broax
Hobbyist Programmer
 
Broax's Avatar
 
Join Date: Jan 2005
Location: Porto, Portugal
Posts: 142
Rep Power: 4 Broax is on a distinguished road
Send a message via MSN to Broax
Ok... So here I seem to have a major problem that is driving me insane!

how do I declare a global variable? and how do I assign a global value to that variable?!

Anyway, most errors are regarding stupid stuff that I actually *know* but that I overlooked (I tend to do that a lot)...

Anyway, for the "if" part you talked about, I was basing myself on the following passage:

Quote:
Conditional operator ( ? ).
The conditional operator evaluates an expression and returns a different value according to the evaluated expression, depending on whether it is true or false. Its format is:

condition ? result1 : result2

if condition is true the expression will return result1, if not it will return result2.

7==5 ? 4 : 3 returns 3 since 7 is not equal to 5.
7==5+2 ? 4 : 3 returns 4 since 7 is equal to 5+2.
5>3 ? a : b returns a, since 5 is greater than 3.
a>b ? a : b returns the greater one, a or b.
But pleaaaase!!! EXPLAINE ME ABOUT THE GLOBAL VARIABLES!!!
__________________
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
Broax is offline   Reply With Quote
Old Jan 25th, 2005, 7:22 PM   #45
Broax
Hobbyist Programmer
 
Broax's Avatar
 
Join Date: Jan 2005
Location: Porto, Portugal
Posts: 142
Rep Power: 4 Broax is on a distinguished road
Send a message via MSN to Broax
Heeeelp!

Am I that dull and annoying that no one will bother to answer anymore?
__________________
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
Broax is offline   Reply With Quote
Old Jan 25th, 2005, 7:34 PM   #46
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
#include <iostream>
#include <string.h>
using namespace std;

string str1;
int a;
int b;
int c;
float d;
float e;

int main()
{
    int a = 2;
    int b = 5;
    float d = 0.5;
    cout << "stupid little operators test by Broax...\n";
    c = a + b;
    e = c + d;
    cout << ((a >= 4)?"testA":"testeB") << endl;
    cout << "The result is: " << e << endl;
    return 0;
}
__________________

tempest is offline   Reply With Quote
Old Jan 25th, 2005, 9:18 PM   #47
Broax
Hobbyist Programmer
 
Broax's Avatar
 
Join Date: Jan 2005
Location: Porto, Portugal
Posts: 142
Rep Power: 4 Broax is on a distinguished road
Send a message via MSN to Broax
That doesn't actually help a fucking lot you know? You might as well say that I'm dull and annoying and get it over with........
__________________
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
PORTUGALPORTUGA
Broax is offline   Reply With Quote
Old Jan 25th, 2005, 9:44 PM   #48
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
Global variables are generally declared before main. Their scope lasts throughout the entire program. You can assign them a value when you declare them, or later on in the program, it doesn't really matter.

Couple of things on the side:

1.) You would probably get more replies if you created individual threads in the appropriate forum with your questions. Honestly, I don't check introduction threads once I've said hi more than once or twice. I could almost assure you that the help you recieve will be 10 times better in the C/C++ forum in various threads than you're going to get hear. If you'd like, I can split this thread for you so the C/C++ oriented questions are in the proper forum and make a thread or two (as needed) for your various questions.

2.) From what I've learned, global variables are conventionally to be avoided.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower

Last edited by Mjordan2nd; Jan 25th, 2005 at 9:55 PM.
Mjordan2nd is offline   Reply With Quote
Old Jan 26th, 2005, 12:52 AM   #49
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
Talking

jesus, it's only been like 12 hours since i last checked. anyways, to have a global variable, you just declare and initialize it outside of any function like so:

#include <iostream>
using namespace std;

x = 1;  //  <--global variable, declared and such OUTSIDE of any function

int main()
{
  cout<<"here is the global variable..."<<x<<endl;
  
  return 0;
}

now, because it's global anything can change it's value and can be accessed and used and shit bu anyone. i wrote a simple crappy four-function calculator in C, C++, and Java awhile back and posted the code. i used a global variable in the version that i wrote in C to maintain a running tab on what the current value was. i'll find it and post it next...
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Jan 26th, 2005, 12:57 AM   #50
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
Talking

............................can't find it now, double0posted by accident cuz the site went down at 0200 eastern time
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.

Last edited by bl00dninja; Jan 26th, 2005 at 1:16 AM.
bl00dninja is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:00 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC