View Full Version : c++
-kimmie-
11-17-2003, 12:23 AM
it's like this.. im taking c++ and it sucks. if ur good at it. and have time to help me... i would love it.. thanks !
Idbeholda
11-17-2003, 02:56 AM
*chuckles*
what do you need help with (be specific... not something like "i need help with everything")
WIZÄRD
11-17-2003, 07:12 AM
i just know a tiny bit of it lol, nothign major, so I can't really help you kimmie :-\
KANE_6969
11-17-2003, 09:18 AM
i know c++kinda.. it okk the class but only managed a low B
-kimmie-
11-17-2003, 12:22 PM
ok well right now i am having issues with arrays, passing functions to them, etc... it's just hard for me, sigh :(
i took it for " fun " ... my god im a biology major.. why didn't someone smack me? like super hard. well im gonna feel the smack if i don't pass this godforsaken class...
KANE_6969
11-17-2003, 12:35 PM
thats pretty much where i got lost out my mind too.... alot of sleepless nights.. what were you thuinking a comp language?i rememebrbeign ahead inteh class and taking liek 2 weeks on 1 array / function project confusing and takes forever! i sure id knows the language beter pug prolly does too
Mark__C
11-17-2003, 01:13 PM
passing functions to arrays or passing arrays to functions?
the latter can be done with pointers
_I_Play_Chess
11-17-2003, 03:08 PM
http://www.codepedia.com/1/BeginnersGuideToCPlusPlus
_I_Play_Chess
11-17-2003, 03:27 PM
http://www.cprogramming.com/
_I_Play_Chess
11-17-2003, 03:30 PM
Lesson 8: Array basics (Printable Version (http://www.cprogramming.com/tutorial/print/lesson8.html))
Arrays are useful critters because they can be used in many ways. For example, a tic-tac-toe board can be held in an array. Arrays are essentially a way to store many values under the same name. You can make an array out of any data-type including structures and classes.
Think about arrays like this:
[][][][][][]
Each of the bracket pairs is a slot(element) in the array, and you can put information into each one of them. It is almost like having a group of variables side by side.
Lets look at the syntax for declaring an array.
int examplearray[100]; //This declares an array
This would make an integer array with 100 slots, or places to store values(also called elements). To access a specific part element of the array, you merely put the array name and, in brackets, an index number. This corresponds to a specific element of the array. The one trick is that the first index number, and thus the first element, is zero, and the last is the number of elements minus one. 0-99 in a 100 element array, for example.
What can you do with this simple knowledge? Lets say you want to store a string, because C++ has no built-in datatype for strings, at least in DOS, you can make an array of characters.
For example:
char astring[100];
will allow you to declare a char array of 100 elements, or slots. Then you can receive input into it it from the user, and if the user types in a long string, it will go in the array. The neat thing is that it is very easy to work with strings in this way, and there is even a header file called string.h. There is another lesson on the uses of string.h, so its not necessary to discuss here.
The most useful aspect of arrays is multidimensional arrays.
How I think about multi-dimensional arrays.
[][][][][]
[][][][][]
[][][][][]
[][][][][]
[][][][][]
This is a graphic of what a two-dimensional array looks like when I visualize it.
For example:
int twodimensionalarray[8][8];
declares an array that has two dimensions. Think of it as a chessboard. You can easily use this to store information about some kind of game or to write something like tic-tac-toe. To access it, all you need are two variables, one that goes in the first slot and one that goes in the second slot. You can even make a three dimensional array, though you probably won't need to. In fact, you could make a four-hundred dimensional array. It would be confusing to visualize, however.
Arrays are treated like any other variable in most ways. You can modify one value in it by
putting:
arrayname[arrayindexnumber]=whatever;
//or, for two dimensional arrays
arrayname[arrayindexnumber1][arrayindexnumber2]=whatever;
However, you should never attempt to write data past the last element of the array, such as when
you have a 10 element array, and you try to write to the 11 element. The memory for the array
that was allocated for it will only be ten locations in memory, but the twelfth could be
anything, which could crash your computer.
You will find lots of useful things to do with arrays, from store information about certain
things under one name, to making games like tic-tac-toe. One suggestion I have is to use for
loops when access arrays.
<PRE class=example>#include <iostream.h>
int main()
**
int x, y, anarray[8][8];//declares an array like a chessboard
for(x=0; x<8; x++)
**
for(y=0; y<8; y++)
**
anarray[x][y]=0;//sets the element to zero; after the loop all elements == 0
}
}
for(x=0; x<8;x++)
**
for(y=0; y<8; y++)
**
cout<<"anarray["<<x<<"]["<<y<<"]="<<anarray[x][y]<<" ";//you'll see
}
}
return 0;
}
</PRE>Here you see that the loops work well because they increment the variable for you, and you only
need to increment by one. Its the easiest loop to read, and you access the entire array.
One thing that arrays don't require that other variables do, is a reference operator when
you want to have a pointer to the string. For example:
char *ptr;
char str[40];
ptr=str; //gives the memory address without a reference operator(&)
//As opposed to
int *ptr;
int num;
ptr=&num;//Requires & to give the memory address to the ptr
_I_Play_Chess
11-17-2003, 04:11 PM
Lesson 9: Strings (Printable Version (http://www.cprogramming.com/tutorial/print/lesson9.html))
In C++ strings are really arrays, but there are some different functions that are used for strings, like adding to strings, finding the length of strings, and also of checking to see if strings match.
The definition of a string would be anything that contains more than one character strung together. For example, "This" is a string. However, single characters will not be strings, though they can be used as strings.
Strings are arrays of chars. Static strings are words surrounded by double quotation marks.
"This is a static string"
To declare a string of 50 letters, you would want to say:
char string[50];
This would declare a string with a length of 50 characters. Do not forget that arrays begin at zero, not 1 for the index number. In addition, a string ends with a null character, literally a '/0' character. However, just remember that there will be an extra character on the end on a string. It is like a period at the end of a sentence, it is not counted as a letter, but it still takes up a space. Technically, in a fifty char array you could only hold 49 letters and one null character at the end to terminate the string.
TAKE NOTE: char *arry;
Can also be used as a string. If you have read the tutorial on pointers, you can do something such as:
arry = new char[256];
which allows you to access arry just as if it were an array. Keep in mind that to use delete you must put [] between delete and arry to tell it to free all 256 bytes of memory allocated.
For example,
delete [] arry.
Strings are useful for holding all types of long input. If you want the user to input his or her name, you must use a string.
Using cin>> to input a string works, but it will terminate the string after it reads the first space. The best way to handle this situation is to use the function cin.getline. Technically cin is a class, and you are calling one of its member functions. The most important thing is to understand how to use the function however.
The prototype for that function is:
cin.getline(char *buffer, int length, char terminal_char);
The char *buffer is a pointer to the first element of the character array, so that it can actually be used to access the array. The int length is simply how long the string to be input can be at its maximum (how big the array is). The char terminal_char means that the string will terminate if the user inputs whatever that character is. Keep in mind that it will discard whatever the terminal character is.
It is possible to make a function call of cin.getline(arry, '\n'); without the length, or vice versa, cin.getline(arry, 50); without the terminal character. Note that \n is the way of actually telling the compiler you mean a new line, i.e. someone hitting the enter key.
For a example:
<PRE class=example>#include <iostream.h>
int main()
**
char string[256]; //A nice long string
cout<<"Please enter a long string: ";
cin.getline(string, 256, '\n'); //The user input goes into string
cout<<"Your long string was:"<<endl<<string;
return 0;
}</PRE>Remember that you are actually passing the address of the array when you pass string because arrays do not require a reference operator (&) to be used to pass their address. Other than that, you could make \n any character you want (make sure to enclose it with single quotes to inform the compiler of its character status) to have the getline terminate on that character.
String.h is a header file that contains many functions for manipulating strings. One of these is the string comparison function.
int strcmp(const char *s1, const char *s2);
strcmp will accept two strings. It will return an integer. This integer will either be:
Negative if s1 is less than s2.
Zero if s1 and s2 are equal.
Positive if s1 is greater than s2.
Strcmp is case sensitive. Strcmp also passes the address of the character array to the function to allow it to be accessed.
int strcmpi(const char *s1, const char *s2);
strcmp will accept two strings. It will return an integer. This integer will either be:
Negative if s1 is less than s2.
Zero if the s1 and s2 are equal.
Positive if s1 is greater than s2.
Strcmpi is not case sensitive, if the words are capitalized it does not matter.<B>Not ANSI C++</B>
char *strcat(char *desc, char *src);
strcat is short for string concatenate, which means to add to the end, or append. It adds the second string to the first string. It returns a pointer to the concatenated string.
char *strupr(char *s);
strupr converts a string to uppercase. It also returns a string, which will all be in uppercase. The input string, if it is an array and not a static string, will also all be uppercase. <B>Not ANSI C++</B>
char *strlwr(char *s);
strlwr converts a string to lowercase. It also returns a string, which will all be in uppercase. The input string, if it is an array, will also all be uppercase.
size_t strlen(const char *s);
strlen will return the length of a string, minus the termating character(/0). The size_t is nothing to worry about. Just treat it as an integer, which it is.
Here is a small program using many of the previously described functions:
<PRE class=example>#include <iostream.h> //For cout
#include <string.h> //For many of the string functions
int main()
**
char name[50]; //Declare variables
char lastname[50]; //This could have been declared on the last line...
cout<<"Please enter your name: "; //Tell the user what to do
cin.getline(name, 50, '\n'); //Use gets to input strings with spaces or
//just to get strings after the user presses enter
if(!strcmpi("Alexander", name)) //The ! means not, strcmpi returns 0 for
** //equal strings
cout<<"That's my name too."<<endl; //Tell the user if its my name
}
else //else is used to keep it from always
** //outputting this line
cout<<"That's not my name.";
}
cout<<"What is your name in uppercase..."<<endl;
strupr(name); //strupr converts the string to uppercase
cout<<name<<endl;
cout<<"And, your name in lowercase..."<<endl;
strlwr(name); //strlwr converts the string to lowercase
cout<<name<<endl;
cout<<"Your name is "<<strlen(name)<<" letters long"<<endl; //strlen returns
//the length of the string
cout<<"Enter your last name:";
cin.getline(lastname, 50, '\n'); //lastname is also a string
strcat(name, " "); //We want to space the two names apart
strcat(name, lastname); //Now we put them together, we a space in
//the middle
cout<<"Your full name is "<<name; //Outputting it all...
return 0;
}
</PRE>
Mark__C
11-17-2003, 04:14 PM
well shes taking a class im pretty sure they'll teach her all that.
i think she was looking for a simple answer
pugamsish
11-17-2003, 06:24 PM
testing the WYSIWYG
leblitzer
11-18-2003, 04:51 PM
Originally posted by -kimmie-
it's like this.. im taking c++ and it sucks. if ur good at it. and have time to help me... i would love it.. thanks !
well i became a C++ expert for some reason , so wut do u wanna prog with it?
leblitzer
11-18-2003, 04:55 PM
Originally posted by -kimmie-
ok well right now i am having issues with arrays, passing functions to them, etc... it's just hard for me, sigh :(
i took it for " fun " ... my god im a biology major.. why didn't someone smack me? like super hard. well im gonna feel the smack if i don't pass this godforsaken class...
passing array to fun is quite easy try a thing like that
try to pass a pointer to a char witch represent that array
int meanArray(char * tab,max)**
int j;
for(int i=0,i<max,i++)**
j=+tab[i];
}
return j;
}
must work
leblitzer
11-18-2003, 04:57 PM
i just saw ipc comment , its true u can make a class witch reprent the array and work with that object, enjoy :)
baloney_mahoney
11-18-2003, 07:03 PM
Good god, haven't any of you experts learned by now that using pointers is the worst kind of programming? You think you're good? You want to get a job as a professional C++ programmer with a company then get that damn 'pointer' stuff out of your system and learn to program without using them! Crap, learn to program without pointers even if you dont want to be a professional programmer; it's just plain poor programming.
Mark__C
11-18-2003, 07:45 PM
um no im not a professional programmer
im going into the medical field so pointers are good enough for me
baloney_mahoney
11-18-2003, 07:52 PM
Originally posted by Mark__C
um no im not a professional programmer
im going into the medical field so pointers are good enough for me
Yep, you have the right idea. Pointers you need in the medical field.
John-
11-18-2003, 08:25 PM
whats pointers?
gimme it as simple as you can lol
baloney_mahoney
11-18-2003, 08:46 PM
In Visual Basic you do not use Pointers. In C/C++ you have the option to use them and many C/C++ programmers use them but it is not good to do so for many reasons.
A Pointer is a variable that contains the address of another variable. So, instead of referencing the other variable by name as you do in VB you reference it by using the pointer to it.
Example (but not in the C language)
Assume MyData is located at memory location 5000.
MyPointer = MyData ' the address in memory where MyData is, is placed in MyPointer
So, instead of MyPointer containing data, it contains 5000.
Now when programmers want the information in MyData they 'point' to it with the pointer 'MyPointer' instead of calling it by it's variable name.
I can't go into detail because that's not really the point but I hope this kind of explains what a pointer is.
Mark__C
11-18-2003, 10:22 PM
uh. I meant i wont need to know what you consider to be "correct programming"
Mark__C
11-18-2003, 10:23 PM
and yes you can use pointers in vb if you need to
baloney_mahoney
11-18-2003, 10:27 PM
Yes you can use AddressOf if you want to and I am sure you can have other type of pointers but they are not common.
And yes, you really don't need to know what I consider to be 'correct' or consider what anyone considers to be correct You can program anyway you want to. And i knew what you meant anyway.
leblitzer
11-19-2003, 12:27 PM
Originally posted by baloney_mahoney
Good god, haven't any of you experts learned by now that using pointers is the worst kind of programming? You think you're good? You want to get a job as a professional C++ programmer with a company then get that damn 'pointer' stuff out of your system and learn to program without using them! Crap, learn to program without pointers even if you dont want to be a professional programmer; it's just plain poor programming.
in C++ pointer rulez!
if your a noob with it use C lite or Secure C# or even J++
but u cant deal with system on it :(
baloney_mahoney
11-19-2003, 12:54 PM
Originally posted by leblitzer
in C++ pointer rulez!
if your a noob with it use C lite or Secure C# or even J++
but u cant deal with system on it :(
If you write programs in C/C++ and you do them for your own private purpose then program anyway you want to. But, in the professional world, using pointers is highly discouraged if not dissallowed.
Pointers lead into big time problems and company's really get upset when a programmer used pointers in his program. Then, that person leaves the company. Then, his program blows up. Now, the company has to give that code to another programmer and debugging C/C++ programs with pointers is a nightmare not only for the poor bastard who got the code to debug but for other programmers as well.
When you say 'C++ pointers rulez' you are only saying that because that is your way of doing things the easy way because you probably don't know how to write C++ programs without using them. C++ programmers get too dependent on using pointers and it becomes very difficult for alot of them to deal with coding any other way.
Listen man, when I first got a job as a programmer and I was asked to create some code for a large application ( I was one of many programmers on the team) and I had to do it in C++ I used pointers like crazy. Man, did I get the 'kick in the butt' for doing that! The team leader of the project made me re-write everything and told me "never use pointers in any programs as long as you are with this company".
Now, I see why. No one but no one wants to debug C++ programs with pointers.
But you can do as you please because you are an 'expert' or at least you say you are. But get a job with a company and see how far that will get you. And I dont mean some small rinky-dink company that doesn't care, I'm talking about big companies that do big time applications. It just isnt allowed.
leblitzer
11-19-2003, 01:04 PM
is alstom a big company coz i worked there n i did pointer a lot even on criticical prog
baloney_mahoney
11-19-2003, 01:21 PM
Originally posted by leblitzer
is alstom a big company coz i worked there n i did pointer a lot even on criticical prog
It's neither here nor there whether alstom is a big company or not. I don't know. Get a job with SBC and see how much they love pointers.
Some companies, big or small, don't care. I don't want to get into an argument over using pointers in your C/C++ programs. That will just get nowhere. But when you are part of a team of programmers working on projects that other programmers are affected by what you do just keep in mind that they probably would rather that you didnt use pointers because they don't want to debug that kind of code. Debugging C++ is difficult as it is let alone trying to figure it out when it is full of pointers. Maybe you haven't experienced that yet and maybe you never will.
leblitzer
11-19-2003, 03:15 PM
why do u want to debug a prog , u prolly never heard about formal method thats why u do a lot a og bug on your prog
baloney_mahoney
11-19-2003, 03:36 PM
Originally posted by leblitzer
why do u want to debug a prog , u prolly never heard about formal method thats why u do a lot a og bug on your prog
That is a silly question.
Of course I don't want to debug programs. No one really wants to debug programs especially if that program was written by someone else who didn't know his ass from a hole in the ground. But, unfortunately we have to.
It's not a question of 'want to', it's a question of 'have to' when you work for the man and he tells you that is what you have to do.
I think you are living in a dream world where everything is perfect and there are no programs that contains bugs and errors.
Here's the facts: There is no such thing as a perfect program, all programs at one time or another contained logic errors and bugs. Many companies have programs that were written years ago and they still use them and they are always falling apart. Man, you just haven't been around enough to know what the %^&@ you are talking about.
And here's another thing: Some companies buy programs from other companies like the company you work for who doesn't give a damn about the quality of their programs and those programs have to be debugged to see what the hell is going on and make modifications to them.
If you can't understand this then you better keep your programs on a private basis because maybe the company you work for has no restrictions but that is not the normal.
If your programs are perfect and free of logic error and bugs then you should get the 'Perfect Programmer of the Century Award'.
Now, please don't turn this into a stupid argument.
leblitzer
11-19-2003, 04:55 PM
nope formal method can generate prog with 0% of error, dude make some study before to speak of think that u dont know
leblitzer
11-19-2003, 05:02 PM
a bruz work there http://www.gemplus.com/smart/r_d/trends/system_model/b_method/description.html
baloney_mahoney
11-19-2003, 05:24 PM
Originally posted by leblitzer
nope formal method can generate prog with 0% of error, dude make some study before to speak of think that u dont know
Well, leb, you are most fortunate to have such a tool that can generate 0% error applications.
I don't remember saying anything about 'formal method' one way or another. That was your doings.
You should go to comapnies like Oracle, Microsoft, SBC, Pacific Telesis, TRW, Sun MircoSystems, Laten Software, Bordland, and other such large companies and let them know this.
But, generating error-free applications using 'formal method' doesn't mean that all applications are generated this way. It doesn't mean that all programs won't be debugged. It doesn't mean that 'pointers' are highly discouraged by many large companies with hundreds of programmers. It doesn't mean that there are 10000s of programs already out there with many many errors in them.
However, I think the issue was wheather using pointers is considered good programming or not. Your "formal method" has nothing to do with the issue.
So, what is your point?
leblitzer
11-20-2003, 07:33 AM
Microsoft is well known to do prog with bug , so ppl will buy patch foreever, they dont care is there product is good or not, prog who implie security of ppl like in technologie embeded like in train and transport cant do that prog with bugs...
vBulletin v3.5.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.