473,503 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Whats the problem with this code

48 New Member
HI,

I am reading a text file and then adding 1 to each element and displaying it.

the code is as follows:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

int main ()
{
string line;
ifstream fnamet("abc.txt");
int Rows=0,Columns=0;
int relR,relG,relB,count;
if (fname.is_open())
{
while (! fname.eof() )
{
int i=0,a[4];//taken no of columns < =4
getline (fname,line);
istringstream tSS( line );
while( ! tSS.eof())
{
tSS>>a[i];
i++;
}

//if(Columns<2&&i>1) Columns=i;
if(i>3)
{
R=a[0]+1;
G=a[1]+1;
B=a[2]+1;
count=a[3];


printf("%d %d %d %d\n",R,G,B,a[3]);
}
}
printf("Rows=%d Columns=%d\n",histRows,histColumns);

fname.close();
}
else cout << "Unable to open file";
return 0;
}

Tha above code doesnt show any problem and shows the output.
Now I need to enter those values in a array as follows.
array[R][G][b] = count;
and my array size is 128*128*128.
If i am trying to do above, it doesnt show any compiling errors, but output is not generated. Shows that .exe file stopped working.

Pls help me how to achieve this.

Thanks,
Rishi
Jun 29 '07 #1
9 1495
niskin
109 New Member
ifstream fnamet("abc.txt");
int Rows=0,Columns=0;
int relR,relG,relB,count;
if (fname.is_open())
{
while (! fname.eof() )
Here you have a file pointer called fnamet, but your loops are to do with a filepointer called fname.
Jun 29 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
This says you are using Windows:
#include "stdafx.h"
so you should be using the RGB structure of Win32.
Jun 30 '07 #3
flavourofbru
48 New Member
Yes I am using windows.

So wats the problem with the code.
Though it doesnt show any errors and gets compiled.
When showing the output it simply outputs .exe file stopped working.

Please help.

Thanks!!
Jun 30 '07 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
Better send your stdafx.h so I can get this to compile.

Also, I need a sample of your input file.
Jul 1 '07 #5
flavourofbru
48 New Member
Hi,
Here is my stdafx.h file

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once


#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <stdio.h>
#include <tchar.h>



// TODO: reference additional headers your program requires here

and my sample text file is as follows:

12 23 45 65
89 34 9 43
95 32 65 39

pls help!!
Thanks!!
Jul 2 '07 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
Expand|Select|Wrap|Line Numbers
  1. //#include "stdafx.h"
  2. #pragma once
  3.  
  4.  
  5. #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
  6. #include <stdio.h>
  7. #include <tchar.h>
  8.  
  9. #include <iostream>
  10. #include <fstream>
  11. #include <string>
  12. #include <sstream>
  13. using namespace std; 
  14.  
  15. int main () 
  16. {
  17. string line;
  18. //ifstream fnamet("abc.txt");
  19. ifstream fname("c:\\scratch\\instructor\\abc.txt");
  20. int Rows=0,Columns=0;
  21. int relR,relG,relB,count;
  22. if (fname.is_open())
  23. {
  24. while (! fname.eof() )
  25. {
  26. int i=0,a[4];//taken no of columns < =4
  27. getline (fname,line);
  28. istringstream tSS( line );
  29. while( ! tSS.eof())
  30. {
  31. tSS>>a[i];
  32. i++;
  33. }
  34.  
  35. //if(Columns<2&&i>1) Columns=i;
  36. if(i>3)
  37. {
  38. /*
  39. R=a[0]+1;
  40. G=a[1]+1;
  41. B=a[2]+1;
  42. count=a[3];
  43.  
  44.  
  45. printf("%d %d %d %d\n",R,G,B,a[3]);
  46. */
  47. relR=a[0]+1;
  48. relG=a[1]+1;
  49. relB=a[2]+1;
  50. count=a[3];
  51.  
  52.  
  53. printf("%d %d %d %d\n",relR,relG,relB,a[3]);
  54.  
  55. }
  56. }
  57. //printf("Rows=%d Columns=%d\n",histRows,histColumns);
  58. printf("Rows=%d Columns=%d\n",Rows,Columns);
  59.  
  60. fname.close();
  61. }
  62. else cout << "Unable to open file"; 
  63. return 0;
  64. }
  65.  
This does what you programmed. I didn't know what histRows and histColimns are so I fudged it.
Jul 2 '07 #7
flavourofbru
48 New Member
Hi,

Thanks for the help. The histRows and HistColumns are just the Rows and Columns.

The above program works properly. The problem is coming when I am trying to do the following. I am adding the following code in this place

if(i>3)
{

relR=a[0]+1;
relG=a[1]+1;
relB=a[2]+1;
count=a[3];

/*add the code at this place
initialize a 3D array of size 128*128*128 and at this place add the following
array_3D[relR][relG][relB] = count; */

printf("%d %d %d %d\n",relR,relG,relB,a[3]);

}

There are no compiling errors but while looking at the output it shows .exe files stopped working.

Please help.

I need to enter the count values into a 3d array at the position of array_3d[relR][relG][relB].

Thanks!!
Jul 2 '07 #8
weaknessforcats
9,208 Recognized Expert Moderator Expert
128 x 128 x 128 = 294912.

As an array this is fine. As a stack array, this is way too big.
Expand|Select|Wrap|Line Numbers
  1. int array[128][128][128];   //wrong! probably exceeds your stack memory allowance
  2. int (* array1)[128][128] = new int[128][128][128]; //OK
  3.  
Jul 2 '07 #9
flavourofbru
48 New Member
HI Thanks for the help!!
It worked.
Jul 4 '07 #10

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

Similar topics

4
4057
by: asdf | last post by:
Hello! Can someone tell me whats wrong with this piece of code: Option Compare Database Option Explicit Sub retrieve() Dim rst As ADODB.Recordset Dim i As Integer
21
2005
by: Paul Tremblay | last post by:
Hi All, I am a veteran C/C++ programmer (Unix background) and I want to get to speed with Visual Studio .Net I have legacy C/C++ code that I want to use in my application. However, I'm not...
2
2561
by: Showjumper | last post by:
Whats the diff between the 2 and when do you use one and not the other?
8
1606
by: buc | last post by:
I have a simple combox on the screen that is bound via a datareader to a stored proc in sql that returns a simple string. The code is 'load stored proc then dReader =...
3
1746
by: Vaidas Gudas | last post by:
I has web project on virtual pc, maked with framework 2.0. there I was used the method role.roleexists("admin") and everything was worked good. but when i replace this project on my local machine,...
4
2757
by: sophie | last post by:
Whats going on here: Read in a number as a string: scanf("%s", &number); number = 12345, for arguements sake Print it like this its fine:
2
8836
by: Aaron Gray | last post by:
Whats going on with setAttribute on IE it appears to work on some examples and working code but not on other code that I am writting ? <style> .foo { font-size: 200%; } </style>
4
3074
by: LoneHunter01 | last post by:
Basically, I just need a general direction on where to go for this. Yes, this is for a school project, though it's strictly an optional one (and I have tried many solutions, one in-depth). We've...
5
2299
by: islayer | last post by:
can someone tell me what is wrong with the bold code? i am just learning perl. the program should create a perl file with a random name (5 letters, followed by a number), but the name is always just...
1
1419
by: x40 | last post by:
I try to learn python thru solving some interisting problem, found google trasure hunt, write first program ( but cant find whats wrong). # Unzip the archive, then process the resulting files to...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7274
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
7323
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...
0
7453
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5005
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4670
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3162
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.