a_migdal
asked on
array input loop
enter 3 digits into each of the array value's and keep entering 3 digits until we enter a digit of 600 which then stops us entering to the array and will then print the array on screen, it looks like it could be a really simple solution but i cant see it.
here is what we have so far:
void main()
{
const int max = 100;
int a[ max ];
int instruction;
cin >> instruction;
a[ max ] = instruction;
while ( instruction!=600 )
{
for ( int i =0; i < max; i++ )
a[ max ] = instruction;
i++;
cin >> instruction;
}
for ( int j = 0; j < max; j++ )
cout << setw( 7 ) << j << setw( 13 ) << a[ j ] << endl;
}
here is what we have so far:
void main()
{
const int max = 100;
int a[ max ];
int instruction;
cin >> instruction;
a[ max ] = instruction;
while ( instruction!=600 )
{
for ( int i =0; i < max; i++ )
a[ max ] = instruction;
i++;
cin >> instruction;
}
for ( int j = 0; j < max; j++ )
cout << setw( 7 ) << j << setw( 13 ) << a[ j ] << endl;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Oh, kmalhotra
I hope you don't mind my working with the code you already posted.
I hope you don't mind my working with the code you already posted.
jj819430-
I think the original poster wants to be able to extract the instructions as units. Your suggestion to use an STL container is a good one: I think a vector of int would be more appropriate in this case (look at poster's other open questions for some background).
-bcl
I think the original poster wants to be able to extract the instructions as units. Your suggestion to use an STL container is a good one: I think a vector of int would be more appropriate in this case (look at poster's other open questions for some background).
-bcl
Just change the part of your code where you index.
You put max to index with. An array's first index is 0
a[ max ] = instruction;
Should be something like
a[current_index] = instruction;
Then you did it again.
while ( instruction!=600 )
{
for ( int i =0; i < max; i++ )
////////////////max....
a[ max ] = instruction;
Also, you should not copy other persons code here as you should know the code before you advance. Teachers will know you have no clue and give you the F.
Another thing you want to prompt inside your loop to get the values
while ( instruction!=600 && instruction < max )
{
clrscr();//conio.h
cout << "Enter value ";
cin >> instruction;
a[ i ] = instruction;
}//end loading
the var i now has how many instructions got loaded. Hmmm, you could use that value in another loop to print them.
while(x < i)
{
print...
}
RJ
You put max to index with. An array's first index is 0
a[ max ] = instruction;
Should be something like
a[current_index] = instruction;
Then you did it again.
while ( instruction!=600 )
{
for ( int i =0; i < max; i++ )
////////////////max....
a[ max ] = instruction;
Also, you should not copy other persons code here as you should know the code before you advance. Teachers will know you have no clue and give you the F.
Another thing you want to prompt inside your loop to get the values
while ( instruction!=600 && instruction < max )
{
clrscr();//conio.h
cout << "Enter value ";
cin >> instruction;
a[ i ] = instruction;
}//end loading
the var i now has how many instructions got loaded. Hmmm, you could use that value in another loop to print them.
while(x < i)
{
print...
}
RJ
I am going to edit kmalhotra's submission so you won't have any issues with the memory.
void main()
{
string a = "";
int instruction, count;
count = 0;
do {
cin>>instruction;
a += instruction;
count++;
}while (instruction != 600);
cout<<"-------------------
for (int ctr = 0; ctr < count; ctr++) {
cout << setw( 7 ) << ctr << setw( 13 ) << a[3*ctr] + a[3*ctr +1] + a[3*ctr + 2] << endl;
}
}
All this is is putting it into a string. It will automatically deal with any memory allocation needs because it is really a char* and it using pointers. Much simpler for your project then actually writing a linked list or something like that.