473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem Updating An 2D Array

Hi, my program is suppost to read a txt file with a 9x9 table of 1 digit number like this:
9 6 0 5 0 0 0 8 3
3 0 0 2 7 9 0 0 4
0 0 2 6 0 0 0 9 0
0 9 0 0 0 6 7 0 0
7 4 0 0 0 0 0 5 1
0 0 8 4 0 0 0 2 0
0 7 0 0 0 5 4 0 0
6 0 0 9 1 4 0 0 5
5 1 0 0 0 8 0 3 2

and then enter each value into a 2d array - rooms[9][9], which was declared in rooms.h:
'static int Game::rooms[9][9];'
and defined with the line in rooms.cpp:
'int Game::rooms[9][9];'
The definition of read() function was placed in rooms.cpp:
Expand|Select|Wrap|Line Numbers
  1. void Game::Read(string n)
  2. {
  3.     string Name = n;               
  4.  
  5.     ifstream I;            
  6.     I.open(Name.c_str());
  7.  
  8.     while (!I.eof())
  9.     {
  10.         int Num;
  11.         for (int i=0; i<r; i++)
  12.         {
  13.             for(int j=0; j<c; j++)
  14.             {
  15.                 I >> Num;
  16.                 rooms[i][j] = Num;
  17.             }
  18.         }
  19.     }
  20.     I.close(); 
  21.  
After it finished entering it will display the updated room array in console. For some reason the result shown became all '2's instead of a replica of the one in the txt file. The debugger showed Num is modified as it reads each number in the txt but for some reason it didn't get entered into the array slot? And I've looked for the value 2 in every identifier but it seems it came out of no where. I've tried puting 'rooms[i][j] = 1' instead of Num, and the new array shown indeed are all 1s. It seems it doesn't like rooms[][] to be set to a variable. What did I missed?
Dec 13 '06 #1
1 1207
Hi, my program is suppost to read a txt file with a 9x9 table of 1 digit number like this:
9 6 0 5 0 0 0 8 3
3 0 0 2 7 9 0 0 4
0 0 2 6 0 0 0 9 0
0 9 0 0 0 6 7 0 0
7 4 0 0 0 0 0 5 1
0 0 8 4 0 0 0 2 0
0 7 0 0 0 5 4 0 0
6 0 0 9 1 4 0 0 5
5 1 0 0 0 8 0 3 2

and then enter each value into a 2d array - rooms[9][9], which was declared in rooms.h:
'static int Game::rooms[9][9];'
and defined with the line in rooms.cpp:
'int Game::rooms[9][9];'
The definition of read() function was placed in rooms.cpp:
Expand|Select|Wrap|Line Numbers
  1. void Game::Read(string n)
  2. {
  3.     string Name = n;               
  4.  
  5.     ifstream I;            
  6.     I.open(Name.c_str());
  7.  
  8.     while (!I.eof())
  9.     {
  10.         int Num;
  11.         for (int i=0; i<r; i++)
  12.         {
  13.             for(int j=0; j<c; j++)
  14.             {
  15.                 I >> Num;
  16.                 rooms[i][j] = Num;
  17.             }
  18.         }
  19.     }
  20.     I.close(); 
  21.  
After it finished entering it will display the updated room array in console. For some reason the result shown became all '2's instead of a replica of the one in the txt file. The debugger showed Num is modified as it reads each number in the txt but for some reason it didn't get entered into the array slot? And I've looked for the value 2 in every identifier but it seems it came out of no where. I've tried puting 'rooms[i][j] = 1' instead of Num, and the new array shown indeed are all 1s. It seems it doesn't like rooms[][] to be set to a variable. What did I missed?

Hi,

I some how don't want to do any thing in C++. Dont know if the below C code is going to help you or not just give it a try..

#include<stdio.h>
void main()
{
int i,j,a[10][10],num;
FILE *f1;
f1 = fopen("c:/input.txt","r");
for(i=0;i<=9;i++)
for(j=0;j<=9;j++)
{
fscanf(f1,"%d",&num);
a[i][j]=num;
}
for(i=0;i<=9;i++)
{
for(j=0;j<=9;j++)
printf("%d ",a[i][j]);
printf("\n");
}
fclose(f1);
}

what say :)

Regards,
ShaggY@FtF
Dec 13 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Sure | last post by:
Hello All, I want to update a form using the LWP & HTTP method. It was working fine when I am updating the values like this $ua = LWP::UserAgent->new; $url...
14
by: Lars Netzel | last post by:
A little background: I use three Datagrids that are in a child parent relation. I Use Negative Autoincrement on the the DataTables and that's workning nice. My problem is when I Update these...
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
5
stepterr
by: stepterr | last post by:
I have a form that is built based on a query. Everything is working except when I submit the form the radio buttons are only updating the first row in my database. dcategory and dthumbnail are two...
2
by: sirdavethebrave | last post by:
Hi guys - I have written a form, and a stored procedure to update the said form. It really is as simple as that. A user can go into the form, update some fields and hit the update button to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.