|
Post by Nicky Peter Hollyoake on Mar 21, 2008 13:30:43 GMT -5
Ok, i've been practicing 'C++' i've downloaded codeblocks because Dev-C++ kept giving me errors (is it full of bugs or something?). Anyways I just need some advise on this code (its my first code i've actually completed).
#include <iostream.h>
int main(){
int a = 1, b = 2; const char *n = "Number:";
cout << n + (a + b);
return 1; }
It brings up the black screen and then I wanted it to show "Number: 3" but itstead it just shows "ber:" anyone know why?
I wanna know if someone could explain "const char" does const char mean if your using more then one character? if your usign 1 chracter you can just put "char"?
[glow=red,2,300]Nicky[/glow]
|
|
|
Post by matthew on Mar 21, 2008 14:52:34 GMT -5
#include <iostream>
int main(void) { int a = 1; int b = 2; const char *n = "Number: ";
// Display text & result of sum std::cout << n << a + b << std::endl;
// Wait for a key to be pressed std::cin.get();
return 0; }
Edit
As you're writing a C++ program you shouldn't really be using char* or const char*, so I've written the routine using the string command.
#include <iostream> #include <string>
int main(void) { int a = 1; int b = 2; std::string n("Number: ");
// Display text & result of sum std::cout << n << a + b << std::endl;
// Wait for a key to be pressed std::cin.get();
return 0; }
|
|
|
Post by Empyrion Martyr on Mar 21, 2008 17:56:07 GMT -5
knowing the whereabouts of char* and const's of all types is mandatory even for c++ programmers (at least that's what i think) I think you should first learn how to handle them in all possible ways and then move on to the string container. A lot gets lost in handling vectors and pointers if you don't do that at the beginning.
and try to use the "using namespace std;" to shorten the code.
|
|
|
Post by matthew on Mar 21, 2008 18:33:00 GMT -5
|
|
|
Post by Tom Mulgrew on Mar 21, 2008 21:54:28 GMT -5
As a C++/C# programmer I have to say I strongly disagree with that particular part of that FAQ. If you use a lot of functions/classes in a library (and who doesn't use the C++ standard library in C++?) then "using namespace" is a completely sensible thing to do. Just my 2c.
|
|
|
Post by matthew on Mar 21, 2008 23:36:47 GMT -5
I think experienced C++ coders probably use namespace std because they're smart enough to avoid naming conflicts.  I suppose the FAQ is aimed at people who have just started learning the language & is probably designed to give them the best advice.
|
|
|
Post by Nicky Peter Hollyoake on Mar 22, 2008 19:01:48 GMT -5
I'm guessing "include <string>" lets you use "string"?
What does namespace do? I would like to know what its doing if i'm gonna use it.
does "void" have to be in "int main()"?
And how do you use functions in it? I know it goes something like ...
void swap(a, b){ int c = a; a = b; b = c; }
But how would I use it after?
|
|
|
Post by Empyrion Martyr on Mar 22, 2008 19:34:45 GMT -5
using GCC, the default compiler of codeblocks (as i recall) "#include <string>" indeed lets you use the string container which has several useful methods including size, resize, find and many others on top of which you can easily build the B4gel styled functions like left$() or mid$(), plus it's safer to use.
namespaces are groups of functions, templates, basically any type of code. Take this for example:
namespace alpha { int a,b; string theta("initialized value") class ........... }
or
namespace beta { int a,b; float c; .... etc }
then accessing those things declared would be done thru the scoping operator, as in alpha::a, alpha::b, alpha::theta, alpha::class1.method1(param1) etc Several differently named namespaces can hold identically named vars, classes etc. for example alpha::class1 and beta::class1 would reffer to 2 classes from different namespaces.
the "using namespace alpha;" makes it possible to acces the data without writing the "alpha::", for example a,b,theta,class1.method1(etc) but still beta::a, beta::b you get the idea. In case you also write "using namespace beta;" you get naming conflicts (yeah, there are STL namespaces like std and that's why the faq says avoid namespacing)
you can have namespaces inside namespaces etc you get the point
------
third point, a void function like the one you wrote is called simply by it's name.. example
cout<<"a="<<a<<"\nb="<<b<<endl; swap(a,b); cout<<"now a="<<a<<"\nb="<<b<<endl;
if your function returns a value treat it as a variable. void simply means no type. basically a void function is a procedure, the same as the "sub" in b4gel. Be wary that (void*) is not actually "void" or empty, and such functions CAN return almost ANY type of data, which you can later cast to properly use.
in GCC you should be writing "int main()" instead of the old "void main()" and the two are mutually exclusive.
|
|
|
Post by Nicky Peter Hollyoake on Mar 22, 2008 19:55:27 GMT -5
Thanks.
Alright I made my "sub" but something seems to be going wwrong, can't figure it out, help?
#include <iostream.h>
void swap(int*a, int*b) {
int c = *a;
a = b; b = a; }
int main() {
int a = 1, b = 2;
cout << "A: " << a << " B:" << b; swap(a, b); cout << "A: " << a << " B:" << b; }
and how would you go down to the next line to? So the results all don't go on one line.
Whats the oistream to? I just know its important, thats all i've learned about it, lol.
Tom:
Will you allow function overloading in Basic4GL? I think thats what its called.
[glow=red,2,300]Nicky[/glow]
|
|
|
Post by Nicky Peter Hollyoake on Mar 23, 2008 14:30:47 GMT -5
I worked out functions, now I just figuring out while (I know I still got alot to learn, taking it one step at the time though  #include <iostream.h>
int lose = false; int a = 0; int main() {
while (lose = false) { a = a + 1; cout << a; }
} I want an infinity loop. I thought that would work, but guess not? How does the for loop go too? For (int i; i < 10; i++) { }  How does that work? First word is the integer? second is if i less then 10 keep looping? and three add 1 on to i? [glow=red,2,300]Nicky[/glow] EDIT: Ok I got it. #include <iostream.h>
int times(int var1, int var2) { return (var1 * var2); }
int lose = false; int a = 0;
int main() {
while (lose = true) { a++; cout << times(a, a) << "\n";
if (a > 10) { lose = true; }
}
} But now the "if" statement won't work, I want it to stop when a is past 10. I'm just conbining everything I know in this one if your interested why the function. I would like if someone can still give me a better explnation of how the for is worked. Could someone edit my code, show how I could make it better, and sort my little "if" problem out? [glow=red,2,300]Nicky[/glow]
|
|
|
Post by matthew on Mar 23, 2008 15:41:48 GMT -5
Try this, does it do what you wanted?
#include <iostream>
// Function prototype int times(int, int);
int main() { int lose = false; int a = 0;
while (lose == false) { std::cout << times(a, a) << std::endl;
a++;
if (a > 10) { lose = true; }
}
// Wait for a key to be pressed std::cin.get();
}
int times(int var1, int var2) { return (var1 * var2); }
|
|
|
Post by Empyrion Martyr on Mar 23, 2008 18:16:15 GMT -5
yeah nicky you wrote "while (lose = true)" when it shoulda been "while (lose == false)" as pointed by mathew.
the == being written as = is a common mistake but i think GCC gives out a warning that the stament might be incorrect.
I'm not sure what you mean when you say you want an infinity loop but these are a couple of ways to do it
while(TRUE) {}
and "for(;TRUE!=0;) {}" basically it's: for ( initialization, execute_while_condition_is_true, expr_to_execute_each_step _ initialization and expression_to_exec can dissapear if needed use commas for multiples inits or expr example: "for ( n=0, i=100 ; n!=i ; n++, i-- )"
also, there is a shortcut to writing VARIABLE!=0 (or VARIABLE!=FALSE) inside conditions; it's simply writing VARIABLE: "while(a!=0)" or "if(szlensm!=false)" can get stripped to "while(a)" and "if(szlensm)" the iostream is a std stream (simpoly a class) derived from "istream" and "ostream" (input and output streams) they were designed to ease the input and output of data (in C you would use functions for that, not overloaded operators as in C++)
|
|
|
Post by Nicky Peter Hollyoake on Apr 8, 2008 13:12:11 GMT -5
They a better way of making a delay? like "Sleep(100)" in B4GL, this is my attempt.
#include <iostream.h>
/* subs */ void printr(int number){ cout << number; cout << "\n"; } /* sub end */
int main() {
int delay = 0;
while (true) { delay = 0; printr(rand() % 101);
while (delay < 100000000) { delay++; } } }
Also how do you get keyboard input?
By the way, i'm still working on namespace, just trying to figure out some things like "public" because I see you have to use that in them just can't figure out what for.
Nicky
|
|
|
Post by Empyrion Martyr on Apr 8, 2008 15:10:22 GMT -5
I think in a windows app that piece of code above would give an unresponsive program, plus, you don't have a unit of measure for the time to spend in the sleep stance. use "time.h", google search it on the net, also, keyboard input is done in several ways, the classic "cin>>" and by using "#include <conio.h>" (I asssume you are using borland? and then calling "getch()" and "gets()", also with "kbhit()" char p; p=getchar(); If you really wanna get confused with c and c++ i suggest you scour the net for obfuscation programs and quines 
|
|
|
Post by James :) (aka Madcow) on Apr 9, 2008 0:42:13 GMT -5
yeah nicky you wrote "while (lose = true)" when it shoulda been "while (lose == false)" as pointed by mathew. the == being written as = is a common mistake but i think GCC gives out a warning that the stament might be incorrect. it dosn't invalidate the code but what it dose is it sets the varible to that value and checks that it equals it but basicaly it will always equal true (in most lanugages anyway).
|
|