Server Help

Non-Subspace Related Coding - First C++ project [Uh-oh]

BDwinsAlt - Mon Mar 20, 2006 8:55 pm
Post subject: First C++ project [Uh-oh]
***Note: Code appears sloppy on the forum.***


Aside from biller devolpment (well anyone can do that), i decided to make this VERY simple console application. I also made one like it in java. :p

I guess this means I can say I ported a C++ project to Java.

Anways here's the C++ Code for the Area and Circumference of a circle. Feel free to give any advice (nicely) [ Think i have enough comments? :-P ]

Circles.cpp
Code: Show/Hide

#include <iostream>                  // This includes some file we need.
#include <string>                  // This includes some file we need.
using namespace std;               // This says we're using namespace std [ I think :X ].
int main() {                     // This starts the main program.

   system("Color 17");               // Turns the background blue[1] and the font white[7].
   double radius;                  // This instalizes the radius input.
   string response;               // This instalizes the response string.
   cout << "What is the radius? ";     // Prints "What is the radius?".
   cin >> radius;                  // This inputs the radius.

   cout <<  "\nCircumference = " << (radius)*(3.14)*(2) << endl;   // This sends out the circumference.
   cout << "Area = " << (radius)*(radius)*(3.14) << endl;            // This sends out the area.
   cout << "\nWould you like to do another one? [Y/N]" << endl; // Asks you if you want to do it again.
   cin >> response;    // Input for Yes or No.
   
   if(response == "Y")    // If user types "Y" they are sent to repeat what just happened.
   {
      cout << "\n" << endl;
      main();  // Returns to what went on previously.
   }

   if(response == "y")   // If user types "y" [lower-case 'y'] they are sent to repeat what just happened.
   {
      cout << "\n" << endl;
      main();  // Returns to what went on previously.
   }

   if(response == "N")    // If user types "N" the program exits.
   {
      exit(0);  // Exits the program.
   }

    if(response == "n")    // If user types "n" [lower-case 'n'] the program exits.
   {
      exit(0);  // Exits the program.
   }

   else   // If neither Y(y) or N(n) is entered after radius.
   {
        system("cls");  // Clears the whole prompt screen.
        main();         // Returns to starting point.
   }

}



Now for the Java [LOVE <3]

Circles.java
Code: Show/Hide

/*
This is open source to all.
This is a simple way to get the circumerence of a circle and the area.
It is pretty basic and pointless.
*/

import java.io.*;
import java.util.*;
 
   public class Circles{
   public Circles(){}

   public static void main(String [] args){

    try {
System.out.println( "What is the radius?  " );
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String str = "";
        while (str != null) {
            str = in.readLine();
System.out.println( "\n Circumference = " + ( Double.valueOf(str) )*(2)*(3.14) );
System.out.println( " Area = " + ( Double.valueOf(str) )*( Double.valueOf(str) )*(3.14) );
System.out.println( "\nWhat is the radius?" );
}
    } catch (Exception e) {
    }

}
}


I know this is pretty useless and you can do it easily on a calculator.
I'll explain this anyways. What you do for the Circles.exe is:

1. Double click Circles.exe
2. Type in the radius [Prompt: What is the Radius?]
3. The Circumference and Area is displayed.
4. Enter Y or N [Prompt: Would you like to do another one? [Y/N]]
5. Send BDwinsAlt money on Paypal.


For the jar.

1. Open command prompt and make the directory your jdk<version>\bin folder.
2. Type java -jar <path to Circles.jar>/Circles.jar
3. Type in the radius [Prompt: What is the Radius?]
4. Repeat as long as desired.
5. Give BDwinsAlt a big fat kiss.
Mr Ekted - Mon Mar 20, 2006 10:33 pm
Post subject:
Don't call main() from inside main(). Either use some kind of looping construct (eg while) of use goto (which is fine IMO in this situation).
BDwinsAlt - Tue Mar 21, 2006 12:24 am
Post subject:
Thank you :lol:

This better? (no comments added this time)

Code: Show/Hide

#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

int main() {

   system("Color 17");
   double radius;
   string response;
   int i;

   for( i = 0; i < 10; i++ )
   {
      goto mainf;
   }

   mainf:
      cout << "What is the radius? ";
      cin >> radius;
      cout <<  "\nCircumference = " << (radius)*(3.14)*(2) << endl;
      cout << "Area = " << (radius)*(radius)*(3.14) << endl;
      cout << "\nWould you like to do another one? [Y/N]" << endl;
      cin >> response;

   if(response == "Y")
   {
      cout << "\n" << endl;
      goto mainf;
   }

   if(response == "y")
   {
      cout << "\n" << endl;
      goto mainf;
   }

   if(response == "N")
   {
      exit(0);
   }

    if(response == "n")
   {
      exit(0);
   }

   else
   {
        system("cls");
      goto mainf;
   }
}


Mr Ekted - Tue Mar 21, 2006 2:05 am
Post subject:
No.
Mr Ekted - Tue Mar 21, 2006 2:09 am
Post subject:
Code: Show/Hide
#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

int main()
{
double radius;
string response;
int    i;

system("Color 17");

do
   {
   cout << "What is the radius? ";
   cin  >> radius;
   cout << "\nCircumference = " << (radius * 3.14 * 2) << endl;
   cout << "Area = " << (radius * radius * 3.14) << endl;
   cout << "\nWould you like to do another one? [Y/N]" << endl;
   cin  >> response;
   }
while (response == "y" || response == "Y");
}

BDwinsAlt - Tue Mar 21, 2006 11:40 am
Post subject:
Oh i see what you're saying now. Thanks man. Any ideas on when the new continuum will be out? :-P

Cyan~Fire - Tue Mar 21, 2006 2:46 pm
Post subject:
You are proof that "Aside from biller devolpment (well anyone can do that)" is not true.

Do you have any concept of how computer programming works? You seem to have no idea how procedural programming works, and that's the basis of everything. (Yes, you even need to know procedural to do Java.)
BDwinsAlt - Tue Mar 21, 2006 3:46 pm
Post subject:
That's why it is my first c++ project.
Trial and Error...
Cyan~Fire - Tue Mar 21, 2006 4:00 pm
Post subject:
Some of programming is trial and error, but not to the extent you're doing. I don't even know how you pieced that together without reading some sort of tutorial, which you obviously didn't. Have you tried cplusplus.com?
BDwinsAlt - Tue Mar 21, 2006 4:03 pm
Post subject:
I haven't yet. I basically just went into Visual C++ and starting typing stuff :?
Cyan~Fire - Tue Mar 21, 2006 5:49 pm
Post subject:
Right. Not a good idea. If you want to be a good programmer, learn by the book first.

Also, use stdio.h, not iostreams.
Dr Brain - Tue Mar 21, 2006 7:00 pm
Post subject:
Cyan~Fire wrote:
Also, use stdio.h, not iostreams.


stdio.h isn't even a valid C++ system header name. Oh wait, you mean C...

iostream has its place. There's a reason every C++ book uses it.
Mr Ekted - Tue Mar 21, 2006 7:46 pm
Post subject:
Dr Brain wrote:
stdio.h isn't even a valid C++ system header name. Oh wait, you mean C...

iostream has its place. There's a reason every C++ book uses it.


Ya so newbs who are learning don't have to learn everything at once. Once you get beyond the "I'm a stupid newb" stage, if you still think iostream is a good thing, then you will always be a newb.
Dr Brain - Tue Mar 21, 2006 9:36 pm
Post subject:
I decided to check out your printf zealotry. Your previous statements about cout calling printf are total bull. In fact, because cout knows exaclty what the type is, in general, it preforms faster than printf.

Here are benchmarks I just preformed on my 300MHz linux computer. I optimized both of the with -O3, and ran them several times, picking the fastest run. I used the linux time command to generate the results.

Code: Show/Hide

iostream optimized run:
real    0m0.262s
user    0m0.053s
sys     0m0.096s

printf optimized run:
real    0m0.341s
user    0m0.081s
sys     0m0.155s


Source code for both test is attached.

Conclution: iostream, in general, performs better than stdio.
Cyan~Fire - Tue Mar 21, 2006 10:22 pm
Post subject:
The iostream code was inside the executable (template class).
Mr Ekted - Wed Mar 22, 2006 7:02 am
Post subject:
Dr Brain wrote:
I decided to check out your printf zealotry. Your previous statements about cout calling printf are total bull. In fact, because cout knows exaclty what the type is, in general, it preforms faster than printf.


Trace though the source code for a simple "cout << "hello";" call sometime. It's a freaking nightmare. But you are off the mark.

One, it's not that iostreams directly calls printf, it's that iostreams is about 4 levels of indirection avove the OS, while the printf stuff is 1. I hate wrappers. Part of coding is 1) understanding how things work, etc. take a look at the nightmare code in iostreams. And 2) being able to debug when something is broken. You may have a bug which overwrites something that causes a crash in the middle of the iostreams processing. Good luck with that.

Two, real apps (the kind which we usually talk about here, map editors, etc) don't use console input/output. It's reserved for silly test utilities while you are learning to code. The real issue is formatting buffers.

Three, where user interaction is concerned, a few milliseconds is nothing. Write an test app that uses heavy string processing over millions of records. Working in a buffer will kick any wrapper's butt.
Dr Brain - Wed Mar 22, 2006 8:37 am
Post subject:
Those "wrappers" are all inline, and once you compile it in some kind of release mode, they perform just fine.

Of course console I/O isn't used for real apps, but file streams and buffer streams work just fine and have the same advantage over fprintf and sprintf.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group