Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: BaDDBLooD on August 21, 2004, 02:15 PM

Title: My First c++ application
Post by: BaDDBLooD on August 21, 2004, 02:15 PM


#include <iostream.h>

int main()
{

   int choice;
   int number1;
   int number2;
   char indicator;

   cout << "1) Addition" << endl
       << "2) Subtraction" << endl
        << "3) Multiplication" << endl
        << "4) Division" << endl;

   do
   {
      cout << "Enter First Digit: ";
      cin >> number1;

      cout << "Enter Second Digit: ";
      cin >> number2;

      cout << "Choose Operator: ";
      cin >> choice;

      switch(choice)
      {
         case 1: cout << endl << "The answer is: " << number1 + number2 << endl;                  
               break;
         case 2: cout << endl << "The answer is: " << number1 - number2 << endl;                   
               break;
         case 3: cout << endl << "The answer is: " << number1 * number2 << endl;                   
               break;
         case 4: cout << endl << "The answer is: " << number1 / number2 << endl;                   
               break;
         default: cout << endl <<"You chose invalid selection" << endl;
      }

      cout << endl << "Do you want to enter another (y or n)? ";   
      cin >> indicator;
      cout << endl;
   } while((indicator == 'Y' || indicator == 'y'));

  return 0;
}



Suggestions / Comments Apreciated!
Title: Re:My First c++ application
Post by: iago on August 21, 2004, 02:17 PM
It's better than Mephisto's (http://forum.valhallalegends.com/phpbbs/index.php?board=30;action=display;threadid=6430;start=msg56384#msg56384) :D

<edit> Don't take any of his advice in that thread, please.  It's not very good.
Title: Re:My First c++ application
Post by: Sargera on August 21, 2004, 02:32 PM
Quote from: BaDDBLooD on August 21, 2004, 02:18 PM
Lmao! wow that is really bad!

Actually, I just think you're stupid and agreeing with iago.  It's generally shit, and thanks again, iago (*grins*) for bringing that up again.  With a few edits of that calculator (bad coding, like recursing main and the array usage which I don't actually mind doing in such an insignificant application) and it's fine.  :)

Edit: Your calculator is no better than someone elses who is starting out in C++.  Oh yeah, and you might want to use ANSI C++ :)
Title: Re:My First c++ application
Post by: iago on August 21, 2004, 02:36 PM
Quote from: Sargera on August 21, 2004, 02:32 PM
Quote from: BaDDBLooD on August 21, 2004, 02:18 PM
Lmao! wow that is really bad!

Actually, I just think you're stupid and agreeing with iago.  It's generally shit, and thanks again, iago (*grins*) for bringing that up again.  With a few edits of that calculator (bad coding, like recursing main and the array usage which I don't actually mind doing in such an insignificant application) and it's fine.  :)

Edit: Your calculator is no better than someone elses who is starting out in C++.  Oh yeah, and you mind want to use ANSI C++ :)

I think his is perfectly good :)

And lol @ you replying so fast.  I was going to aim you soon, but I was eating pizza :(

Pizza > Mephisto
Title: Re:My First c++ application
Post by: BaDDBLooD on August 21, 2004, 03:23 PM
My Second Application:



#include <iostream.h>

int main()
{
   char indicator = 'n';
   int value = 0;
   double f = 0;

   do
   {
      cout << endl << "Enter value: ";
      cin >> value;

      f=1;

      for(int i = 2; i<=value; i++)
         f *= i;

      cout <<  value << "! is " << f;

      cout << endl << "Do you want to enter another value (y or n)? ";
      cin >> indicator;

   } while((indicator=='y') || (indicator=='Y'));

   return 0;
}

Title: Re:My First c++ application
Post by: Sargera on August 21, 2004, 03:32 PM
Maybe you should use contemporary ANSI C++ :)

Also, your indents are inconsistant.  Should generally always use a 4-space tab.  Some people like to use 3-space tabs and 2-space tabs when indenting their conditional statements.
Title: Re:My First c++ application
Post by: BaDDBLooD on August 21, 2004, 03:35 PM
Eh.. it looks correct in vc++, it fucks up when i use code tags ;O

another program:



#include <iostream.h>

int main(void)
{
   int f = 0, c = 0, type = 0;
   char indicator = 'n';

   do
   {
      cout << "1) Farenheit to Celcius" << endl
         << "2) Celcius to Farenheit" << endl
         << endl << "What do you want to convert? ";

      cin >> type;

      if(type == 1)
      {
         cout << endl << "Enter Temperature in Farenheit: ";
         cin >> f;
         cout << "Temperature: " << (f/1.8) - 32 << " Celcius" << endl;
      }
      else if(type == 2)
      {
         cout << endl << "Enter Temperature in Celcius: ";
         cin >> c;
         cout << "Temperature: " << (1.8*c) + 32 << " Farenheit" << endl;
      }

      cout << "Do you want to enter another value (y or n)? ";
      cin >> indicator;

   } while((indicator=='y' || indicator=='Y'));

   return 0;
}

Title: Re:My First c++ application
Post by: Yoni on August 21, 2004, 05:55 PM
I wouldn't multiply floats by ints unless there's a special reason to.

i.e., instead of doing (1.8*c), you're better off doing (9*c)/5.
And instead of (f/1.8), use (5*f)/9.
Title: Re:My First c++ application
Post by: Sargera on August 21, 2004, 06:11 PM
Why do you have such a problem using up-to-date ANSI C++?  :(
Title: Re:My First c++ application
Post by: Adron on August 21, 2004, 06:18 PM
Quote from: Yoni on August 21, 2004, 05:55 PM
I wouldn't multiply floats by ints unless there's a special reason to.

i.e., instead of doing (1.8*c), you're better off doing (9*c)/5.
And instead of (f/1.8), use (5*f)/9.

You made me think of f/2.8. Do you know a good explanation for f/2.8 etc?
Title: Re:My First c++ application
Post by: BaDDBLooD on August 21, 2004, 06:28 PM
Quote from: Sargera on August 21, 2004, 06:11 PM
Why do you have such a problem using up-to-date ANSI C++?  :(

What are you talking about?
Title: Re:My First c++ application
Post by: Adron on August 21, 2004, 06:30 PM
Quote from: BaDDBLooD on August 21, 2004, 06:28 PM
Quote from: Sargera on August 21, 2004, 06:11 PM
Why do you have such a problem using up-to-date ANSI C++?  :(

What are you talking about?

Maybe he wants you to use <iostream> instead of <iostream.h>.
Title: Re:My First c++ application
Post by: Grok on August 21, 2004, 06:39 PM
"You code is bad"
"Your code is worse"

such posts that are not followed by constructive suggestions will be held against the person who posted it.
Title: Re:My First c++ application
Post by: Kp on August 21, 2004, 08:18 PM
Quote from: Grok on August 21, 2004, 06:39 PM"You code is bad"
"Your code is worse"

such posts that are not followed by constructive suggestions will be held against the person who posted it.

Is it considered constructive to inform the original author that his bad code is an inherent fault of the language he has chosen to use, and that he should therefore consider switching immediately to a superior language? :P
Title: Re:My First c++ application
Post by: Sargera on August 21, 2004, 10:25 PM
Quote from: Adron on August 21, 2004, 06:30 PM
Quote from: BaDDBLooD on August 21, 2004, 06:28 PM
Quote from: Sargera on August 21, 2004, 06:11 PM
Why do you have such a problem using up-to-date ANSI C++?  :(

What are you talking about?

Maybe he wants you to use <iostream> instead of <iostream.h>.

Hmm, I was always told (and thought) that <iostream> was up-to-date on ANSI standards with the usage of the std namespace, as where <iostream.h> was old ANSI C++.
Title: Re:My First c++ application
Post by: Stwong on August 21, 2004, 11:59 PM
I tend to use stdio, but that's just personal preference.  Well, thanks to a speed advantage. :P
Title: Re:My First c++ application
Post by: Sargera on August 22, 2004, 12:00 AM
Quote from: Stwong on August 21, 2004, 11:59 PM
I tend to use stdio, but that's just personal preference.  Well, thanks to a speed advantage. :P

A speed advantage which is in nearly every case irrelevant?
Title: Re:My First c++ application
Post by: Arta on August 22, 2004, 01:05 PM
Quote from: Sargera on August 22, 2004, 12:00 AM
irrelevant?

Lies.
Title: Re:My First c++ application
Post by: MyndFyre on August 22, 2004, 02:08 PM
Quote from: Arta[vL] on August 22, 2004, 01:05 PM
Quote from: Sargera on August 22, 2004, 12:00 AM
irrelevant?

Lies.

Would either of you care to clarify what you mean?
Title: Re:My First c++ application
Post by: Banana fanna fo fanna on August 22, 2004, 02:35 PM
sargera is the definition of a troll.
Title: Re:My First c++ application
Post by: Adron on August 22, 2004, 02:45 PM
Quote from: Sargera on August 22, 2004, 12:00 AM
Quote from: Stwong on August 21, 2004, 11:59 PM
I tend to use stdio, but that's just personal preference.  Well, thanks to a speed advantage. :P

A speed advantage which is in nearly every case irrelevant?

I think I'd say "in most cases there's no relevant speed advantage to using stdio".
Title: Re:My First c++ application
Post by: Grok on August 23, 2004, 03:42 AM
Quote from: $t0rm on August 22, 2004, 02:35 PM
sargera is the definition of a troll.

The above quote is the definition of a personal attack.  A name, a label, an intent, and no accompanying on-topic material to cleverly hide that intent.
Title: Re:My First c++ application
Post by: Maddox on August 23, 2004, 04:07 AM
Quote from: Grok on August 23, 2004, 03:42 AM
Quote from: $t0rm on August 22, 2004, 02:35 PM
sargera is the definition of a troll.

The above quote is the definition of a personal attack.  A name, a label, an intent, and no accompanying on-topic material to cleverly hide that intent.

Just felt like typing something Grok?
Title: Re:My First c++ application
Post by: BaDDBLooD on August 23, 2004, 07:48 AM
Quote from: Maddox on August 23, 2004, 04:07 AM
Quote from: Grok on August 23, 2004, 03:42 AM
Quote from: $t0rm on August 22, 2004, 02:35 PM
sargera is the definition of a troll.

The above quote is the definition of a personal attack.  A name, a label, an intent, and no accompanying on-topic material to cleverly hide that intent.

Just felt like typing something Grok?

Actually he was trying to show me about what i should not do. Well that's what i would say anyway!
Title: Re:My First c++ application
Post by: Grok on August 23, 2004, 09:03 AM
Quote from: Kp on August 21, 2004, 08:18 PM
Quote from: Grok on August 21, 2004, 06:39 PM"You code is bad"
"Your code is worse"

such posts that are not followed by constructive suggestions will be held against the person who posted it.

Is it considered constructive to inform the original author that his bad code is an inherent fault of the language he has chosen to use, and that he should therefore consider switching immediately to a superior language? :P

Absolutely.  Meanwhile, you could suggest how to do it anyway in the language he has chosen.  There may be other reasons why someone is stuck with a particular language for the project, so just telling them to switch is not necessarily constructive.
Title: Re:My First c++ application
Post by: Grok on August 23, 2004, 09:05 AM
Quote from: Maddox on August 23, 2004, 04:07 AM
Quote from: Grok on August 23, 2004, 03:42 AM
Quote from: $t0rm on August 22, 2004, 02:35 PM
sargera is the definition of a troll.

The above quote is the definition of a personal attack.  A name, a label, an intent, and no accompanying on-topic material to cleverly hide that intent.

Just felt like typing something Grok?

I suggest you do not get into a habit of being a smartass about my moderating.
Title: Re:My First c++ application
Post by: Banana fanna fo fanna on August 23, 2004, 01:03 PM
/me stops the personal attacks.
Title: Re:My First c++ application
Post by: MyndFyre on August 23, 2004, 01:46 PM
Quote from: $t0rm on August 23, 2004, 01:03 PM
/me stops the personal attacks.

$t0rm, you stink!
Title: Re:My First c++ application
Post by: Banana fanna fo fanna on August 23, 2004, 02:30 PM
Quote from: MyndFyre on August 23, 2004, 01:46 PM
Quote from: $t0rm on August 23, 2004, 01:03 PM
/me stops the personal attacks.

$t0rm, you stink!

myndfyre, youre a microsoft sellout. :-P
Title: Re:My First c++ application
Post by: BaDDBLooD on August 23, 2004, 03:48 PM
Quote from: $t0rm on August 23, 2004, 02:30 PM
Quote from: MyndFyre on August 23, 2004, 01:46 PM
Quote from: $t0rm on August 23, 2004, 01:03 PM
/me stops the personal attacks.

$t0rm, you stink!

myndfyre, youre a microsoft sellout. :-P

Quote

* $t0rm stops the personal attacks.


rrrrrrriiiiiiggggghhhhhttttt........
Title: Re:My First c++ application
Post by: MyndFyre on August 23, 2004, 05:14 PM
Quote from: $t0rm on August 23, 2004, 02:30 PM
Quote from: MyndFyre on August 23, 2004, 01:46 PM
Quote from: $t0rm on August 23, 2004, 01:03 PM
/me stops the personal attacks.

$t0rm, you stink!

myndfyre, youre a microsoft sellout. :-P

Can you blame me?  I can't find a wireless network card that works with Linux.  When I tried installing Linux today on my PC, it came close to blowing up (I can't get any kind of GUI whatsoever).
Title: Re:My First c++ application
Post by: Adron on August 23, 2004, 06:17 PM
Quote from: MyndFyre on August 23, 2004, 05:14 PM
Can you blame me?  I can't find a wireless network card that works with Linux.  When I tried installing Linux today on my PC, it came close to blowing up (I can't get any kind of GUI whatsoever).

Did you check this page (http://www.hpl.hp.com/personal/Jean_Tourrilhes/Linux/)?
Title: Re:My First c++ application
Post by: Newby on August 23, 2004, 06:51 PM
Quote from: $t0rm on August 23, 2004, 02:30 PM
Quote from: MyndFyre on August 23, 2004, 01:46 PM
Quote from: $t0rm on August 23, 2004, 01:03 PM
/me stops the personal attacks.

$t0rm, you stink!

myndfyre, youre a microsoft sellout. :-P
Ooh, BURN.
Title: Re:My First c++ application
Post by: Grok on August 23, 2004, 07:35 PM
He apparently wants to make lots of money and have a good life, big house, giant yard, wife with big titties and great legs, a home theatre, pool, etc.

I know this to be true because otherwise he would be using Linux.
Title: Re:My First c++ application
Post by: Banana fanna fo fanna on August 23, 2004, 10:32 PM
Grok, you know me, I'm anti-Linux for the desktop, and I think ASP.NET is cool.
Title: Re:My First c++ application
Post by: AntiVirus on September 22, 2004, 05:16 PM
#include <iostream.h>

int main()
{

  int choice;
  float number1;
  float number2;
  char indicator;

  cout << "1) Addition" << endl
      << "2) Subtraction" << endl
       << "3) Multiplication" << endl
       << "4) Division" << endl;

  do
  {
     cout << "Enter First Digit: ";
     cin >> number1;

     cout << "Enter Second Digit: ";
     cin >> number2;

     cout << "Choose Operator: ";
     cin >> choice;

     switch(choice)
     {
        case 1: cout << endl << "The answer is: " << number1 + number2 << endl;                  
              break;
        case 2: cout << endl << "The answer is: " << number1 - number2 << endl;                  
              break;
        case 3: cout << endl << "The answer is: " << number1 * number2 << endl;                  
              break;
        case 4: cout << endl << "The answer is: " << number1 / number2 << endl;                  
              break;
        default: cout << endl <<"You chose invalid selection" << endl;
     }

     cout << endl << "Do you want to enter another (y or n)? ";  
     cin >> indicator;
     cout << endl;
  } while((indicator == 'Y' || indicator == 'y'));

 return 0;
}


I tried this program out and I noticed that when you divide a smaller number by a bigger number you get 0.  I then changed it to "float number1" and "float number2" and I got a more correct result.  I don't know if this has been mentioned, but that is just something I found.  Even thought this is an old post, you still may care.
Title: Re:My First c++ application
Post by: MyndFyre on September 22, 2004, 07:04 PM
Quote from: iago on August 21, 2004, 02:17 PM
<edit> Don't take any of his advice in that thread, please.  It's not very good.

My discussion of not recursing the main() function is good!   :o

Quote from: Brandon on September 22, 2004, 05:16 PM
I tried this program out and I noticed that when you divide a smaller number by a bigger number you get 0.  I then changed it to "float number1" and "float number2" and I got a more correct result.  I don't know if this has been mentioned, but that is just something I found.  Even thought this is an old post, you still may care.
It depends on the kind of precision the project requires.  When working with floats, you tend to incur an exponentially larger delay in calculation processing.  That is, floating-point arithmetic is SLOW SLOW SLOW compared to integer arithmetic.
Title: Re:My First c++ application
Post by: Mephisto on September 22, 2004, 11:09 PM
Quote from: MyndFyre on September 22, 2004, 07:04 PM
Quote from: iago on August 21, 2004, 02:17 PM
<edit> Don't take any of his advice in that thread, please.  It's not very good.

My discussion of not recursing the main() function is good!   :o

Quote from: Brandon on September 22, 2004, 05:16 PM
I tried this program out and I noticed that when you divide a smaller number by a bigger number you get 0.  I then changed it to "float number1" and "float number2" and I got a more correct result.  I don't know if this has been mentioned, but that is just something I found.  Even thought this is an old post, you still may care.
It depends on the kind of precision the project requires.  When working with floats, you tend to incur an exponentially larger delay in calculation processing.  That is, floating-point arithmetic is SLOW SLOW SLOW compared to integer arithmetic.

For a simple calculator application, it shouldn't really matter.  :)
Title: Re:My First c++ application
Post by: Eibro on September 23, 2004, 07:39 AM
QuoteIt depends on the kind of precision the project requires.  When working with floats, you tend to incur an exponentially larger delay in calculation processing.  That is, floating-point arithmetic is SLOW SLOW SLOW compared to integer arithmetic.
That is incorrect. Perhaps on older processors this was true, but certainly not anymore.
Title: Re:My First c++ application
Post by: MyndFyre on September 23, 2004, 08:09 PM
Quote from: Eibro[yL] on September 23, 2004, 07:39 AM
QuoteIt depends on the kind of precision the project requires.  When working with floats, you tend to incur an exponentially larger delay in calculation processing.  That is, floating-point arithmetic is SLOW SLOW SLOW compared to integer arithmetic.
That is incorrect. Perhaps on older processors this was true, but certainly not anymore.

You're going to tell me that dividing two 8-byte double-precision numbers is the same speed as integer division?  Not on your life!  One division might not matter, but several million would!
Title: Re:My First c++ application
Post by: Eibro on September 23, 2004, 09:09 PM
Quote from: MyndFyre on September 23, 2004, 08:09 PM
Quote from: Eibro[yL] on September 23, 2004, 07:39 AM
QuoteIt depends on the kind of precision the project requires.  When working with floats, you tend to incur an exponentially larger delay in calculation processing.  That is, floating-point arithmetic is SLOW SLOW SLOW compared to integer arithmetic.
That is incorrect. Perhaps on older processors this was true, but certainly not anymore.

You're going to tell me that dividing two 8-byte double-precision numbers is the same speed as integer division?  Not on your life!  One division might not matter, but several million would!
It's a comparable speed. Then again, it depends on the processor. On some, floating point arithmetic can actually be faster. In any case, it does not "tend to incur an exponentially larger delay", as you claim.
Title: Re:My First c++ application
Post by: Mephisto on September 24, 2004, 09:18 AM
In VS.NET 2003 aren't there performance options to enhance performance for integer/floating-point arithmetic?  And isn't that implied on optimizations for Pentium 4/AMD processors; I saw an article about it from Microsoft explaining that that particular optimization enhances speed for artihmetic for those processors by using different instructions.