00:00
00:00
Newgrounds Background Image Theme

NisoXD just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

C++: Stdio introduction

1,225 Views | 0 Replies
New Topic Respond to this Topic

C++: Stdio introduction 2007-05-01 12:40:47


C++: Main

Okay here's a little tutorial i made for codedatabase, and i thought since NG has a C++ main, why not post it there aswell?? Sorry if this seems to be a little half-arsed, but it's optimized for codedatabase and seeing as you submit sourcecode there, i had no choice but to explain stuff with comments. But it's just to coopy&paste into your IDE/text editor of choice and run (also i'm from sweden so don't spank my bad english). =)

//-- Hello.c
/*
IMPOTANT NOTE: THIS TUTORIAL WAS MADE IN CODE::BLOCKS IDE 1.0 RC2 (WITH NO CHANGES DONE TO IT) ON WINDOWS XP HOME EDITION.

This tutorial is supposed to help you get a basic grip on stdio, as well as
some other handy things.
Stdio is a library for C/C++ that uses streams (in/out) and is in my opinion
better than the iostream library.
There might be some parts of this tutorial that you are not familiar with,
most likely the pointers so i'll give a little brief on it.

&variable --- the & points at the memory address where the variable is located.
Point at the address when using stdio in/out operations.

*/
//Includes
#include <stdio.h> //Used for i/o

//Main loop
int main()
{
char * p; //Used for my uggly pause, but i didn't want to nestle in more libs in this tutorial
char * user; //String to store input in
int age; //Integer for storing age

printf("Hello there, what is your name?\n"); //Prints some text.
scanf("%s", &user); //Read a string into the variable user. %s means you want to read a string.
printf("I am %d years old. %s %s %s %s?\n", 16, "How", "old", "are", "you"); //You should get it how this works
scanf("%d", &age); //Read an int into the variable age. %d means you want to read an integer.

if(age>16) //If your age is more than 16 then...
printf("Hello, %s! You are %d years older than me.\n", &user, age - 16); //Print stuff
else if(age<16) //But if your age is less than 16...
printf("Hello, %s! You are %d years younger than me\n", &user, 16 - age); //Print other stuff
else //Or if you are 16 too...
printf("Hello, %s! You are as old as i am!\n", &user); //Print some other stuff

/*
As you can see i didn't use any { or } in my if statement, that is completely valid, but only the next line belongs to it.
For example,

if(something)
dosomething();

and

if(something){
dosomething();
}

do the same things. This can also be used in for and while loops.
*/

scanf("%s",&p); //Don't get used to doing this! There are better ways to pause.

return 0; //Baibai
}
//END!!

//To learn more about stdio, go to http://www.cplusplus.com/reference/clibrary/c stdio/

Let's just hope NG didn't fuck the code up...