plus, not times
Printable View
Lolfail :P
I had it in my program. I had defined PI to be 3 because I was lazy, I never did it exact because it was only for testing purposes. But yea, formula was: cirumfrence = PI * r * 2 and for radius I had r = circumference/(r*2).
All I did was let the "user" input the circumference if they wanted radius, or input radius if they wanted circumference.
I understand why you want to get into GUI stuff, but trust me, at this stage it'd just confuse you
You've got to get a proper handle (no pun intended) on the language before delving into external APIs
At present, you're doing really simple maths with C++
you've yet to grasp the more advanced concepts needed for GUI apps
We haven't even covered stuff outside main() yet
let alone classes, templates, memory allocation, STL, etc. etc.
Walk, then run
But right now, we're still bobing about in the sea, before we developed legs
:lol: I get it :D
Now lets do methods, this program how two methods, one for area, and one for circumference, and the user gets to decide which to use.
Code:#include <iostream>
#define PI 3.14159
float Circumference(float r){
return r * 2 * PI;
}
float Area(float r){
return r * r * PI;
}
int main(){
//Get the radius of the circle from the user
float radius;
std::cout << "Enter radius." << std::endl;
std::cin >> radius;
//Get the function wanted
std::cout << "\nPress 1 to Calculate Circumference.\nPress 2 to Calculate Area" << std::endl;
int option;
switch (option){ //kind of like a fast if / else
case 1: //do if option == 1
std::cout << Circumference(radius) << std::endl;
break;
case 2: //do if option == 2
std::cout << Area(radius) << std::endl;
break;
default: //neither of the other options
std::cout << "Error, you didn't input one or two" << std::endl;
}
return 0;
}
Alright.
Whhat should I learn next, then? Youtube lessons are not my friend.
what is the std::--- for?
Why not just cout, int etc.
Hmm... how to describe how namespaces work :? That's not important right now (and an odd concept before learning classes,) you can use cout simply and add a using namespace std at the top of the program.
Basically, there are multiple couts and the compiler has to know which one to use. std:: tells it to use the std one, you did that in your program by defining that the entire program uses std. Here's a little demonstration of namespaces.
Weird, I know, but very useful for more advanced programming, for now you can forget what I just said :PCode:#include <iostream>
using namespace std;
namespace Integer{
int var = 5;
}
namespace Decimal{
double var = 3.1416;
}
int main () {
cout << Integer::var << endl;
cout << Decimal::var << endl;
return 0;
}
Why isn't this working? It says I need } at the end, but I did...
Code:#include <iostream>
using namespace std;
int main ()
{
int x = 3;
int y = 4;
if (x > y)
{
cout << "X is greater then Y " << endl;
}
else
{cout << "X is less then Y " << endl;
system("PAUSE");
return 0;
}
Those are methods. That's how you do things multiple times, you can keep calling that as many times as you want. Everything is done like this, even << in cout is kind of a method.
\n is just a newline character, it's the same as endl, but can go inside of the quotes, I just did it to save space.
You have a stray bracket before the last cout, you never close it. :)
Well, namspaces do use the same delimiting syntax as class methods, so I think an understanding is helpful
Namespaces
Note the #include line,Code:#include <iostream>
using namespace std;
int main()
{
cout << "hello world" << endl;
return 0;
}
we're pulling in external code
specifically, code to allow input / output of data using streams
There's loads of external code you can pull into your program to acheive common tasks
because of this, it's important to separate code into "namespaces"
Say we are making a program that tracks finances of a company
We could define a couple of classes in our program to track money incoming and outgoing from our company
We may call these classes "cin" for cash in, and "cout" for cash out
but we'd have a problem - these names clash with the names defined in iostream for input & output to the standard streams
For this reason, any "standard" external code (Ie. comes with all compilers and is expected to be present) is separated off into it's own namespace, called "std"
This brings us to the second line
using namespace std;
is asking the compiler, should it come across something not defined in our program's namespace, to look in the standard namespace for it
You can omit the "using namespace std" line, and append the std namespace to object calls
The above can be re-written to this
So, namespacesCode:#include <iostream>
/*
* we don't need this line anymore
* as we're using absolute referencing
* for std objects
*/
// using namespace std;
int main()
{
std::cout << "hello world" << std::endl;
return 0;
}
just a way to separate out definitions so code from different sources don't clash with each other
\n doesn't flush the stream
\n is just a character, and therefore cannot "do" anything
Ynot's right, \n does not flush the stream.
This is why \n is useful for inside of a string, it allows you to keep buffering it instead of having to wait for the buffer to be flushed.
Well, SOMETHING is flushing the stream, because when I run:
I get "Thingger1" displaying immediately, then it pauses for a second, and then Thingger2 displays. This has consistently been my experience for years and years. If it didn't flush the stream, it would have paused a second and then displayed both lines at the same time. So, what's happening here if not flushing? It might be the lower level stream operations that happen under the hood, but you can't deny this behaviour. :)Code:#include <iostream>
#include <unistd.h>
using namespace std;
int main(void)
{
cout << "Thingger1\n";
sleep(1);
cout << "Thingger2" << endl;
return 0;
}
EDIT to add: Did some googling, and it seems that this is dependent on the compiler. I suspect cout is special in that behaviour, and other streams (fstream, etc.) don't do that. Back in the C days, it was the same story with '\n' and printf.
\n isn't flushing the stream, in most compilers, the stdout streams aren't buffered because there is boatloads of memory and speed to burn. But you're not guaranteed to get that result all the time. Try it with file i/o and see what you get :P
Just remember there is a difference between what a compiler will do and what the C++ Standard states.
Case and point:
The C++ Standard states that sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long), but it's perfectly legal for them to even be the same size.Code:if ((sizeof(char) < sizeof(short)) &&
(sizeof(short) < sizeof(int)) &&
(sizeof(int) < sizeof(long))){
cout << "Expected" << endl;
} else {
cout << "Reality on most compilers" << endl;
}
I have a question.
In this program, when you input two words, (i.e. first name and second name) it only takes the first word. I read somewhere in the tutorials of a data type that takes in any amount of words, rather than one. But I can't seem to find it. In fact, I'm not even sure if it was to do with data types or not.
But anyways, yea, how would I allow for two words to be input?
Code:#include <iostream>
#include <sstream>
using namespace std;
int main()
{
string question = "What is your name? ";
string greeting = "Hello, ";
string yourname;
cout << question << endl;
cin >> yourname;
cout << greeting << yourname << endl;
system ("PAUSE");
return 0;
}
standard streams are whitespace delimited (unix convention)
use getline() to get an entire line (up to newline char)
btw, what's this system("PAUSE") thing?Code:#include <iostream>
#include <sstream>
using namespace std;
int main()
{
string question = "What is your name? ";
string greeting = "Hello, ";
string yourname;
cout << question << endl;
getline(cin, yourname);
cout << greeting << yourname << endl;
system ("PAUSE");
return 0;
}
Oohhhhhhhh... Thanks.
system("PAUSE") means that when I run the program, it doesn't close as soon as it finishes. But it lets you close it by pressing any key.
Within the programing software stuff it isn't an issue, but when it is the .exe on it's own, when you run it, it would close the moment you finish inputing stuff.
Code::Blocks automatically pauses the execution at the end for you, you don't need that unless compiling or running outside of the IDE :)
How to I package together my code into an exe or something both windows and macs can open?