| | |
2d Arrays e FUSSY - Store data in 1d area
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2009
Posts: 149
Reputation:
Solved Threads: 7
hello
I am reading a text file & trying to store some information into a 2d array.
My problem is when I go to store a piece of data into the 1st row (1d area ?
) I get this error
error:
My function:
I am reading a text file & trying to store some information into a 2d array.
My problem is when I go to store a piece of data into the 1st row (1d area ?
) I get this errorerror:
•
•
•
•
incompatible types in assignment of `float' to `float[25]'
•
•
•
•
For eg, I want to store 25.5 where x is
pur[x][];
C++ Syntax (Toggle Plain Text)
float pur[53][25]; // Purchases Array [week number][purchase no.1, p2, pn...] ifstream infile; float x; int count = 0; int count2 = 0; while (infile >> x) { pur[count] = x; // error occurs here while (x!=(char)'\n') { pur[count][count2]= x; count2++; } count2 = 0; count++; }
Last edited by gretty; Aug 28th, 2009 at 10:24 pm.
C++ Syntax (Toggle Plain Text)
float pur[53][25]; // ... pur[count] = x; // error occurs here
C++ Syntax (Toggle Plain Text)
pur[count][count2]= x;
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Improper use of an array,
Try,
C++ Syntax (Toggle Plain Text)
pur[count] = x; // Error
C++ Syntax (Toggle Plain Text)
while (infile >> x) { if(count2==25) { count2=0; count++; } pur[count][count2]= x; count2++; }
Failure is not fatal, but failure to change might be. - John Wooden
•
•
Join Date: Apr 2009
Posts: 149
Reputation:
Solved Threads: 7
•
•
•
•
Improper use of an array,
Try,C++ Syntax (Toggle Plain Text)
pur[count] = x; // Error
C++ Syntax (Toggle Plain Text)
while (infile >> x) { if(count2==25) { count2=0; count++; } pur[count][count2]= x; count2++; }
so I cant cant go ...
cout << pur[x];
& it will output 25.5
Also, from experience. I see that using 2D array to store information
from a file does not always work well. The program might not set
the file content it to proper location. I would suggest to use 1D array.
If 2D is really needed , then use 1D array, and copy it into 2D array.
from a file does not always work well. The program might not set
the file content it to proper location. I would suggest to use 1D array.
If 2D is really needed , then use 1D array, and copy it into 2D array.
1) What word becomes shorter if you add a letter to it?
[ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666]
2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
[*solved by : murtan, xavier666]
3) What is the 123456789 prime numer?•
•
Join Date: Apr 2009
Posts: 149
Reputation:
Solved Threads: 7
Thanks, lol, I realise now I can do this so I have changed it. But now I have run into another problem.
I am tring to convert x, which is a float into an int. but I get this error
Is there a way I can fix this without changing x to a 'const char' or 'char', ie, keep it as a float.
I am tring to convert x, which is a float into an int. but I get this error
•
•
•
•
cannot convert `float' to `const char*' for argument `1' to `int atoi(const char*)'
C++ Syntax (Toggle Plain Text)
while (infile >> x) { count = atoi(x); // error occurs here again while (x!=(char)'\n' || count2<=25) { pur[count][count2]= x; count2++; } count2 = 0; count++; }
Last edited by gretty; Aug 28th, 2009 at 11:24 pm.
•
•
Join Date: Apr 2009
Posts: 149
Reputation:
Solved Threads: 7
•
•
•
•
Also, from experience. I see that using 2D array to store information
from a file does not always work well. The program might not set
the file content it to proper location. I would suggest to use 1D array.
If 2D is really needed , then use 1D array, and copy it into 2D array.
, I agree, 2d arrays are tedius, I wanted to test myself by using one(2d array) in a program but I dont really HAVE to use a 2d array. I am probably going to use a vector because it will be alot more efficient (memory-wise) & alot easier Last edited by gretty; Aug 28th, 2009 at 11:22 pm.
" I am tring to convert x, which is a float into an int. but I get this error"
convert float to int :
you use atoi ( a string to int ). Converts a string to int.
Also atoi is not a good idea. Use the standard sstream header to convert between numbers to string vice-versa.
convert float to int :
C++ Syntax (Toggle Plain Text)
float pi = 3.14159265; int pie1 = pi; //implicit conversion from float to int int pie2 = int(pi); //explicit conversion from float to int, C++ style //recommended int pie3 = (int)pi; //explicit conversion from float to int, C style
you use atoi ( a string to int ). Converts a string to int.
C++ Syntax (Toggle Plain Text)
int i = atoi("100");
Also atoi is not a good idea. Use the standard sstream header to convert between numbers to string vice-versa.
Last edited by firstPerson; Aug 28th, 2009 at 11:53 pm.
1) What word becomes shorter if you add a letter to it?
[ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666]
2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
[*solved by : murtan, xavier666]
3) What is the 123456789 prime numer?•
•
Join Date: Apr 2009
Posts: 149
Reputation:
Solved Threads: 7
thanks guys
Although I am back again with a problem, I have totally overhauled the whole function but it still uses a 2d array.
In the below code, the variable 'count' is never advanced, ie, the if statement never gets called but it should whenever we get to a line break.
The file I am reading is arranged like this
Although I am back again with a problem, I have totally overhauled the whole function but it still uses a 2d array.
In the below code, the variable 'count' is never advanced, ie, the if statement never gets called but it should whenever we get to a line break.
C++ Syntax (Toggle Plain Text)
float x; while (infile >> x) { if (x==(char)'\n') { // this event never happens but it should cout << "count advanced\n"; count++; pCount = 0; } else { pur[count][pCount]= x; pCount++; } }
The file I am reading is arranged like this
•
•
•
•
Week number purchase 1 purchase 2 purchase 3 purchase n
for eg.
1 2.4 1.4
32 1.4 2.7
50 8.45 2.56
![]() |
Similar Threads
- how to store data from com port to access in vb.net (VB.NET)
- Store Data at Access database (C#)
- sql vs xml to store data (MS SQL)
- how to store data from VB to MSaccess (Visual Basic 4 / 5 / 6)
- How to store data in data grid to database by single mouse click in Asp.net (ASP.NET)
- how to store the data? (Java)
Other Threads in the C++ Forum
- Previous Thread: 3D cartesian co-ordinate system
- Next Thread: auto_ptr implementation v.basic for learning purposes
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






