Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
First C++ project [Uh-oh]

 
Post new topic   Reply to topic Printable version
 View previous topic  Creating a ZIP archive and adding file... Post :: Post Writing a Windows Service  View next topic  
Author Message
BDwinsAlt
Agurus's Posse


Age:33
Gender:Gender:Male
Joined: Jun 16 2003
Posts: 1145
Location: Alabama
Offline

PostPosted: Mon Mar 20, 2006 8:55 pm    Post subject: First C++ project [Uh-oh] Reply to topic Reply with quote

***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.




Contains a jar and exe files.
use java -jar Circles.jar for the Circles.jar

Circles.zip - 93.96 KB
File downloaded or viewed 25 time(s)
Back to top
View users profile Send private message Add User to Ignore List Send email Visit posters website AIM Address Yahoo Messenger MSN Messenger
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Mon Mar 20, 2006 10:33 pm    Post subject: Reply to topic Reply with quote

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).
_________________
4,691 irradiated haggis!
Back to top
View users profile Send private message Add User to Ignore List
BDwinsAlt
Agurus's Posse


Age:33
Gender:Gender:Male
Joined: Jun 16 2003
Posts: 1145
Location: Alabama
Offline

PostPosted: Tue Mar 21, 2006 12:24 am    Post subject: Reply to topic Reply with quote

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;
   }
}

Back to top
View users profile Send private message Add User to Ignore List Send email Visit posters website AIM Address Yahoo Messenger MSN Messenger
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Tue Mar 21, 2006 2:05 am    Post subject: Reply to topic Reply with quote

No.
Back to top
View users profile Send private message Add User to Ignore List
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Tue Mar 21, 2006 2:09 am    Post subject: Reply to topic Reply with quote

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");
}
Back to top
View users profile Send private message Add User to Ignore List
BDwinsAlt
Agurus's Posse


Age:33
Gender:Gender:Male
Joined: Jun 16 2003
Posts: 1145
Location: Alabama
Offline

PostPosted: Tue Mar 21, 2006 11:40 am    Post subject: Reply to topic Reply with quote

Oh i see what you're saying now. Thanks man. Any ideas on when the new continuum will be out? :-P




angrydog.jpg - 2.13 KB
File downloaded or viewed 31 time(s)
Back to top
View users profile Send private message Add User to Ignore List Send email Visit posters website AIM Address Yahoo Messenger MSN Messenger
Cyan~Fire
I'll count you!
I'll count you!


Age:36
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Tue Mar 21, 2006 2:46 pm    Post subject: Reply to topic Reply with quote

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.)
_________________
This help is informational only. No representation is made or warranty given as to its content. User assumes all risk of use. Cyan~Fire assumes no responsibility for any loss or delay resulting from such use.
Wise men STILL seek Him.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
BDwinsAlt
Agurus's Posse


Age:33
Gender:Gender:Male
Joined: Jun 16 2003
Posts: 1145
Location: Alabama
Offline

PostPosted: Tue Mar 21, 2006 3:46 pm    Post subject: Reply to topic Reply with quote

That's why it is my first c++ project.
Trial and Error...
Back to top
View users profile Send private message Add User to Ignore List Send email Visit posters website AIM Address Yahoo Messenger MSN Messenger
Cyan~Fire
I'll count you!
I'll count you!


Age:36
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Tue Mar 21, 2006 4:00 pm    Post subject: Reply to topic Reply with quote

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?
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
BDwinsAlt
Agurus's Posse


Age:33
Gender:Gender:Male
Joined: Jun 16 2003
Posts: 1145
Location: Alabama
Offline

PostPosted: Tue Mar 21, 2006 4:03 pm    Post subject: Reply to topic Reply with quote

I haven't yet. I basically just went into Visual C++ and starting typing stuff :?
Back to top
View users profile Send private message Add User to Ignore List Send email Visit posters website AIM Address Yahoo Messenger MSN Messenger
Cyan~Fire
I'll count you!
I'll count you!


Age:36
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Tue Mar 21, 2006 5:49 pm    Post subject: Reply to topic Reply with quote

Right. Not a good idea. If you want to be a good programmer, learn by the book first.

Also, use stdio.h, not iostreams.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Dr Brain
Flip-flopping like a wind surfer


Age:38
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Tue Mar 21, 2006 7:00 pm    Post subject: Reply to topic Reply with quote

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.
_________________
Hyperspace Owner

Smong> so long as 99% deaths feel lame it will always be hyperspace to me
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Tue Mar 21, 2006 7:46 pm    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List
Dr Brain
Flip-flopping like a wind surfer


Age:38
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Tue Mar 21, 2006 9:36 pm    Post subject: Reply to topic Reply with quote

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.




printf.c - 0.19 KB
File downloaded or viewed 35 time(s)

iostream.C - 0.24 KB
File downloaded or viewed 38 time(s)
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Cyan~Fire
I'll count you!
I'll count you!


Age:36
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Tue Mar 21, 2006 10:22 pm    Post subject: Reply to topic Reply with quote

The iostream code was inside the executable (template class).
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Wed Mar 22, 2006 7:02 am    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List
Dr Brain
Flip-flopping like a wind surfer


Age:38
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Wed Mar 22, 2006 8:37 am    Post subject: Reply to topic Reply with quote

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.
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Non-Subspace Related Coding All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 668 page(s) served in previous 5 minutes.

phpBB Created this page in 0.482655 seconds : 44 queries executed (90.3%): GZIP compression disabled