[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

problem with copying Arrays!!!!!  HELP PLEASE!!!

Posted on 1998-11-29
5
Medium Priority
?
181 Views
Last Modified: 2011-09-20
I have an array that was read in under int main()
and it read in 25 integers into an array called src
I have to copy this array into one called dst
I tried to do this:

void copyArray(const int src[], int dst[], int sz0{
     for(int i=0; i < sz; i++){
         dst[i]=src[i];
     }//for loop ends here

Why doesn't this work?  What do I need to do!  This is something I need answered today if possible!  Any help would be appreciated very much!
0
Comment
Question by:snmeek0
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Accepted Solution

by:
vmehro earned 100 total points
ID: 1178811
sz0 or sz?
0
 

Author Comment

by:snmeek0
ID: 1178812
It should be sz

my code is as follows:

#include <iostream.h>
#include <fstream.h>
#include <stlib.h>

void copyArray(const int src[], int dst[], int sz);
void printArray(const int A[], int sz, int nPerLine);

int main(){
   ifstream inFile;
   inFile.open("dataFile");
   
if(inFile.fail()){
   cout << "opening failed.  Exiting...\n";
   exit(1);
}

int src[25], dst[25], i , sz=0, A[25], nPerLine;
while(sz<25){
   inFile >> src[i];
   cout << "src[" << sz << "]=" << src[i] << endl;
   sz++;
}//while loop end

copyArray(src, dst, sz);
printArray(A, sz, nPerLine);

}//main

void copyArray(const int src[], int dst[], int sz){
   for(int i=0; i<sz, i++){
       dst[i]=src[i];
       cout << "dst[" << i << "]=" << dst[i] << "  ";
       cout << "src[" << i << "]=" << src[i] << endl;
   }//for loop
}//copyArray
void printArray(const int A[], int sz, int nPerline){
   cout << "enter # array elements to print per line: ";
   cin >> nPerLine;
   for(int j-0; j<nPerLine; j++){
      cout << "A[" << j << "]=" << A[j] << " ";
      if(j==nPerLine){
          cout << endl;
      }//if
   }//for
}//printArray


It reads in everything fine in main.

The output from main is corect.

Then when it gets to void copyArray, the output for dst[0] and src[0] are both 9, which is the last # read in from the file. (ie dst[24] and src[24] both are 9.
All others are 0.  

Where did I go wrong???
0
 

Author Comment

by:snmeek0
ID: 1178813
It should be sz

my code is as follows:

#include <iostream.h>
#include <fstream.h>
#include <stlib.h>

void copyArray(const int src[], int dst[], int sz);
void printArray(const int A[], int sz, int nPerLine);

int main(){
   ifstream inFile;
   inFile.open("dataFile");
   
if(inFile.fail()){
   cout << "opening failed.  Exiting...\n";
   exit(1);
}

int src[25], dst[25], i , sz=0, A[25], nPerLine;
while(sz<25){
   inFile >> src[i];
   cout << "src[" << sz << "]=" << src[i] << endl;
   sz++;
}//while loop end

copyArray(src, dst, sz);
printArray(A, sz, nPerLine);

}//main

void copyArray(const int src[], int dst[], int sz){
   for(int i=0; i<sz, i++){
       dst[i]=src[i];
       cout << "dst[" << i << "]=" << dst[i] << "  ";
       cout << "src[" << i << "]=" << src[i] << endl;
   }//for loop
}//copyArray
void printArray(const int A[], int sz, int nPerline){
   cout << "enter # array elements to print per line: ";
   cin >> nPerLine;
   for(int j-0; j<nPerLine; j++){
      cout << "A[" << j << "]=" << A[j] << " ";
      if(j==nPerLine){
          cout << endl;
      }//if
   }//for
}//printArray


It reads in everything fine in main.

The output from main is corect.

Then when it gets to void copyArray, the output for dst[0] and src[0] are both 9, which is the last # read in from the file. (ie dst[24] and src[24] both are 9.
All others are 0.  

Where did I go wrong???
0
 
LVL 84

Expert Comment

by:ozo
ID: 1178814
line 28: warning(3665): variable "nPerLine" is used before its
          value is set
       printArray(A, sz, nPerLine);
                         ^

line 33: error(3158): expected a ";"
          for(int i=0; i<sz, i++){
                                ^

line 42: error(3158): expected a ";"
          for(int j-0; j<nPerLine; j++){
                   ^

line 42: warning(3665): variable "j" is used before its value is set
          for(int j-0; j<nPerLine; j++){
                       ^


0
 
LVL 1

Expert Comment

by:The_Brain
ID: 1178815
I would do this

while (!input.eof())
{
   src=input.getline(sourcefile);
   strcpy(dst, src, howevermany.)
   
}
just a suggestion.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question