error reading characters of string

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include<fstream>
#include<Windows.h>
#include<cstdlib>
#include<iostream>
#include<string>
#include<iomanip>
#include<sstream>

using namespace std;

void add_matrix(int row1,int row2,int col1,int col2,double m1[30][30],double m2[30][30],double m3[30][30]);
void GetMatrix(double m1[30][30],  double m2[30][30],
			   ifstream &infile, int option, double m3[30][30],
			   int &row1, int &row2,int &col1,int &col2);

void displaymenu(int &option)
{
		cout<<"\n\nWelcome to Matrix Operation"<<endl;
		cout<<"*****************************************\n\n";
		cout<< "Please select option: \n\n";
		cout<< "\t(1) Add two matrices \n";
		cout<< "\t(2) Multiply two matrices\n";
		cout<< "\t(3) Hadamard product of two matrices\n";
		cout<< "\t(4) Kronecker product of two matrices \n";
		cout<< "\t(5) Display a matrix \n\n";
		cout<< "\t(99) Exit"<<endl;

		cout<<"Please select your menu : ";
		cin>>option;
}

int main( void )
{
	int option=0;
	char quit='n';
//	int choice;
	double m1[30][30], m2[30][30], m3[30][30] ;
	ifstream infile;
	string inputfilename,outputfilename;
	int row1=0, row2=0, col1=0, col2=0;


do{
	error:
	system("cls");
	system("color 5B");
	//display menu
     displaymenu(option);

		switch(option)
		{
			case 1:
			case 2:
			case 3:
			case 4:GetMatrix(m1,m2,infile,option,m3, row1, row2,col1,col2);

			default :
				{
				fflush(stdin);
				system("cls");
				printf("I'm sorry,but you have enter an inavalid selection.Please try again\n\n");
				system("pause");
				system("cls");
				goto error;
				}
		}
		
	}while(quit=='n'||quit=='N');
			
	
	return 0;
}

]void GetMatrix(double m1[30][30],  double m2[30][30],
			   ifstream &infile, int option, double m3[30][30],
			   int &row1, int &row2,int &col1,int &col2)
{
	string inputfilename,outputfilename;
	system("cls");

	cout<<"\n\nPlease enter ONE (1) input filename : ";
	cout<<endl;
	cin>>inputfilename ;
	
	infile.open(inputfilename,ios::in);

	if(infile.fail())
	{
		cout<<"File could not be open !! Press any Key...";
		cin.ignore();
		cin.get();
		exit(-1);
	}

	else
	{
		string line;// problem here
		int row1, row2, col1, col2;
		infile.ignore('/n');
		getline(infile,line);//get the file header 

		infile>>row1 >>col1; 
		cout<<"This is "<< row1<<" x "<< col1 <<" matrix."<<endl;

			for(int i=0 ; i<row1 ; i++)
			{
				for(int j=0 ; j<col1 ; j++)
				{
					infile >> m1[i][j]; 
					cout<<m1[i][j]<<"\t";
				}
			cout<<endl<<endl;
			}
			cout<<line;
	string line2;
		infile>>line;
		/*do
		{
			getline(infile,line);
		}
		while(line!= "</mat>");*/
		cout<<line;

		infile>>line2;	
		cout<<line2;
		
		getline(infile,line2);	
		
		cout<<line2;
		getline(infile,line2);	
		cout<<line2;


			infile>>row2 >>col2; 
			cout<<"This is "<< row2<<" x "<< col2 <<"matrix"<<endl;

			for(int i=0 ; i<row2 ; i++)
			{
				for(int j=0 ; j<col2 ; j++)
				{
					infile >> m2[i][j]; 
					cout<<m2[i][j]<<"\t";
				}
				cout<<endl<<endl;
			}
			
					
			system("pause");


1
2
3
4
5
6
7
8
9
10
11
12
13
14
<mat>
3 3
1 2 2
2 4 3
2 1 7
</mat>


<mat>
3 3
1 2 2
2 4 3
8 5 3
</mat>


test file has stuff that this

Hi guys , I'm helping my friend in solving her problem but apparently I have

problem before getting to her problem (lol). That's it , this program is

suppose to read a matrix file , and the first getline is suppose to get the file

header but it appears that 'line' doesn't take in any value other than empty

thus causing all the problem , I tried to put cin.getline() in front of it to take

away the /n created by the cin before it , but it doesn't work . When I

debug the program when the arrow points to the string line , this error

appears

line <Error reading characters of string.> std::basic_string<char,std::char_traits<char>,std::allocator<char> >

I tried to initialize string line=NULL too , doesn't work either.

Last edited on
closed account (S6k9GNh0)
Please make a test case! http://en.wikipedia.org/wiki/Test_case

Or in lamen terms, provide code that I can easily copy-and-paste into a file for quick compilation. Also, provide a valid input file and the intended functionality vs the current functionality.
Last edited on
Topic archived. No new replies allowed.