Jul 15, 2015 at 1:14am UTC
What I thought this was going to do was put numbers in lineArray and rowArray.
lineArray[0] would be 1 and lineArray[1] would be 2 etc. that's not the output I'm getting and don't know why?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <sstream>
#include <cstddef>
#include <iomanip>
using namespace std;
int main()
{
const int MAX_ROW = 50;
const int MAX_LINE = 50;
const int MAX_MINE = 128;
string lineArray[MAX_LINE];
string rowArray[MAX_ROW];
string mineArray[MAX_MINE];
for (int i=0; i<54; i++)
{
rowArray[i] = i;
cout << rowArray[i] << endl;
}
for (int i=0; i<24; i++)
{
lineArray[i] = i;
cout << lineArray[i] << endl;
}
//cout << lineArray[1] << " Hi " << rowArray[1];
}
Last edited on Jul 15, 2015 at 2:38am UTC
Jul 15, 2015 at 1:39am UTC
Both for-loops start with i=1 , so how could the first element of the arrays ever be accessed?
Line 29: that variable is reset to 0 at the beginning of each loop, so it is always at 1 by the time it gets to line 31.
Last edited on Jul 15, 2015 at 1:40am UTC
Jul 15, 2015 at 1:54am UTC
Yeah my bad I was testing some stuff and I see what your saying but its unimportant. So the first element with I=1 is also unimportant. I just want 1-54 in some postions I don't really care if it starts at 1 or 1001 as long as I got 1,2,3etc. Right now I don't have anything close to that.
Jul 15, 2015 at 2:04am UTC
The way the code is now after I edited it it should work right?
Last edited on Jul 15, 2015 at 2:33am UTC
Jul 15, 2015 at 2:33am UTC
Just another dead question with no help.
Jul 15, 2015 at 3:36pm UTC
Kemort that didn't work. I still get the same output?
Jul 15, 2015 at 3:37pm UTC
I think ill just give up on this and learn vectors. They seem easier to me.
Jul 15, 2015 at 3:39pm UTC
I think reporting someone for not replying to one of your threads fast enough is a pretty good way of alienating yourself on this forum.
Jul 15, 2015 at 4:23pm UTC
I reported cause his answer left me stuck with no help. It didn't have to do with time.
And I don't want this issue to turn into cring and fighting. If its a problem or you don't agree then do something about it.
Last edited on Jul 15, 2015 at 4:25pm UTC
Jul 15, 2015 at 6:29pm UTC
I think the problem here is I declared a string.
string lineArray[MAX_LINE]; on line 39
And then in the for loop i is an int
for (int i=0; i<MAX_LINE; i++)
{
lineArray[i] = i;
cout << lineArray[i] << endl;
}
So can I put an int data number in a string array?
Jul 15, 2015 at 7:04pm UTC
Now this works right but ill have to change MAX_ROW to 54 and of course it puts 54 i's in each postion.
for (int i=0; i<MAX_ROW; i++)
{
rowArray[i] = "i";
cout << rowArray[i] << endl;
}
So it looks to me I need to convert the data number to string then put it into the rowArray[I] so I get
rowArray[0] = 1
RowArray[1] = 2
Etc...
Then later when I need to do some math I have to convert the string numbers back to data numbers.
So "What doesn't work about kenort's code? I ran his code and it works fine, assuming you want the value of i in array[i]."
How did that work for you again? Is there something you got I don't have?
Jul 15, 2015 at 7:19pm UTC
Oh I also tried this
for (int i=0; i<MAX_ROW; i++)
{
rowArray[i] = 4;
cout << rowArray[i] << endl;
}
It gave me a bunch of blank lines.
Jul 15, 2015 at 8:14pm UTC
for (int i=0; i<55; i++)
{
string String = static_cast<ostringstream*>( &(ostringstream() << i) )->str();
rowArray[i] = String;
cout << rowArray[i] << endl;
}
I wonder what this dose?
If you like to learn how to convert it back to data and actually learn something I can tell you.
Jul 16, 2015 at 3:27am UTC
ASCII characters are integers. That is why you are getting blanks and what appears to be random characters because the integer 1 is not ASCII '1'. See http://www.cplusplus.com/doc/ascii/?kw=ASCII among others.
If you want to store a number as a string then see http://www.cplusplus.com/reference/string/to_string/