Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

small snippet don't know why it doesn't work

Status
Not open for further replies.

Beholder2

Technical User
Sep 27, 2004
31
0
0
US
hello I'm a little confused as to why this isn't working, I think it may be the IDE/compiler I use which is Dev c++ 4 (I didn't know what other group to post this in) Its a snippet from a larger program I'm using I wanted to test what exactly wasn't working. I have a 2d array and to test it you enter four values, 2 per element. If you enter 123 and 4 the output should be 1234, but for some reason the first element is overwritten by the second. heres the code:

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include "conio.h"
#include <iomanip.h>
#include <string>
using namespace std;
int main()
{
char test[2,2];
int a=0 ;
int b=0;
do {
b=0;
do {
test[a,b]= getche();
b++;
}while (b <=1);
a++;
} while (a <=1);

int x=0;
for (;x <= 1;x++){

for (int y=0; y <= 1; y++){
cout << test[x,y];

endl;
}
}
system("PAUSE");
return 0;
}

thanks for any suggestions
Beholder
 
Please use the [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

> char test[2,2];
This isn't a 2D array, it's only 1D

Try
[tt]char test[2][2];[/tt]

And change your array references to use two sets of [ ] as well.

--
 
Thanks, I guess was thinking I was using qbasic or something for a second.
 
On second thought, what is my code doing then? with the input 1234 I get the output 3434, you'd think the compiler would say something if it was incorrect syntax. Unless its storying two instances of two input values, I don't know, I can't find anything anywhere that says anything about declaring an array as [2,2].
 
There is comma operator in C and C++: do the 1st then do the 2nd subexpression. So [2,2] is exactly the same as [2].
It's syntactically correct construct.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top