Agni 370 Practically a Master Poster Featured Poster

To give you a brief idea. When you allocate an array you are actually reserving some memory for storing data. now lets c

when you say

int Quantity[6]; you have reserved memory to store 6 int data type. This memory is allocated serially one after the other. so you can imagine it to be 6 blocks-each capable of holding an 'int'-placed one after another.

not how do you access each of these blocks? luckily for you have numbers assigned to each of them(for now you can take it this way, once you learn pointers you'll see exactly how it works) so you have 0 for the first block, 1 for the second block and so on. Thus to either extract the data from a particular location or to insert data into a particular location all you need to do is, go to that memory location using it's number. Quantity[0] for 1st location. now its easy, you can go to any location, get data/put data do whatever you want to :) ...

Note: this was just to get you imagining on the lines. now you can read furthur to get the nitty gritty... you might start enjoying c++

Agni 370 Practically a Master Poster Featured Poster

Post one such program where you used an array and it didnt work. we'll try and help you find the mistakes.

Agni 370 Practically a Master Poster Featured Poster

well there again a lot depends on what you are trying to achieve. but i can give you some examples.

->setmethods a usually void. all that this method does is set the value of a member variable. hence you dont need to return anything.
->as i had written earlier functions where the parameters are huge we prefer to pass them by reference and make the function as void. In that case we will avoid copying of the variables.
->A return value function example would be the 'getmethods' which you would use to retrive the value of some member variable that you would have set earlier.
->and then most of the time when you want some value from the function to be used in your function you will need a return-value.

Agni 370 Practically a Master Poster Featured Poster

There are a lot of considerations but to start with you can use one rule.

-> when you dont want to change the actual variable, you pass by value. in this case in a copy of the variable will be made in the called function and hence the original value will remain same.
-> when you want the value of the actual variable to change then you pass by reference. whatever changes you make to the value in the called function will change the value of the original variable in the calling function.
-> whenever the data passed is large, for examples structures or objects we prefer passbyref as a new copy will not be made.

you can explore more now...

Agni 370 Practically a Master Poster Featured Poster

Hi,

Suppose I have a character array array1[]={A,B,B,B,A,A}. Now I want to declare a structure with a pointer in it and make each array1 element point to the previous element.For example last array1 element 'A' will point to the previous 'A' which will point to 'B' and so on. Can somebody please help me with the coding?

Any quick help much appreciated.

Thank you

i think your prof wanted you to use link list. in a link list you can keep the pointer to the next element.

struct node
{
char a;
node* p;
}

here you can assign the valus of 'a' from the array element and then keep a pointer to the next element.

Agni 370 Practically a Master Poster Featured Poster

cool...

Agni 370 Practically a Master Poster Featured Poster

I dont think so. just put the loop and print each element. however you can format the output in any way, everything in 1 line, separate lines, tabs etc.

Agni 370 Practically a Master Poster Featured Poster

cout << data[index]

Agni 370 Practically a Master Poster Featured Poster

you have c1,c2,c3 declared in global as int then inside the fn as char and then again as int... what r u trying to do? you declare them as char then you pass them to cResist which takes int, so it has the ASCII values and then you pass them to another fn and use them in a switch ... i think the first step should be to understand what kind of var you need and declare them properly..

Agni 370 Practically a Master Poster Featured Poster

one correction ..

this.m[x] = c;

should be

this->m[x] = c;

Agni 370 Practically a Master Poster Featured Poster

Collect add;
add.director=director+c.director; \\string
add.title=title+c.title; \\string
add.genre=genre+c.genre; \\string
add.length=length+c.length; \\int
add.rating=rating+c.rating; \\string
add.release=release+c.release; \\string

collect class doesnt seem to have these members at all(director,title etc..)

what you should, if you have no option but to use the operator overloading

void Collect::operator+(const Movie& c){

this.m[x] = c; 

;}

i dont think you need to return anything either in this case, simply add the movie to the array ... you will have to keep a tab of the number of elems in the array so that you add it at the right position..

Agni 370 Practically a Master Poster Featured Poster

To me the usage of operator overloading to do this doesnt seem good.. you have a collect class and that has an array of movie class. simply write an addMovie function in collect class, pass on the movie object to it and insert it in the array. neat and much easy to understand. have you been speicifcally asked to do it using operator overloading?

blcase commented: really took the time and tried to solve my problem....thanks! +1
Agni 370 Practically a Master Poster Featured Poster

deleted what? ... you dont want to sol any more?

Agni 370 Practically a Master Poster Featured Poster

when you add the next movie to the same collect object doesnt it override the existing values? how are you storing it? if possible plz post a bigger fragment of the code, its difficult to understand like this ...

Agni 370 Practically a Master Poster Featured Poster

dont seem to get it ... you have a class called collect.. the attributes if that class are initialized from some attributes of the movie class, rt? where is the array? can you put the code where you are using this operator?

Agni 370 Practically a Master Poster Featured Poster

well i'm assuming that you have done the rest of the code correctly..... and if you can tell me the error it's throwing then i might help.. but as far as ur initial qs is concerned the proposed sol will work..

Agni 370 Practically a Master Poster Featured Poster

Inside the Inner Loop

for (i=0; i < 3; i++){

  for (j = 0; j < 3; j++)
    {
        inFile >> matrix[i][j];
        printf("\n%f\t%f\t%f", matrix[i][j]);
    }
}

like this..

Agni 370 Practically a Master Poster Featured Poster

it doesn't say what are the number of rows or columns in the matrix...

Agni 370 Practically a Master Poster Featured Poster

sorry for the repetition, i guess i didnt refresh for long :) ...

Agni 370 Practically a Master Poster Featured Poster

as user for i/p i meant 'ask' user for i/p

btw if matrix size is undefined at compile time then simple array will not work as you wont know the dimensions..

Agni 370 Practically a Master Poster Featured Poster

declare a 2 dim array.
inside the inner loop, as user for i/p and keep storing in each element of the array.

--post the code inside code tags

Agni 370 Practically a Master Poster Featured Poster

dude i just executed your code .. n it was as smooth as fine scotch on rocks ... no segmentation faults... wish i could help...

Agni 370 Practically a Master Poster Featured Poster

printf("%d\t", matrix[j]); shd be inside the inner loop ...

Agni 370 Practically a Master Poster Featured Poster

without trying to understand the logic i can tell u a few things ..

> change 'main' to 'int main'

>
change
nWords = numWordsInFile( textFile, nWords);
to
only
numWordsInFile( textFile, nWords);
&
int numWordsInFile( ifstream &in, int &nWords)
to
void numWordsInFile( ifstream &in, int &nWords);

as here you'r passing nWords by reference so you dont need to return anything from this function, nWords will automatically have the calculated value as its a reference.

similary for the other function also.

3> no need to create local variables in these functions, manipulate the reference directly.

Agni 370 Practically a Master Poster Featured Poster

exactly thats y i mentioned that use environment variables like $MYPATH but yes its always there that you might change the drive in which you place the code etc. but after a point the dir structure has to be the same as urs. i mean suppose you put it in

"e:/linux/eclipse/workspace/" and set $MYPATH as e:/linux then ur makefile will have

$MYPATH/workspace

now the other person might store it in

"c:/temp/project/workspace"

he can set $MYPATH as c:/temp/project and the makefile will work for him provided the final folder is same as urs, which is workspace in this case..

got it?

Agni 370 Practically a Master Poster Featured Poster

Then put it in the makefile and give the makefile with the source code for compilation and yes to make sure the makefile works for anyone, make sure the paths are not hardcoded. use environment variables like $PATH etc. so that it works for anyone in any directory structure..

Agni 370 Practically a Master Poster Featured Poster

either you give the complete path in the include statement. or else what you can do is that you can add the complete path in the include path of the compiler. in command prompt we do it by using the compiler option -I. eg

gcc -I<path> file.cpp

you can set this in the makefile or
if its GUI there must be some option to set this path for the compiler.

Agni 370 Practically a Master Poster Featured Poster

when you declare a function as const, it implies that you cannot change the state of the object inside that function. that means you cannot change the value of any member variable inside the function and you cannot make a call to any non-const function either. that is y its not allowing you to modify 'x' inside add, however when your remove the const your are able to modify it. it has nothing to do with the variable being private, even if it was public it would not have allowed you to modify it.

superjacent commented: Very helpful. +1
Agni 370 Practically a Master Poster Featured Poster

the correct condition is

if((yy%4 == 0) && !(yy%100 ==0) || (yy%400 == 0))

probably you can have a while loop to keep taking inputs and returning the number of days till the user says i'm done.

so all your code can be put inside a while loop and you can ask the user if he wants to continue or not.

Agni 370 Practically a Master Poster Featured Poster

Its kinda long but here it is...

// Program assignment #1
// Brandon Moffett

#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

// function prototypes

int main()
{
    
    int mm,dd,yy,mm2,dd2,yy2,maxdays,day;
    char s,s2;
//prompt the user for input
cout << "Please input your birthday using this format (MM-DD-YYYY)\n";
cin >> mm;
cin.get(s);
cin >> dd;
cin.get(s);
cin >> yy;
cout << "Please input the current date using this format (MM-DD-YYYY)\n";
cin >> mm2;
cin.get(s2);
cin >> dd2;
cin.get(s2);
cin >> yy2;

  switch (mm)
     {
     case 1: maxdays = 31;
          if (dd <= maxdays)
             cout << "Dates are acceptional, please continue...\n";
             else
                 cout << "Invalid day of the month : " << dd << endl;
                 break;
     case 3:maxdays = 31;
          if (dd <= maxdays)
             cout << "Dates are acceptional, please continue...\n";
             else
                 cout << "Invalid day of the month : " << dd << endl;
                 break;
     case 5:maxdays = 31;
          if (dd <= maxdays)
             cout << "Dates are acceptional, please continue...\n";
             else
                 cout << "Invalid day of the month : " << dd << endl;
                 break;
     case 7:maxdays = 31;
          if (dd <= maxdays)
             cout << "Dates are acceptional, please continue...\n";
             else
                 cout << "Invalid day of the month : " << dd << endl;
                 break;
     case 8:maxdays = 31;
          if (dd <= maxdays)
             cout << "Dates are acceptional, please continue...\n";
             else
                 cout << "Invalid day of the month : " << dd << endl;
                 break;
     case 10:maxdays = 31;
          if (dd <= maxdays)
             cout << "Dates are …
Agni 370 Practically a Master Poster Featured Poster

can you post that code? i dont know y you need the loop...

Agni 370 Practically a Master Poster Featured Poster

may b u can try opening the file in the append mode. in that case it would not overwrite

Agni 370 Practically a Master Poster Featured Poster

Just create file before the start of loop. inside the loop, keep writing to it. after the loop close it. have u tried reading the documentation for ofstream or fopen ? i guess you shd do that now

Agni 370 Practically a Master Poster Featured Poster

just type 'resize + array' in the search text box and you will find the answer.

Agni 370 Practically a Master Poster Featured Poster

you have functions like fopen, fclose,gets,puts etc for file manipulations, provided by the standard libraries. look up in some documentation.

Agni 370 Practically a Master Poster Featured Poster

you can open a text file and write into it. that shd work i guess.