cout<<"Enter the scores for bowler1 "123234345
cin >> ?
This is a 4x6 array.(5 man bowling team) I would like to enter the scores on the same line. char arr[5][50];
cout << "Enter line1:";
cin >> arr[0];
cout << "Enter line2:";
cin >> arr[1];
int fn(int a){ return 1;}
int fn(const int a){ return 2;}
void main()
{
}
for (i = 0; i < length_of_arrays; i++)
{
int_array[i] = (int)char_array[i];
}
And then this to copy an int array into a char array:for (i = 0; i < length_of_arrays; i++)
{
char_array[i] = (char)int_array[i];
}
The (int) tells the subscript of the character array to act like an integer. The (char) tells the subscript of the integer array to act like a char, etc. etc.class date
{
public:
date()
{
cout << "This is a DEFAULT constructor" << endl;
}
};
The term default is used because when an object is created without passing any parameters to the constructor, the default is used.c:\> adder 93 25
int main(int argc, char *argv[])
//-------------------------------------------------------------------
#include <iostream.h>
int main(int argc, char *argv[])
{
int i;
cout << "Running " << argv[0] << endl;
for (i = 1; i < argc; i++)
{
cout << " Argument #" << i << " is \"" << argv[i] << "\"."
<< endl;
}
return 0;
}
//-------------------------------------------------------------------
// -------------------------------------------------------------
class Something
{
public:
int Load (fstream &file);
int Save (fstream &file);
short x, y;
};
int Something::Load(fstream &file)
{
file.read(&x, 2);
file.read(&y, 2);
return 0;
}
int Something::Save(fstream &file)
{
file.write(&x, 2);
file.write(&y, 2);
return 0;
}
// ----------------------------------------------------------------
fstream f;
Something o1, o2;
cout << "Initializing ... " << endl;
o1.x = 0;
o1.y = -5;
o2.x = 182;
o2.y = 67;
cout << "Values are: O1[" << o1.x << ", " << o1.y << "] O2["
<< o2.x << ", " << o2.y << "]." << endl;
cout << "Saving to file ... " << endl;
f.open("data.dat", ios::out | ios::binary);
o1.Save(f);
o2.Save(f);
f.close();
o1.x = 0;
o1.y = 0;
o2.x = 0;
o2.y = 0;
cout << "Values are: O1[" << o1.x << ", " << o1.y << "] O2["
<< o2.x << ", " << o2.y << "]." << endl;
cout << "Loading from file ... " << endl;
f.open("data.dat", ios::in | ios::binary);
o1.Load(f);
o2.Load(f);
f.close();
cout << "Values are: O1[" << o1.x << ", " << o1.y << "] O2["
<< o2.x << ", " << o2.y << "]." << endl;
// ----------------------------------------------------------------
Make any sense? I hope so. Just remember that you actually have to write out the individual variables to the file.system("command");
So, you might have a program like the following:// ------------------------------------------------------------------
#include <iostream.h>
#include <stdlib.h>
int main()
{
cout << "Calling dos shell (type Exit to return) ... " << endl;
system("command");
cout << "Welcome back to my program!" << endl;
return 0;
}
// ------------------------------------------------------------------
// structure definition
struct
{
char str[20];
} egg;
// function prototype
void func (egg x[]);
// ^ ^ ^
// | | |
//name of struct | |
//name of parameter|
// to show we're passing an array
main()
{
// create an array of objects of type 'egg'
egg var[20];
// pass the array we made to the function
func (var);
}
// function definition
void func (egg x[])
{
}
char myarray[20];
That would point to an empty spot in memory followed by nineteen more empty spots. To delve into this further, let's say that memory location was "102 99th St. Oak" (just to be funny cuz computers' addresses are in hex). And that location is empty, followed by nineteen more empty spaces:myarray -> 102 99th St. Oak (subscript 0: myarray[0])
103 99th St. Oak (subscript 1: myarray[1])
104 99th St. Oak (subscript 2: myarray[2])
105 99th St. Oak (subscript 3: myarray[3])
106 99th St. Oak (subscript 4: myarray[4])
107 99th St. Oak (subscript 5: myarray[5])
108 99th St. Oak (subscript 6: myarray[6])
109 99th St. Oak (subscript 7: myarray[7])
110 99th St. Oak (subscript 8: myarray[8])
111 99th St. Oak (subscript 9: myarray[9])
112 99th St. Oak (subscript 10: myarray[10])
113 99th St. Oak (subscript 11: myarray[11])
114 99th St. Oak (subscript 12: myarray[12])
115 99th St. Oak (subscript 13: myarray[13])
116 99th St. Oak (subscript 14: myarray[14])
117 99th St. Oak (subscript 15: myarray[15])
118 99th St. Oak (subscript 16: myarray[16])
119 99th St. Oak (subscript 17: myarray[17])
120 99th St. Oak (subscript 18: myarray[18])
121 99th St. Oak (subscript 19: myarray[19])
That last one (121 99th St. Oak) we'd use for our NULL character. Now if we were crazy and weird we could add one to 'myarray' like this:myarray++;
Now it would point to 103 99th St. Oak. That means we'd only have like 19 empty areas to work with, plus we need to use one for the NULL character.// -----------------------------------------------------------------
#include <iostream.h>
main()
{
// I save the last character for a NULL character (myarray[19] = 0)
char myarray[20] = "1234567890abcdefghi";
// create a pointer
char *ptr;
// 'myarray' points to an address that has 19 subsequent empty slots
// we know that by how we declared it above. When I do the follow-
// ing, I give 'ptr' the same memory address. So know it too will
// point to that same spot in memory. Now we can mess with THAT
// and not change where our original 'myarray' points to. That way
// if we really mess it up we can always "ptr = myarray" to get the
// address back
ptr = myarray;
cout << "myarray = \"" << myarray << "\"" << endl;
cout << "ptr = \"" << ptr << "\"" << endl;
// now we're going to do something strange that you might find
// interesting. We're going to add one to the value of 'ptr'
// and then print subscript 0 of 'ptr'. That means whatever
// character that its pointing to will be printed.
cout << "ptr[0] = '" << ptr[0] << "'" << endl;
ptr++;
cout << "ptr[0] = '" << ptr[0] << "'" << endl;
// pretty cool, huh? Now we'll keep doing this until we reach
// a NULL character. But first let's start over.
ptr = myarray;
cout << "ptr ... ";
while (ptr[0] != 0) // loop while we're NOT equal to a NULL char
{
cout << ptr[0];
ptr++;
}
cout << " ... " << endl;
// okay that's it... hope it was useful at all :)
}
// -----------------------------------------------------------------
main()
{
// this being the end of your program of course ...
{ char p; // a temporary variable
cout << "press enter to continue";
cin.get(p);
}
}
// -[cls method 1]----------------------------------------------------------
int i;
for (i = 0; i < 24; i++)
cout << endl;
// -------------------------------------------------------------------------
Another way is if you're using DOS or a system that accepts the CLS command
at the prompt (I believe LINUX does as well), then you can call the system:// -[cls method 2]----------------------------------------------------------
system("cls");
// -------------------------------------------------------------------------
For this last one to work you'd have to include "stdlib.h". In Borland C++
and other compilers there is a function called 'clrscr()' in "conio.h" that
you can use just like a CLS:// -[cls method 3 - not portable!]------------------------------------------
clrscr();
// -------------------------------------------------------------------------
Compilers create a macro named something similar to their own name when you include any one of their libraries. This makes it easy to detect the compiler you're using. This is useful for things making a function that will compile flawlessly in any compiler. Below is what I mean. Cut out the following code and put it at the top of ANY of your programs and you will be able to call "cls();" to clear the screen:// -[cut here]--------------------------------------------------------------
#if defined(BORLAND_C)
#include <conio.h>
#define cls() clrscr()
#endif
#if !defined(BORLAND_C)
#include <stdlib.h>
#define cls() system("cls");
#endif
// -[end cut here]----------------------------------------------------------
atoi()
or some similar function. Take a look at the following:// -[checking user input]---------------------------------------------------
#include <iostream.h> // for cout/cin
#include <stdlib.h> // for atoi()
#include <ctype.h> // for isdigit()
main()
{
char usrinp[80]; // string to store user input
int value; // the variable we'll want to store the end value in
int i; // simple index variable
do
{
value = 0;
cout << "Please enter a whole number (not zero!): ";
cin >> usrinp;
for (i = 0; usrinp[i]; i++)
{
if (!isdigit(usrinp[i])) break;
}
if (usrinp[i])
cout << "I said a NUMBER numb-skull!!!" << endl;
else
value = atoi(usrinp);
} while (!value);
cout << "The value is " << value << endl;
}
// -------------------------------------------------------------------------
srand(time(NULL));
This makes the current time the seed so the random numbers that are generated will truly be random. Now to actually USE the function... rand() will return a number from zero to one LESS than the number you moduli it by. So in the following it will return 0, 1, 2, 3, 4, or 5:rand()%6;
So if you had a variable named 'x' and you wanted to put a number in it that was in the range of zero to 100 you'd do the following:x = rand()%101;
Therefore if you needed a different range (like 5 - 10 example), you'd need to do some simple adding. Let's say we wanted a number from 5 to 10. That's six possibilities (5, 6, 7, 8, 9, 10), so we'd moduli by 6 (0, 1, 2, 3, 4, 5) and add 5:x = rand()%6 + 5;
Whatever the return is will be added to the number 5. So if it returned the maximum (5) then the result (after adding) would be 10. And if it returned the minimum (0) then the result (after adding) would be 5. So we get that range that we need.// ------------------------------------------------------------------
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
int rnd(int min, int max);
int main()
{
srand(time(NULL));
cout << "Random number in the range of 1-5: " << rnd(1,5) << endl;
cout << "Range of 16-31: " << rnd(16,31) << endl;
return 0;
}
int rnd(int min, int max)
{
return (rand()%(max-min+1))+min;
}
// ------------------------------------------------------------------
A breakdown of the one and only line of our 'rnd' function would be as follows if we were getting a number from 5 to 10:(rand()%(10-5+1))+5;
(rand()%( 5+1))+5;
(rand()%(( 6 ))+5;
rand()%6+5;
A better example would be 121 to 253:(rand()%(253-121+1))+121;
(rand()%( 132+1))+121;
(rand()%( 133 ))+121
rand()%133+121;
And that's that.// ------------------------------------------------------------------
#include <iostream.h>
enum BOOL { True = 1, False = 0 };
void function (int n);
int main()
{
BOOL x;
x = TRUE;
function(x);
return 0;
}
void function (int n)
{
cout << "You passed: " << n << endl;
}
// ------------------------------------------------------------------
Or you can explicitely tell the compiler that the parameter you want to pass is of a specific enumerated type like in the following:// ------------------------------------------------------------------
#include <iostream.h>
enum Month
{
January = 0, February = 1, March = 2, April = 3, May = 4, June = 5,
July = 6, August = 7, September = 8, October = 9, November = 10,
December = 11
};
int function(Month m);
int main()
{
Month mon;
mon = August;
cout << "The month selected is \"";
function(mon);
cout << "\"" << endl;
return 0;
}
int function(Month m)
{
switch(m)
{
case January:
cout << "January";
break;
case February:
cout << "February";
break;
case March:
cout << "March";
break;
case April:
cout << "April";
break;
case May:
cout << "May";
break;
case June:
cout << "June";
break;
case July:
cout << "July";
break;
case August:
cout << "August";
break;
case September:
cout << "September";
break;
case October:
cout << "October";
break;
case November:
cout << "November";
break;
case December:
cout << "December";
break;
}
return 0;
}
// ------------------------------------------------------------------
Hope this helps! Good luck in your class!// ------------------------------------------------------------------
#include <iostream.h>
int do_process_1();
int do_process_2();
int main()
{
char like_to_continue;
int process_number;
do
{
cout << "Type a menu option number and press enter" << endl;
cout << "[1] Do First Process" << endl;
cout << "[2] Do Second Process" << endl;
cout << "[-->";
cin >> process_number;
if (process_number == 1)
do_process_1();
else if (process_number == 2)
do_process_2();
else
cout << "Unknown process!" << endl;
cout << endl << "Would you like to continue (y/n)?";
cin >> like_to_continue;
} while (like_to_contine == 'y' || like_to_continue == 'Y');
return 0;
}
int do_process_1()
{
cout << "Doing first process ... DONE (that was quick)" << endl;
return 0;
}
int do_process_2()
{
cout << "Doing second process ... DONE (that was quick)" << endl;
return 0;
}
// ------------------------------------------------------------------
This is untested code, but I think it will work. =) Hope this helps!//sketchy program:
char * function() {
char n[10];
strcpy(n, "Monday");
return n;
}
main() {
char a[10];
strcpy(a, function());
cout << a << '\n'; //prints "Mond"
}
A: You're lucky you get anything back at all from that function :). The reason being is that 'char n[10]' is a LOCAL variable. This means that the data and the variable itself is trashed when the function returns. You were lucky enough that four of the original characters were still in the same place when you got back to "main()" =). To solve this you can make 'n' in 'function()' not die until the program exits. Simply put the word "static" in front of "char n[10]" and this program will work.// ------------------------------------------------------------------
#include <iostream.h>
int counter();
int main()
{
cout << "The counter is " << counter() << endl;
cout << "The counter is " << counter() << endl;
cout << "The counter is " << counter() << endl;
cout << "The counter is " << counter() << endl;
cout << "The counter is " << counter() << endl;
cout << "The counter is " << counter() << endl;
return 0;
}
int counter()
{
static int keep_count=-1; //initialize keep_count to minus 1
return (++keep_count); //add one to keep_count and return it
}
// ------------------------------------------------------------------
The value of keep_count is not destroyed or reinitialized to '-1' even after the function returns. This means that when you call it a second time its value is zero and becomes one which is returned. The third time its value is one and becomes two, etc. etc.Page Content & Design ©1998 By Neil C. Obremski |
![]() home |
![]() prfce |
![]() loop |
![]() refs |
![]() ptrs |
![]() struct |
![]() class |
![]() array |
![]() rsrc |
![]() site |
![]() indx |