Getting started in C++ - WARNING: may contain noob questions

Getting started in C++ - WARNING: may contain noob questions

Mate de Vita
Kelli

2008 Oct 4 • 2453
159 ₧
Down Rodeo said:
For some reason I just triple posted. I didn't think that was possible. Ah well.

You did?
...and that's the bottom line because Mate de Vita said so.
 
 
 
2009 Aug 19 at 20:54 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
 
 
 
2009 Aug 20 at 09:51 UTC
Mate de Vita
Kelli

2008 Oct 4 • 2453
159 ₧
How do I compile a program I wrote with Code::Blocks in the terminal (using gcc)? If I write gcc test.c nothing happens.
...and that's the bottom line because Mate de Vita said so.
 
 
 
2009 Aug 20 at 11:08 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
Check your home folder. There should be a file called "a.out" there which you can run with the terminal. Uh, there should also be a green arrow at the top of the screen, unless I'm thinking of another IDE.

So, tell us, what does it do? :D

Also you can specify an output name to gcc with the -o option, e.g.
gcc -o /home/mate/Programs/C/yourmother.runnable test.c

I think that works. Certainly the -o option does; I think that's how you specify a path.

And when you say, nothing happens, does it error at you, or just pull up another terminal line?
 
 
 
2009 Aug 20 at 18:23 UTC — Ed. 2009 Aug 20 at 18:24 UTC
Mate de Vita
Kelli

2008 Oct 4 • 2453
159 ₧
Down Rodeo said:
Check your home folder. There should be a file called "a.out" there which you can run with the terminal. Uh, there should also be a green arrow at the top of the screen, unless I'm thinking of another IDE.

K, will try that when I get to my linux computer.

Down Rodeo said:
So, tell us, what does it do? :D

This one is actually just the default start up script, which is int main() { printf("Hello World!\n"); return 0; }

Down Rodeo said:
And when you say, nothing happens, does it error at you, or just pull up another terminal line?

The latter, just pulls up another terminal line.
...and that's the bottom line because Mate de Vita said so.
 
 
 
2009 Aug 20 at 20:47 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
 
 
 
2009 Aug 21 at 00:41 UTC
Mate de Vita
Kelli

2008 Oct 4 • 2453
159 ₧
How exactly do I run the a.out? If I go to that directory and just write a.out it says command not found.
...and that's the bottom line because Mate de Vita said so.
 
 
 
2009 Aug 21 at 12:30 UTC
jrkookid
Half Irish, half NOT FUNNY

2007 May 27 • 1415
110 ₧
Mate de Vita said:
#include <iscloudretarded>


 
 
 
2009 Aug 21 at 16:34 UTC
Mate de Vita
Kelli

2008 Oct 4 • 2453
159 ₧
jrkookid said:
Mate de Vita said:
#include <iscloudretarded>



Yes kookid, I think you've already shown to us that you are slow in the general section. Actually you more pointed it out but w/e. But what in the name of are you doing here? I wasn't aware of the fact that you either need or could offer any C/C++ help.
...and that's the bottom line because Mate de Vita said so.
 
 
 
2009 Aug 21 at 19:45 UTC — Ed. 2009 Aug 21 at 19:46 UTC
jrkookid
Half Irish, half NOT FUNNY

2007 May 27 • 1415
110 ₧
Actually.... I was just looking around and noticed the funny code that you entered. I don't know much about C/C++ actually, I used to know some coding in VB6
 
 
 
2009 Aug 21 at 20:13 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
Ah, Visual basic. It makes life so easy!

As for the question a few bits back, you need to type ./a.out. Time for explanations: When you try to run a program, the terminal will look in a few places, like /usr/bin/ and various other places. To tell it that you want something in the directory you're in you need to type '.', period meaning "the directory I am currently in". The slash is obviously what denotes the next section of the command, this being a.out. Tsimple! This means you can go "cd ././././Desktop" if you are navigating from your home directory and it will be the same as "cd Desktop". Also two dots means "the directory above this one", so you get things like "cd ../Desktop/../Desktop" or whatever. You can also run programs stored in one directory in another by cd'ing to, say, Desktop/PlaceIWantToRun then (assuming the program a.out is in your home folder) typing in ../../a.out. It's easy!
 
 
 
2009 Aug 22 at 00:39 UTC
Mate de Vita
Kelli

2008 Oct 4 • 2453
159 ₧
Damn, this tutorial sucks. It just tells you all the time, yes you do this like this: example, but we'll explain why this is so later.
Do you maybe have something better for C?

The test program works fine though
...and that's the bottom line because Mate de Vita said so.
 
 
 
2009 Aug 22 at 12:55 UTC — Ed. 2009 Aug 22 at 12:56 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
Which tutorial's that? Uh, I started at C++ then looked at C because many things are similar. By that stage I knew what a lot of things did so I was able to look up very specific bits of C if I didn't know how to do something.

I found a C tutorial that tells you to type void main() which is retarded in SO MANY WAYS argh and that's the number one search result on Google, don't use that one. It might be instructive to think about the things you want to do then try to find small pages on those. Like, "how do I do pointers?" Or you could ask me for a botched job that is more personalised, it's up to you :p
 
 
 
2009 Aug 22 at 13:52 UTC
Mate de Vita
Kelli

2008 Oct 4 • 2453
159 ₧
Down Rodeo said:
Which tutorial's that? Uh, I started at C++ then looked at C because many things are similar. By that stage I knew what a lot of things did so I was able to look up very specific bits of C if I didn't know how to do something.

It was something I found googling.

Down Rodeo said:
I found a C tutorial that tells you to type void main() which is retarded in SO MANY WAYS argh and that's the number one search result on Google, don't use that one.

So what exactly is the difference? Between void main() and int main()?
Down Rodeo said:
It might be instructive to think about the things you want to do then try to find small pages on those. Like, "how do I do pointers?" Or you could ask me for a botched job that is more personalised, it's up to you :p

Yeah, unfortunatelly I don't know enough about programming to even know what I need for a certain thing.
For example if I want to printf an integer variable's value then a plus sign ("+") and then another integer variable's value that were previously read by scanf, I have no idea what to use. I just know from that tutorial that it has something to do with %d and &variable_name.
So I'm not even sure what to google.

What I would really need is a good C tutorial for a complete beginner. Though my dad did say he has some book about C, it may prove to be useful. If he can find it.
...and that's the bottom line because Mate de Vita said so.
 
 
 
2009 Aug 22 at 18:01 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
void main() means the function main has no return type. This, as I have said, is wrong. Main always returns an int, which is why you will see most program's main functions returning 0 right at the end. Of course if there is a problem during execution you can make it return other things. Like 1.

To use printf(): it can be used as a easily as printf("This is a string"); or for the thing you said with variables you could go like printf("This is a string, then an integer which has the value %d",myIntForPrinting). This is different as you can see because it is suddenly taking two arguments rather than just one. The %d is a formatting character. The compiler sees this and knows to substitute a variable in there instead of %d. The %d tells it to format the number as a decimal, which is to say an int. So your thing would be like printf("%d + %d",firstnumber,secondnumber) where the previous line would be scanf("%d",&secondnumber) where you have already defined secondnumber to be of type int. You're going to need to know pointers soon. The wiki page is surprisingly comprehensive. These pages on printf and scanf are really good as well.

I might edit this to have linebreaks at some point, it is very cumbersome to read.
 
 
 
2009 Aug 22 at 19:17 UTC — Ed. 2009 Aug 22 at 19:18 UTC
Mate de Vita
Kelli

2008 Oct 4 • 2453
159 ₧
Ok, I'll read those today.

Btw what's evaluated sooner == or || ?

For example if I want to repeat the loop if x equals 1 or 2, which line is correct:
while (x==1 || 2)
while (x==1 || x==2)

(yes, I could also make more brackets to make both correct - probably - but that's not the point)
...and that's the bottom line because Mate de Vita said so.
 
 
 
2009 Aug 23 at 09:17 UTC — Ed. 2009 Aug 24 at 13:31 UTC
Mate de Vita
Kelli

2008 Oct 4 • 2453
159 ₧
So, my first program in C is done. It does pretty much the same as the one in C++ (2 integers, then it adds, subtracts, multiplies, divides and exponentiates (?) them) but this one also asks how many decimal places you want to have in the division results. But it doesn't have a "Would you like the program to repeat (Y/N)?" loop at the end yet. I'll have to implement that when I learn how to compare strings (and chars).
...and that's the bottom line because Mate de Vita said so.
 
 
 
2009 Aug 24 at 13:57 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
C string functions!

I guess the easiest way to do it would be to have an int that is set to zero at the start, then have a do...while loop wrapping the program. If the user says 'y' then change the int to be nonzero, it loops, change the int back to zero and so on.

The best way to check the operator precedence would be to write a program checking it. If in doubt, parenthesis out. As far as I'm aware though the OR construct binds tighter.
 
 
 
2009 Aug 25 at 12:31 UTC — Ed. 2009 Aug 25 at 12:32 UTC
Mate de Vita
Kelli

2008 Oct 4 • 2453
159 ₧
So my dad brought me the book C Programming language by Brian W. Kernighan and Dennis M. Ritchie (names sound familiar?) and now I'm learning from there, I have to say it's much better than the googled interwebs tutorials.
...and that's the bottom line because Mate de Vita said so.
 
 
 
2009 Aug 27 at 17:58 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
K&R C, it was the standard until about, oh, twenty years ago :p

Jokes aside, today's C is slightly different, though many programs are written in a style that is the same as K&R C. The current C standard is C89, though there should be a new one out soon? In fact, there is a newer standard from only a few years ago, it's the C++ that is still to come out. There are no majorly major differences, at least at a beginner level.
 
 
 
2009 Aug 27 at 22:47 UTC
Page 1 [2]