View Full Version: A tip

C++ Learning Community > C++ Tips > A tip


Title: A tip
Description: for beginners


Sam Fisher vs Solid Snake - August 13, 2003 05:47 PM (GMT)
and FHCandyman is right

Kylevision - August 8, 2003 12:46 AM (GMT)
If you are beginng to use C++ I suggest you use functions, it makes editing your programs much easyer.

Sam Fisher vs Solid Snake - August 8, 2003 12:49 AM (GMT)
well DUH! functions are basically the first thing people learn if you bought a book, you learn variables/operators first though

ih8censorship - August 8, 2003 01:39 AM (GMT)
QUOTE
well DUH! functions are basically the first thing people learn if you bought a book, you learn variables/operators first though


hey he was just getting a point across you seemed to be a little hostile there. functions were talked about in like the 4th chapter of my book. still some of us who use tutorials cause we cant afford a book might not know that.

Sam Fisher vs Solid Snake - August 8, 2003 02:33 AM (GMT)
true, sry, but it was in my 2-3 chapters, and functions are basically the main part of games, well kinda, they just add on to the game

Sam Fisher vs Solid Snake - August 8, 2003 02:57 PM (GMT)
i dont try to be hostile either, and i forgot tons of people are n00bs at c++

FHCandyman - August 8, 2003 09:38 PM (GMT)
functions, you really have to know what those are, after all, everything done in C++ is generally done in a function(at least where I'm at) That's a good tip for the really new noobs, and tip #2 for the 'never seen C++ in my life' people, is that variables hold values to use in the functions :D We could make this thread the simplest guide to learning C++ ever :lol:

Sam Fisher vs Solid Snake - August 8, 2003 09:45 PM (GMT)
variables declared within functions, can only be declared again in the same function, variables declared outside a function must stay out of a function, and all variables must have differnet names

FHCandyman - August 8, 2003 09:50 PM (GMT)
and never try to start a program in a function like functAdd(), because whenever a program runs it automatically calls main(), so always remember to start your program in main()

all variables and functions must be declared before you define and use them.

Sam Fisher vs Solid Snake - August 8, 2003 09:54 PM (GMT)
yup, also here is a list of meanings
cout=console output
cin=console input
std=standard namespace

FHCandyman - August 8, 2003 10:28 PM (GMT)
in order to use cout or cin(and i guess std too), you must put #include <iostream.h> in your program, usually at the top before everything else.

#include <iostream.h>
main()

iostream stands for input output stream, which is needed for displaying text, and #include tells the program to check and make sure that the following is included in the program, it is run before even main() and is needed to display any text on the screen... in functions, the actions it performs are called statements, which are enclosed by the braces {}

main()
{
statement;
}

statements generally end with semicolon ; which marks the end of a statement in C++(correct me if i'm wrong, i havent mastered this stuff yet)

i cant explain what std does, does it require iostream?

Sam Fisher vs Solid Snake - August 8, 2003 10:32 PM (GMT)
std makes the code cleaner or sumething surprisingly and it does need iostream

FHCandyman - August 8, 2003 10:38 PM (GMT)
I think we have enough for info for a quiz question, 'write a program that says Hello World' :D

Sam Fisher vs Solid Snake - August 8, 2003 10:40 PM (GMT)
you can look at my src code in c++ creations named Src code part one and Scr code Part 2, but it wont help, except till u are much better at c++

Kylevision - August 13, 2003 03:57 AM (GMT)
QUOTE (Sam Fisher vs Solid Snake @ Aug 8 2003, 02:57 PM)
i dont try to be hostile either, and i forgot tons of people are n00bs at c++

If you are refreing to me I have most likely been using C++ way longer then you.

Sam Fisher vs Solid Snake - August 13, 2003 04:21 AM (GMT)
i wasnt meaning you and pretty much everybody has knowledge and i dint mean n00b, and yes you probably do know more than me, since i have been devoting time to 2 separate books to learn, so it takes more time(a BASIC book and C++ book)

FHCandyman - August 13, 2003 05:07 PM (GMT)
i think when he said n00bs, he meant the people who read your post kylevision, not you yourself. As in a lot of n00bs may not know what function are etcetra and so on.

Kylevision - August 13, 2003 07:19 PM (GMT)
n00b is the most annoying word on the internet...

FHCandyman - August 13, 2003 09:35 PM (GMT)
i have to admit, i hate it too. i've just gotten used it. i usually say 'newbie' or just beginner.

Joefess53 - September 2, 2003 06:45 AM (GMT)
Lol well ill do the challenge then being a "New Person "



QUOTE

#include <iostream>
using namespace std;

int main()
{
cout << "Hello World"<<endl;

return 0
}



Mastiff Odit - October 29, 2003 03:29 AM (GMT)
Quote:
" n00b is the most annoying word on the internet..."
I have to agree.

Quote:
" i have to admit, i hate it too. i've just gotten used it. i usually say 'newbie' or just beginner."
Me too, I normaly type Newbee or Beginer.

And since I'm new too I'll take the challenge.
But I'll write a deferent program and no namespace std:

#include <iostream>

int main()
{
char n[10];
cout << "Hello What's your name?"<<endl;
cin.getline (char n[], int 10);
cout << "So your names " << n << " is it? Well thats a good name!";

return 0
}

Joefess53 - October 29, 2003 06:18 AM (GMT)
very nice Mastiff but ive just realied on mine and ur code we both forgot to put system("pause") so the person can see whats happened

MonkeyMan - October 30, 2003 02:49 AM (GMT)
CODE
#include <iostream.h>
int main()
{
 char buff[255];
 cout<<"Welcome to the world of simplicity!"<<endl;
 cout<<"Enter your name: ";
 cin>>buff;
 cout<<"Well, hello "<<buff<<" to my world!"<<endl;
 cout<<"Here is a random joke: "<<endl;
 cout<<"There are 10 kinds of people in this world. People that know binary and those who dont."<<endl;
 cout<<"Did you understand the joke? [Yes/No]: ";
 cin>>buff;
 cout<<buff<<" i think so."<<endl;
 cout<<"Well... maybe I should let you get back to your daily boringness"<<endl;
 system("pause");
 return 0;
}

Ok... i got no clue why i just made that... its 9:48pm.... errr.... i have gone crazy

7OD - October 30, 2003 03:27 PM (GMT)
MonkeyMan: Why is a '<<' following all of the console input keywords ;)? And an end (which was apparently an 'end1' put in the worng place... I use '\n' and find it simpler :D)

The line to pause the system does not make the program compile in VC++, does it?

MonkeyMan - October 30, 2003 11:19 PM (GMT)
O my... i just realized i put << instead of >>... o well (fixed)

TheRealNapster - November 7, 2003 03:25 PM (GMT)
does this
CODE

cin.getline(char passWord[],int 100);

allow the user to input a passWord of up to 100 characters?
i need to know becuase i need a program that allows the input of a password.

inline_skater20032001 - November 7, 2003 09:05 PM (GMT)
Yes except you dont have to put
CODE
int 100
, instead you can do it this way:
CODE

#include <iostream>
using namespace std;
char passWord[100];
int main()
{
    cout << "Enter a string: ";
    cin.getline(passWord, 100);
    cout << "You entered the following string: " << passWord <<endl;
    system("pause");
    return 0;
}

You don't have to use int, char, etc. after you have already defined them at the beginning of the program.

TheRealNapster - November 7, 2003 10:06 PM (GMT)
well then whywont this work

CODE

#include <iostream.h>
int main()
{
   char passWord[100];
   int opSelect;
 do{
      cout<<"Please enter the password: ";
            cin.getline(passWord, 100);
   
        switch (passWord)
          {  case 'barclay':
                cout<<". . . . . . . . Access Granted . . . . . . . ."<<endl;
                system("pause");
                goto lblQuitting;
 
             default:
                 cout<<". . . . . . . . Access Denied . . . . . . . ."<<endl;
                 cout<<" Enter a '1' if you would like to try again, or type any other number to quit."<<endl;
                 cin>>opSelect;                  
                    if(opSelect == '1')
                     { continue; }
                    else
                      { goto lblQuitting; }
           }
           }while(1);  
             lblQuitting:;
  return 0;
}

ZerO - December 7, 2003 11:36 PM (GMT)
Functions where like in my 2. chapter.... An by the way, u dont have to afford a book... ever heard of the library? U should go there some time... LOTS of lots of great books about c++ and other programming languages!

J-Swift - December 11, 2003 09:17 AM (GMT)
QUOTE
well then whywont this work


The switch executes and performs the subsequent case statements. The problem in your code is that case can only take 1) integer arguments or 2) single character arguments. This makes your code incorrect because you are using a string parameter as a comparison where the code expects a char. You DID however enclose it in single quotes which is good. To do the comparison your way you would have to do 7 seperate switch statements, each one looking at the next subscript of the array passWord ( i.e. first one sees if passWord[0] == 'b' second one sees if passWord[1] == 'a' etc....) Better off using strcmp.

EDIT: Oooh, nasty 'goto's. You should change those to return 0 's or to a sentinal value to break out of the doWhile and terminate normally. This promotes good structural programming.




* Hosted for free by InvisionFree