|
 |
|
 |
01-25-2005, 08:32 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Dec 2004
Posts: 43
|
2 questions about arrays and incudes files, and etc.
Just need to know a couple or so questions....
...an aray is an integer like this ( int valueArray[128] ) and what I understand it does is it acts like a bunch of integers like having int 1, input, value, or whatever but retains the same name and just adds a 1, 2, 3, and so on so you can store more info and wasting a hell of a lot less time? I am almost done with the chapter in C++ for dummies, im getting there...
...when you make your own "Include" file--as such #include "math" -- where is this file saved to and how exactly are they made? Are they just the same as any regular c++ file but you just use it to save you lots of time typing? I am on part 2 of C++ for dummies, and I may go into this later I don't know.
I got more questions, there just not coming to mind... so thanx in advance!
|
|
|
01-25-2005, 11:41 PM
|
#2 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Quote:
|
...an aray is an integer like this ( int valueArray[128] ) and what I understand it does is it acts like a bunch of integers like having int 1, input, value, or whatever but retains the same name and just adds a 1, 2, 3, and so on so you can store more info and wasting a hell of a lot less time?
|
Try decent english. I might understand what you are talking about.
Quote:
|
...when you make your own "Include" file--as such #include "math" -- where is this file saved to and how exactly are they made? Are they just the same as any regular c++ file but you just use it to save you lots of time typing?
|
Both header files (.h, .hpp) and source files (.c, .cpp, cxx) can be included with the statement:
Code:
#include "myheader.h"
#include <iostream>
#include <math.h>
#include "special_operations.cpp"
The files you make are usually stored in your project folder. The files provided by the IDE/Compiler are usually stored in .../source and .../include.
The linker and compiler will co-operate in making an executable from these files, through an intermediate step by making them obj files first. The linker is responisble for this part. From *.obj they are converted to executables.
__________________
|
|
|
01-26-2005, 10:00 AM
|
#3 (permalink)
|
|
mike
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
|
For headers usually you keep them in your project folder. Not everyone uses an IDE. I would say most common is to use a *.h name, but as Valmont said you can also use *.hpp and I believe *.hxx. I just use *.h so I dont forget, I'm a bit rusty. Usually in C++ you would fill out function prototypes, sometimes structs and usually class definitions. Here's an example of three files, main.cpp, someclass.cpp and someclass.h. Just assume they are all stored in the same directory.
someclass.h
Code:
#ifndef SOMECLASS_H_
#define SOMECLASS_H_
// Lets say this class stores a user, simplistic example...
class SomeClass
{
private:
string user;
string pass;
public:
SomeClass(); // default constructor
SomeClass(string, string); // constructor that takes username and pass
void setUser(string);
void setPass(string);
string getUser();
string getPass();
};
#endif
someclass.cpp
Code:
#include <iostream>
using namespace std;
#include "someclass.h" // include header file you wrote
SomeClass::SomeClass() // default constructor
{
// set values to empty strings
user = ""; pass="";
}
SomeClass::SomeClass(string uName, string pWord)
{
user = uName; pass = pWord;
}
void SomeClass::setUser(string uName)
{
user = uName;
}
void SomeClass::setPass(string pWord)
{
pass = pWord;
}
string SomeClass::getUser()
{
return user;
}
string SomeClass::getPass()
{
return pass;
}
main.cpp
Code:
#include <iostream>
using namespace std;
#include "someclass.h"
int main(void)
{
// Build array of 5 users
SomeClass users[5];
int i;
while(i < 5)
{
cout << "Enter user " << i << ": ";
cin >> users[i].setUser();
cout << "Enter password " << i << ": ";
cin >> users[i].setPass();
i++;
}
i = 0;
// dump data
while(i < 5)
cout << "User: " << users[i].getUser() << endl
<< "Pass: " << users[i].getPass() << end << endl;
return 0;
}
|
|
|
01-27-2005, 08:58 AM
|
#4 (permalink)
|
|
Registered User
Join Date: Dec 2004
Posts: 43
|
Quote:
|
Originally Posted by Valmont
Try decent english. I might understand what you are talking about.
|
Ok, how do Arrays work all together? Please tell me in a way a newbie could understand. Thanks in advance!
|
|
|
01-27-2005, 09:39 AM
|
#5 (permalink)
|
|
mike
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
|
I basically learned arrays from practicing as much as I could with them and reading a bunch of book chapters and tutorials about them. By the time I took Intro to C++ in school last term I already knew enough about them to not really learn anything new.
A pretty good tutorial website for things like arrays and structs is http://juicystudio.com/tutorial/cpp/index.asp
Code:
// Arrays.cpp
#include <iostream>
using namespace std;
const int ARRAY_SIZE = 10;
int main()
{
// Build an array of int's and fill it with the numbers 1-10
int numericArray[ARRAY_SIZE] = {1,2,3,4,5,6,7,8,9,10};
// Build an array of char's the hard way (character by character)
char characterArray[ARRAY_SIZE] = {'h', 'e', 'l', 'l', 'o', '\n'};
// Build an array of doubles and fill it - don't fill it
double userInputArray[ARRAY_SIZE];
// Now lets loop through the numericArray and print the values
for(int i = 0; i < ARRAY_SIZE; i++)
cout << "Array element " << i << " = " << numericArray[i] << endl;
// Lets loop through the characterArray and print the character's
for(i = 0; i < ARRAY_SIZE && characterArray[i] != '\n'; i++)
cout << characterArray[i];
cout << endl; // insert our own line break after loop
// Fill the doubleArray with values from the user
for(i = 0; i < ARRAY_SIZE; i++)
{
cout << "Enter array element " << i << " (type double): ";
cin >> userInputArray[i];
}
// And finally print the results
for(i = 0; i < ARRAY_SIZE; i++)
cout << "Array element " << i << " = " << userInputArray[i] << endl;
return 0;
}
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 08:23 AM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|