C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 4th, 2008, 06:43 PM
kage207 kage207 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2008
Posts: 1 kage207 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 23 sec
Reputation Power: 0
2D Arrays and functions

Quote:
/* This program creates a 2-D array with a max size of 20x20. These squares are 'magic'. Each number is presented once from 1-nth times in the magic square. */

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

int readMagic(ifstream ins, int &magic[], int sides);

void printMagic(int &magic[], int sides, ifstream &ins);
int main() {
int magic[20][20],
ifstream ins;
int sides ;
ins >> sides;
if(sides > 25)
{
cout << "The length of the square is too large." <<endl ;
cout << "Tty a new file." << endl ; return 0 ; } cout << "What file do you want to process? ";
cin >> filename; ins.open(filename.c_str()); if (ins.fail()) { cout << endl ; cout << "Bad file name" << endl ; return 0; } readMagic(ins, magic, sides) ; printMagic(magic, sides, ins) ; } int readMagic(ifstream ins, int magic[], int sides) { for(int col = 0; col < sides; col++) { for(int row = 0; row < sides; row++) { ins >> magic[col][row]; } } return magic[] ; } void printMagic(int magic[], int sides, ifstream &ins) { for(int col = 0; col < sides; col++) { for(int row = 0; row < sides; row++) { ins >> magic[col][row]; } } }


I'm trying to pass a 2D array into the functions so I can read numbers from a file into the array. The first ins >> sides means the first number determines the dimensions of the square. So if it is 5 then it's going to be a 5 by 5 triangle. Right now I'm getting errors saying

Quote:
1>------ Rebuild All started: Project: Assign11, Configuration: Debug Win32 ------ 1>Deleting intermediate and output files for project 'Assign11', configuration 'Debug|Win32' 1>Compiling... 1>Assign11.cpp 1>c:\temp\assign11\assign11\assign11.cpp(14) : error C2234: 'magic' : arrays of references are illegal 1>c:\temp\assign11\assign11\assign11.cpp(16) : error C2234: 'magic' : arrays of references are illegal 1>c:\temp\assign11\assign11\assign11.cpp(21) : error C2146: syntax error : missing ';' before identifier 'ins' 1>c:\temp\assign11\assign11\assign11.cpp(21) : error C2065: 'ins' : undeclared identifier 1>c:\temp\assign11\assign11\assign11.cpp(24) : error C2065: 'ins' : undeclared identifier 1>c:\temp\assign11\assign11\assign11.cpp(36) : error C2065: 'filename' : undeclared identifier 1>c:\temp\assign11\assign11\assign11.cpp(37) : error C2065: 'ins' : undeclared identifier 1>c:\temp\assign11\assign11\assign11.cpp(37) : error C2228: left of '.open' must have class/struct/union 1> type is ''unknown-type'' 1>c:\temp\assign11\assign11\assign11.cpp(37) : error C2065: 'filename' : undeclared identifier 1>c:\temp\assign11\assign11\assign11.cpp(37) : error C2228: left of '.c_str' must have class/struct/union 1> type is ''unknown-type'' 1>c:\temp\assign11\assign11\assign11.cpp(39) : error C2065: 'ins' : undeclared identifier 1>c:\temp\assign11\assign11\assign11.cpp(39) : error C2228: left of '.fail' must have class/struct/union 1> type is ''unknown-type'' 1>c:\temp\assign11\assign11\assign11.cpp(46) : error C2065: 'ins' : undeclared identifier 1>c:\temp\assign11\assign11\assign11.cpp(47) : error C2065: 'ins' : undeclared identifier 1>c:\temp\assign11\assign11\assign11.cpp(56) : error C2109: subscript requires array or pointer type 1>c:\temp\assign11\assign11\assign11.cpp(60) : error C2059: syntax error : ']' 1>c:\temp\assign11\assign11\assign11.cpp(69) : error C2109: subscript requires array or pointer type 1>Build log was saved at "file://c:\temp\Assign11\Assign11\Debug\BuildLog.htm" 1>Assign11 - 17 error(s), 0 warning(s) ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Reply With Quote
  #2  
Old December 4th, 2008, 10:52 PM
tinit5190 tinit5190 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2008
Posts: 21 tinit5190 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 50 m 40 sec
Reputation Power: 0
That last chunk of text is gross, would you be able to space that out or is it too large?
That is in the actual code, and not the errors.

From what I can see right now, the main problem is that you are passing your array into the function via single dimension. You have

Code:
int readMagic(ifstream ins, int &magic[], int sides);

which should be
Code:
int readMagic(ifstream ins, int &magic[20][20], int sides);


another error is that you are calling a function before you are actually defining what the array is. You should make it standard that your variable declarations always come right after
int main()

Jeez, i'm just picking this apart.
your int readMagic is above the int main(), but you have no variable that "readMagic" is going to be stored into

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > 2D Arrays and functions


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT