Need help with arrays

Mar 17, 2017 at 8:55pm
I am new to C++. I am also new to arrays.
These are the istructions.
You are going to create an array of ints of size 10. You will then make a function with the following signature:

void readInts( string input, string output );
and this needs to be declared in a header called:

lab7.h
The function will read one numbers per line from the input. The first number will simply be stored in the array at the next available location. If the array is full, then you do not add the number to the array.

Once all the data has been read in and possibly stored in the array, you will loop over the array and give me the index location and the value stored there.

here's my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include "lab7.h"

void readInts( string input, string output )
{
  ifstream in( input );
  ofstream out( output );

  int array [10];
  int x = 0;

  input.open();

  while ( count < 10 && input >> array[10])

  input.close();

  out << "Index Value" << endl;

  for ( x = 0; x < 10; x++ )
  {
    out << x << " " << array[x] << endl;
  }
}

I keep getting these errors when I try to compile,
lab7.cpp
lab7.cpp(11): error C2039: 'open': is not a member of 'std::basic_string<char,std::char_traits<char>,std::allocator<char
>>'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\xstring(2633): note: see declaration of 'std::basic_strin
g<char,std::char_traits<char>,std::allocator<char>>'
lab7.cpp(13): error C2677: binary '&&': no global operator found which takes type 'std::string' (or there is no acceptab
le conversion)
lab7.cpp(13): error C2563: mismatch in formal parameter list
lab7.cpp(15): error C2039: 'close': is not a member of 'std::basic_string<char,std::char_traits<char>,std::allocator<cha
r>>'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\xstring(2633): note: see declaration of 'std::basic_strin
g<char,std::char_traits<char>,std::allocator<char>>'
testmain.cpp
Could someone help?
edit:
this is my header
1
2
3
4
5
6
7
8
#define _USE_MATH_DEFINES
#include <iostream>
#include <fstream>

using std::ifstream;
using std::ofstream;

void readInts( string input, string output );
Last edited on Mar 17, 2017 at 8:56pm
Mar 17, 2017 at 9:27pm
you want in.open, not input.open.
but the constructor already opened the file, so you really don't need an open command at all.

And if you did use in.open, it would be in.open(input) as the function open needs the file name.



Mar 18, 2017 at 12:21am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "lab7.h"

void readInts( string input, string output )
{
  const int arraysize = 10;
  int array [arraysize];
  int x = 0;
  ifstream in(input);
  ofstream out(output);


  while ( x < 10 && in.fail() )
  {
    in >> array[x];
    x++;
  }

  out << "Index Value" << endl;

  for ( x = 0; x < 10; x++ )
  {
    out << x << " " << array[x] << endl;
  }
}


I got it to compile but I am not getting the intended answer.
for the input,
1
2
3
4
the output I'm supposed to get is
Index Value
0 1
1 2
2 3
3 4
4 ?
5 ?
6 ?
7 ?
8 ?
9 ?
but the output I am getting is
Index Value
0 3538196
1 3772960
2 3929580
3 9
4 3538240
5 3538228
6 3746525
7 3929580
8 -1163184090
9 3538240
someone help?
Mar 18, 2017 at 12:33am
while ( x < 10 && in.fail() )
Reads while x is less than 10 and in is in a failure state, which is clearly not what you want.

while (x< 10 && in >> array[x])
Reads while x is less than 10 and a value is successfully extracted from the input stream, which is more in line with your intent.
Mar 18, 2017 at 1:01am
That's what I was doing before I wrote this but my code wasn't compiling
Topic archived. No new replies allowed.