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. } } |
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) { } } } |
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; } } |
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"); } |
Cyan~Fire wrote: |
Also, use stdio.h, not iostreams. |
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. |
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 |
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. |