hi everyone

for my comp sci class i'm supposed to write a piece of code which will take in 10 ints and put them into an array of such size. unless the input is either a 3 or 7 in which case wats left of the array will be written with either number. i'm only havin trouble with printing out the array with the desired values.

i thought of using a for loop to obtain the values,
then a while loop to check wheter it is a seven or a three
fill in whats left of it with the respective numbers
and output it

the problem is that i am stuck with the cout << command because all i get when i run it is the address and if not able to figure out wats wrong at this point, i dont know how to figure out if my program is working correctly

PS: i think i comment too much

// THIS PROGRAM will take 10 numbers from user and put them into an array
//      if any of them is a 3 then the rest of the array will be filled with 3's
//		if any of them is a 7 then the what is left of the array will be filled 7's

/**PROCEDURE
 * (a) declare an integer array of size 10, and 
 *		(a1)  ask user to enter up to 10 integers and 
 *		(a2)  put them into the array
 * (b) if a 3 is ever entered, fill what is left with 3's and the program will stop
 * (c) if a 7 is ever entered, fill what is left with 7's and the program will stop
 **/
 
#include <iostream>

using namespace std;

int main(){

int data[10]; //(a)
int value=0;

//for(int i=0; i<10; i++){

	
	for(int i=0; i<10; i++){
		cout << "please enter up to ten integers: ";
		cin >> value;
		data[i] = value;
			//while(value!=3){
			
			//}
	}
	
	
	cout<<data;
return 0;
}

Recommended Answers

All 12 Replies

cout << data[index]

cout << data[index]

i am actually trying to print out the whole array no just a value

You cannot print the whole array in one action. You cannot input a whole array in one action. There's really not much at all you can do with an array as a whole.

You have to display it, element by element, in the same fashion as your input loop.

You cannot print the whole array in one action. You cannot input a whole array in one action. There's really not much at all you can do with an array as a whole.

You have to display it, element by element, in the same fashion as your input loop.

is it possible in any way to print out the whole data???

I dont think so. just put the loop and print each element. however you can format the output in any way, everything in 1 line, separate lines, tabs etc.

Read again what I wrote.

>You cannot print the whole array in one action.
Sure you can:

#include <algorithm>
#include <cstddef>
#include <iostream>
#include <iterator>

template <typename T, std::size_t N>
T *end ( T (&array)[N] ) { return array + N; }

int main()
{
  int a[] = {1,2,3,4,5,6,7,8,9,0};

  copy ( a, end ( a ), std::ostream_iterator<int> ( std::cout, "\n" ) );
}

>You cannot input a whole array in one action.
Sure you can:

#include <algorithm>
#include <cstddef>
#include <iostream>
#include <iterator>

int main()
{
  int a[10];
  int *end;

  // Unsafe
  end = copy (
    std::istream_iterator<int> ( std::cin ),
    std::istream_iterator<int>(),
    a );
  copy ( a, end, std::ostream_iterator<int> ( std::cout, "\n" ) );
}

That's not a particularly good idea though, as it's effectively an unbounded loop, so you've got the risk of buffer overflow on your array:

#include <algorithm>
#include <cstddef>
#include <iostream>
#include <iterator>

int main()
{
  int a[10];
  int *end;

  end = a;

  // Unsafe
  while ( std::cin>> *end )
    ++end;

  copy ( a, end, std::ostream_iterator<int> ( std::cout, "\n" ) );
}

However, you can write your own copy_n algorithm and using it still counts as one action:

#include <algorithm>
#include <cstddef>
#include <iostream>
#include <iterator>

namespace jsw {
  template <typename In, typename Out, typename Count>
  Out copy_n ( In first, In last, Count n, Out dst )
  {
    while ( n-- > 0 && first != last )
      *dst++ = *first++;

    return dst;
  }
}

int main()
{
  int a[10];
  int *end;

  end = jsw::copy_n (
    std::istream_iterator<int> ( std::cin ),
    std::istream_iterator<int>(),
    sizeof a / sizeof *a,
    a );

  copy ( a, end, std::ostream_iterator<int> ( std::cout, "\n" ) );
}

>You cannot print the whole array in one action.
Sure you can:

Let me rephrase that.

In the context of what's learned so far in a first term C++ course, there is no simple way to read in or display an array in one action.

cool...

for (int i =0; i < 10;i++)
      cout << data[i] << " ";

the space is there for clarity, but you can take it out.

sorry i had posted on the wrong thread

let me rephrase what i had said before... i do not really need to print the whole array in one step/action..... i just needed it to appear in one line.... i realized that it wasnt happenin because an arrays name is just the address of it.... so i just wrote another separate for loop to make it happen


thanks everyone i appreciate the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.