Help debugging code

my code is:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  #include "mapwalk.h"

void mapWalk( string input, string output )
{
  int numbers[ROWS][COLS];

  istream in(input);
  ostream out(output);

  string junk, file;

  in << junk << file;

  out >> "Loading: " >> file >> endl;

  readtwoD_Data(out, file);
}

void readtwoD_Data(ostream &out, string file)
{
  ifstream in(file);
  int cols;
  int rows;
  int twoDarray[ROWS][COLS];

  read(in, rows, cols, twoDarray[][COLS]);

  display(out, rows, cols, twoDarray[][COLS]);
}

void read(istream &in, int rows, int cols, const int twoDarray[][COLS] )
{
  string junk;

  in << junk << cols << junk << rows;

  if ( cols > COLS )
  {
    cols = COLS;
  }
  else if ( rows > ROWS )
  {
    rows = ROWS;
  }

  in << junk << junk << junk << junk << junk << junk;

	for ( int i=0; i < rows; i++ )
	{
		for ( int j=0; j < cols; j++ )
		{
			in << twoDarray[i][j];
		}
		in.ignore( 5000, '\n' );
	}
}

void display(ostream &out, int rows, int cols, const int twoDarray[][COLS])
{
  out >> "Number of Rows: " >> rows >> endl
      >> "Number of Columns: " >> cols >> endl;
  for ( int i=0; i < rows; i=i+1 )
	{
		for ( int j=0; j < cols; j=j+1 )
		{
      out >> setw(3) >> j >> endl;
			out >> i >> twoDarray[i][j];
		}
		out >> endl;
	}
}

I am getting the following errors,
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
mapWalk.cpp
.\mapWalk.cpp(7): error C2664: 'std::basic_istream<char,std::char_traits<char>>::basic_istream(const std::basic_istream<
char,std::char_traits<char>> &)': cannot convert argument 1 from 'std::string' to 'std::basic_streambuf<char,std::char_t
raits<char>> *'
.\mapWalk.cpp(7): note: No user-defined-conversion operator available that can perform this conversion, or the operator
cannot be called
.\mapWalk.cpp(8): error C2664: 'std::basic_ostream<char,std::char_traits<char>>::basic_ostream(const std::basic_ostream<
char,std::char_traits<char>> &)': cannot convert argument 1 from 'std::string' to 'std::basic_streambuf<char,std::char_t
raits<char>> *'
.\mapWalk.cpp(8): note: No user-defined-conversion operator available that can perform this conversion, or the operator
cannot be called
.\mapWalk.cpp(26): error C2059: syntax error: ']'
.\mapWalk.cpp(28): error C2059: syntax error: ']'
.\mapWalk.cpp(52): error C2678: binary '>>': no operator found which takes a left-hand operand of type 'std::istream' (o
r there is no acceptable conversion)
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(482): note: could be 'std::basic_istream<char,std
::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::basic_streambuf<char,std::char_t
raits<char>> *)'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(463): note: or       'std::basic_istream<char,std
::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(void *&)'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(445): note: or       'std::basic_istream<char,std
::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(long double &)'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(427): note: or       'std::basic_istream<char,std
::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(double &)'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(408): note: or       'std::basic_istream<char,std
::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(float &)'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(390): note: or       'std::basic_istream<char,std
::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(unsigned __int64 &)'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(371): note: or       'std::basic_istream<char,std
::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(__int64 &)'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(352): note: or       'std::basic_istream<char,std
::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(unsigned long &)'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(334): note: or       'std::basic_istream<char,std
::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(long &)'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(316): note: or       'std::basic_istream<char,std
::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(unsigned int &)'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(290): note: or       'std::basic_istream<char,std
::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(int &)'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(271): note: or       'std::basic_istream<char,std
::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(unsigned short &)'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(236): note: or       'std::basic_istream<char,std
::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(short &)'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(217): note: or       'std::basic_istream<char,std
::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(bool &)'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(210): note: or       'std::basic_istream<char,std
::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::ios_base &(__cdecl *)(std::ios_b
ase &))'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(203): note: or       'std::basic_istream<char,std
::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::basic_ios<char,std::char_traits<
char>> &(__cdecl *)(std::basic_ios<char,std::char_traits<char>> &))'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(197): note: or       'std::basic_istream<char,std
::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::basic_istream<char,std::char_tra
its<char>> &(__cdecl *)(std::basic_istream<char,std::char_traits<char>> &))'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(1073): note: or       'std::basic_istream<char,st
d::char_traits<char>> &std::operator >><std::char_traits<char>>(std::basic_istream<char,std::char_traits<char>> &,signed
 char *)'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(1080): note: or       'std::basic_istream<char,st
d::char_traits<char>> &std::operator >><std::char_traits<char>>(std::basic_istream<char,std::char_traits<char>> &,signed
 char &)'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(1087): note: or       'std::basic_istream<char,st
d::char_traits<char>> &std::operator >><std::char_traits<char>>(std::basic_istream<char,std::char_traits<char>> &,unsign
ed char *)'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(1094): note: or       'std::basic_istream<char,st
d::char_traits<char>> &std::operator >><std::char_traits<char>>(std::basic_istream<char,std::char_traits<char>> &,unsign
ed char &)'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\istream(1104): note: or       'std::basic_istream<char,st
d::char_traits<char>> &std::operator >><char,std::char_traits<char>,const int>(std::basic_istream<char,std::char_traits<
char>> &&,_Ty &)'
        with
        [
            _Ty=const int
        ]
.\mapWalk.cpp(52): note: while trying to match the argument list '(std::istream, const int)'


could someone help me getting rid of them.
Last edited on
I am writing a code for my project and I have to read data from this file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ncols        27
nrows        13
xllcorner    -80.625000000000
yllcorner    37.125000000000
cellsize     0.016666666667
 662 680 704 800 997 906 754 668 662 768 770 672 706 797 827 721 606 567 584 666 759 768 675 647 647 654 639
 567 600 621 647 671 652 622 607 689 697 670 752 716 653 637 674 725 753 799 756 695 657 613 656 618 579 606
 538 562 573 579 616 658 679 616 644 726 754 685 728 778 748 710 700 651 659 638 589 650 694 545 486 596 783
 541 640 732 769 782 741 720 728 735 710 727 737 698 718 661 639 627 652 597 539 695 709 498 462 558 739 712
 625 602 630 674 724 746 733 694 675 768 703 637 646 658 656 620 570 548 534 590 594 481 440 546 729 643 486
 594 646 702 699 675 656 703 761 734 672 608 627 660 660 602 536 492 619 677 503 430 427 491 609 493 449 410
 575 587 658 668 629 594 602 617 603 594 616 629 636 674 617 491 567 791 631 452 435 404 416 429 401 393 405
 570 589 587 575 556 555 580 599 602 611 622 619 637 658 619 479 506 624 683 599 433 512 517 428 435 495 470
 541 539 549 526 535 535 538 593 606 624 631 637 644 640 624 492 511 599 539 485 495 546 498 409 485 555 744
 562 550 553 558 542 547 571 605 651 675 672 640 636 614 592 498 496 537 548 551 541 534 467 428 547 679 938
 596 589 582 559 564 597 608 600 598 603 620 623 627 592 544 555 538 559 572 552 533 498 441 504 593 887 993
 601 600 580 557 554 547 580 591 607 603 599 617 635 626 604 614 613 606 610 585 569 477 521 584 697 931 939
 585 583 555 566 571 560 583 590 605 635 632 633 637 646 656 660 646 620 617 592 548 470 547 577 678 791 807
read the first 5 lines separately, extracting the numerical information from each of them and using that information (particularly the ncols and nrows numbers) to read the rest of the file into a std::vector<std::vector<int>> i.e. a 2D matrix of numbers
ok, i thought i might as well get you started with the first five lines:
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 <fstream>
#include <string>

int main()

{
    std::ifstream inFile("F:\\test1.txt");

    size_t rows{}, cols{};
    double xllcorner{}, yllcorner{}, cellsize{};
    std::string line{};

    while(inFile)
    {
        //http://www.cplusplus.com/reference/string/string/getline/
        //use getline() with delimiter
        getline(inFile, line, ' ') && inFile >> rows;
        getline(inFile, line, ' ') && inFile >> cols;
        getline(inFile, line, ' ') && inFile >> xllcorner;
        getline(inFile, line, ' ') && inFile >> yllcorner;
        getline(inFile, line, ' ') && inFile >> cellsize;
    }

    std::cout << "Rows: " << rows << "\n";
    //etc
    std::cout << "Cellsize " << cellsize << "\n";
}
/*inFile is just the first 5 lines in this example:
ncols        27
nrows        13
xllcorner    -80.625000000000
yllcorner    37.125000000000
cellsize     0.016666666667*/


Topic archived. No new replies allowed.