Page 1 of 4 123 ... LastLast
Results 1 to 10 of 31

Thread: C++ Help

  1. #1
    Junior Member
    Join Date
    Jul 2001
    Posts
    8

    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

  2. #2
    The Iceman Cometh
    Join Date
    Aug 2001
    Posts
    1,209
    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

  3. #3
    AO Antique pwaring's Avatar
    Join Date
    Aug 2001
    Posts
    1,409
    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>.
    Paul Waring - Web site design and development.

  4. #4
    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'.

  5. #5
    Senior Member
    Join Date
    May 2002
    Posts
    344
    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
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  6. #6
    AO Antique pwaring's Avatar
    Join Date
    Aug 2001
    Posts
    1,409
    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:

    $i = mysql_connect($host, $username, $password) or die("Couldn't connect");

    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!)

    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.
    Paul Waring - Web site design and development.

  7. #7
    Senior Member
    Join Date
    Sep 2003
    Posts
    500
    So pwarning, if you don't use namespace std, then what do you do, define everything before your programs?
    You shall no longer take things at second or third hand,
    nor look through the eyes of the dead...You shall listen to all
    sides and filter them for your self.
    -Walt Whitman-

  8. #8
    Senior Member
    Join Date
    Oct 2001
    Posts
    186
    using std::cout;
    using std::cin;
    using std::endl;

    and so on
    Ben Franklin said it best. \"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.\"

  9. #9
    Senior Member
    Join Date
    May 2002
    Posts
    344
    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 )

    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
    Support your right to arm bears.


    ^^This was the first video game which i played on an old win3.1 box

  10. #10
    White your a guru From now on I'll code respectably && use ||.

    -Life is like a box of chocolates, sweet-

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •