C++ Help

Show 40 post(s) from this thread on one page
Page 1 of 4 123 ... Last
• October 23rd, 2003, 07:17 AM
hickman
C++ Help
Okay here is my question i can do very little programming. I can semi write a program in C++ that inputs 3 test scores and then prints teh avgerage of the two highests scores. But what i can't do is Input 3 values and output one of the following three messaes that best describes the three Values 1. THREE VALUES EQUAL 2. TWO VALUES EQUAL 3. NO VALUES EQUAL. so if someone could kinda of explain how to go about writting the code for a program like this please get back to me
• October 23rd, 2003, 07:45 AM
avdven
Code:

```#include <iostream> using namespace std; int main() {   int x, y, z;   cout << "Input 3 numbers: ";   cin >> x >> y >> z;   if ((x == y) && (y == z))     cout << "Three values equal\n";   else if ((x == y) || (x == z) || (y == z))     cout << "Two values equal\n";   else     cout << "No values equal\n";   return 0; }```
(haven't actually compiled it, but it should work...

The basic logic follows:

- Start by (obviously) declaring three variables (in this case, I used integers)
- Then read in all three integers one by one.
- Next, you need to compare the variables to see how many of them are equal
-> By using nested if-else statements, you can reduce the amount of statement redundancy
-> First - check to see if all the variables are equal
-> Then check to see (since they're not all equal), if two of them are equal
-> Finally, since none are equal, you're done

AJ
• October 23rd, 2003, 10:43 AM
pwaring
Okay, three points need to be made here:

1. Don't post the same question twice - it's confusing (I'd already answered this question in another thread for hickman, and then I came across this one)
2. The tutorials forum is, suprisingly enough, for TUTORIALS. Questions should go in the newbie help forum, programming security or whatever
3. Is it really too much effort to write std::cout instead of cout? Almost all the code and books I have seen on C++ either have the 'using namespace std' statement or just don't bother (meaning in the latter case that it's not standard C++, although you might get away with it if you use <iostream.h> instead of <iostream>.
• October 23rd, 2003, 01:13 PM
PM8228
At least he wont be able to C/P the code and make it work. He'll have to figure some syntax stuff out. Also, do lots of people use the || and &&, I always use 'and' and 'or'.
• October 23rd, 2003, 02:34 PM
White_Eskimo
Quote:

Also, do lots of people use the || and &&, I always use 'and' and 'or'.
err yeah well they are standard operators that pretty much do the same thing in all language. I always use them and i high descourage you from using the "and" or "or" keywords in their place because you might start to get confused with you are using the bitwise operators ('|' or '&'). Also avdven, instead of putting \n (new line escape character for those who dont know...) at the end of your cout statements, you should just and your lines with endl; or because you dont want to piss off pwarning std::endl; :) Anyways, follow pwarning's instructions, read the tutorials and dont post the same thing twice :)
• October 24th, 2003, 11:20 AM
pwaring
Quote:

Originally posted here by PM8228
At least he wont be able to C/P the code and make it work. He'll have to figure some syntax stuff out. Also, do lots of people use the || and &amp;&amp;, I always use 'and' and 'or'.
I always use && and ||, and I've yet to come across any publicly-released code that uses 'and' or 'or' (in fact, to be honest I've never seen them mentioned in any of my resources/books, and I wasn't even sure if they'd work as a result). It's recommended to use the 'shorthand' operators, simply because everyone else does and it's a convention that's widely adopted. I know that the precedence for the longhand operators is lower than the shorthands (not that you should rely on the order of precedence to make your programs work) - which means you can write things like:

in PHP and Perl (okay, Perl version would be different, but you get the idea). I think you could use || instead of or and it would still work though - but I know that I've seen somewhere that if you use or it definitely works because it's lower in the precedence table.

(I think I digressed a bit there, best get back on topic!)

Quote:

Originally posted here by White_Eskimo

err yeah well they are standard operators that pretty much do the same thing in all language. I always use them and i high descourage you from using the &quot;and&quot; or &quot;or&quot; keywords in their place because you might start to get confused with you are using the bitwise operators ('|' or '&amp;'). Also avdven, instead of putting \n (new line escape character for those who dont know...) at the end of your cout statements, you should just and your lines with endl; or because you dont want to piss off pwarning std::endl; :) Anyways, follow pwarning's instructions, read the tutorials and dont post the same thing twice :)

If I recall correctly, std::endl is better than \n because it flushes the output buffer and I think std::cout is buffered by default. So I think (but I'm not sure) that if you created a program with three lines of output using \n, they would all be flushed at the end of the program, whereas using std::endl would mean each one was output at a time. I'm not 100% sure on this, but I remember reading somewhere that std::endl was something to do with buffering and std::cout.

Oh, and my pedanticness when it comes to std::cout instead of using namespace std and cout, is because it sort of defies the whole point of namespaces being available and implemented if you're just going to override them without a good reason.
• October 24th, 2003, 04:36 PM
Lansing_Banda
So pwarning, if you don't use namespace std, then what do you do, define everything before your programs?
• October 24th, 2003, 11:20 PM
EaseZE
using std::cout;
using std::cin;
using std::endl;

and so on
• October 25th, 2003, 01:53 AM
White_Eskimo
Quote:

So pwarning, if you don't use namespace std, then what do you do, define everything before your programs?
I bet he does what any half-smart programmer would do which is put all of the using std directives into a header file and then before he writes his programs, he just types in #include "directives.h" and viola he has everything he wanted (if you read my tutorial about the preprocessor you would know how to do this :))

Quote:

I'm not 100% sure on this, but I remember reading somewhere that std::endl was something to do with buffering and std::cout.
Yup pwarning you are right there...also you could do something by calling the flush() function in the iostream library that will flush the buffer for you...an example of this in code would look something like this:
Code:

```#include <iostream> using std::cout; using std::cin; int main (void) {     char decision;     cout << "Is White_Eskimo a hardcode C++ H4xwh0r3? (y/n)";     cout.flush();     cin >> decision;     if (decision == 'y')             cout << "Damn right he is!";     else             cout << "WRONG...Deleting your C drive..."; }```
Anyways, the only problem about using the flush() function that is built in to your iostream library is that it doesnt skip a line like endl does :)
• October 25th, 2003, 02:11 AM
PM8228
White your a guru :) From now on I'll code respectably && use ||. ;)

-Life is like a box of chocolates, sweet-
Show 40 post(s) from this thread on one page
Page 1 of 4 123 ... Last