C++ problem

I have to create an adjacency matrix, and for that i have to read my input file line by line and then I have to read that line word by word and store each line in an array. I wrote the following code but its giving me loads of errors. Can someone pls help me with it?#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main()
{

struct circuit /*storing all the data from the file in a matrix*/
{
string device;
string nodeA,nodeB;
string value;
}ckt[100];
ifstream in("s2.txt");
if(!in)
{
cout<< "can't open";
exit(1);
}
else
{
int i;
char *str;
istream& getline(char* str,255);

for(i=0;i<4;i++)
{

string word;
cin>>word;
char *strcpy(&ckt[i].device,&word);
cin>>word;
char *strcpy(&ckt[i].nodeA,&word);
cin>>word;
char *strcpy(&ckt[i].nodeB,&word);
cin>>word;
char *strcpy(&ckt[i].value,&word);
}

istream& getline(char* str,255, " ");
}
char choice;
int i;
cout<<"Enter the choice you want to search";
cin>>choice;
for(i=0;i<4;i++)
{
if(choice - ckt[i].nodeA==0)
cout<<"its there"<<ckt[i].nodeB;
}
return 0;
}


Please don't use strcpy() with std::string.
1
2
3
4
5
6
7
8
9
10
11
12
13
string word;

cin>>word;
ckt[i].device = word;

cin>>word;
ckt[i].nodeA = word;

cin>>word;
ckt[i].nodeB = word;

cin>>word;
ckt[i].value = word;


Or more simply:


1
2
3
4
cin >> ckt[i].device;
cin >> ckt[i].nodeA;
cin >> ckt[i].nodeB;
cin >> ckt[i].value;

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{

	struct circuit /*storing all the data from the file in a matrix*/
	{
		string device;
		string nodeA, nodeB;
		string value;
	} ckt[100];
	ifstream in("s2.txt");
	if(!in)
	{
		cout << "can't open";
		exit(1);
	}
	else
	{
		int i;
		char *str; // What is this for?
		istream& getline(char* str,255); // What is this for?

		for(i = 0; i < 4; i++)
		{

			// fixed
			cin >> ckt[i].device;
			cin >> ckt[i].nodeA;
			cin >> ckt[i].nodeB;
			cin >> ckt[i].value;
		}

		// What was this supposed to do?
		// istream	& getline(char* str,255, " ");
	}
	char choice;
	int i;
	cout << "Enter the choice you want to search";
	cin >> choice;
	for(i = 0; i < 4; i++)
	{
		// ckt[i].nodeA is a string and choice is a char.
		// What do you hope to achieve by subtracting a
		// string from a char?
		if(choice - ckt[i].nodeA == 0) // won't work!!
			cout << "its there" << ckt[i].nodeB;
	}
	return 0;
}
Last edited on
@Galik

1)Basically I wanted to have ckt[i].device as a string but the others as int or float. Is it possible??
2) I want to compare the choice and ckt[i].node's value
char *str; // What is this for?
istream& getline(char* str,255); // What is this for?

>I have to extract first four words of my input file and I have to store each of those four words in an object together. So, I have used that function to read d file line by line. Is the syntax wrong??
Aditi wrote:
1)Basically I wanted to have ckt[i].device as a string but the others as int or float. Is it possible??


Yes, just change their types in the struct declaration.

Aditi wrote:
2) I want to compare the choice and ckt[i].node's value


Then I think choice needs to be a string, not a char:

1
2
3
4
	string choice; // string, compare like with like
	int i;
	cout << "Enter the choice you want to search";
	cin >> choice;

Aditi wrote:
istream& getline(char* str,255); // What is this for?

I have to extract first four words of my input file and I have to store each of those four words in an object together. So, I have used that function to read d file line by line. Is the syntax wrong??


That is simply a function declaration. It doesn't do anything.

But these lines here do the actual extracting:

1
2
3
4
5
6

			// fixed
			cin >> ckt[i].device;
			cin >> ckt[i].nodeA;
			cin >> ckt[i].nodeB;
			cin >> ckt[i].value;
I have to extract the line first and then the four words of that line. How do I do it?
Aditi wrote:
I have to extract the line first and then the four words of that line. How do I do it?


It depends on the format of the input. Can you give us a sample?

The basic way to do this is like this:

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

int main()
{
	std::ifstream ifs("my-file.txt"); // open file

	std::string line; // create a string variable called line to receive each line

	// read one line from ifs into variable called 'line' and loop if successful
	while(std::getline(ifs, line)) 
	{
		// Now you need to extract your 4 values from line

		std::istringstream iss(line); // create an input stream using your line

		iss >> ckt.device; // read your values out of the variable line through the stream
		iss >> ckt[i].nodeA;
		iss >> ckt[i].nodeB;
		iss >> ckt[i].value; 
	}
}
Last edited on
how about using vectors?
arrays are so last century.
use boost::regex so you can split on anything.

or maybe I should get a life ;-)

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
#include <iostream>
#include <vector>
#include <string>
#include <boost/regex.hpp>

using namespace std;

template <class T>
T split(string s, string pat="\\s+" )
{
    boost::regex reg(pat);
    boost::sregex_token_iterator it(s.begin(), s.end(), reg, -1);
    boost::sregex_token_iterator end;
    T container;

    for ( ;it != end; ++it ) {
        container.insert(container.end(),*it);
    }
    return container;
}

typedef  vector<string> lvec;
typedef vector<lvec> vec;

int main(int argc, char ** argv) {

    string s;
    vec v;

    while(getline(cin, s)) {

        v.push_back( split<lvec>(s) );

    }
    // lines are split on space
    // and you now have a vector of vector<string>
    // lets print 'em out

    vec::iterator p = v.begin();
    for( ; p != v.end(); ++p) {

        p->resize(4); // you only want first 4 elements of line

        lvec::iterator y = p->begin();
        // the actual split lines
        for( ; y != p->end(); ++y) {
            cout << *y << " | ";
        }
        cout << endl;
    }

    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ df -h | ./1
$ df -h | ./1
Filesystem | Size | Used | Avail | 
/dev/ad4s1a | 496M | 287M | 169M | 
devfs | 1.0K | 1.0K | 0B | 
/dev/ad4s1d | 2.8G | 166M | 2.4G | 
/dev/ad4s1e | 496M | 172K | 456M | 
/dev/ad4s1f | 22G | 5.7G | 14G | 
/dev/ad4s2d | 97G | 59G | 30G | 
/dev/ad4s2e | 48G | 31G | 14G | 
/dev/ad6s4d | 107G | 52G | 46G | 
/dev/ad6s2d | 9.7G | 1.8G | 7.1G | 
procfs | 4.0K | 4.0K | 0B | 
/dev/da0s1 | 75G | 40G | 34G | 
/dev/md0 | 19M | 44K | 18M | 
$
Last edited on
My input file is like this
as 1 0 34
b3 2 1 9000 in=0
c3 3 2 4.56
.
.
.
n lines

Now I just need to read each line individually and it has to be stored like this
ckt[0].device= as
ckt[0].nodeA=1
ckt[0].nodeB=0
ckt[0].value=34
ckt[1].device=b3
ckt[1].nodeA=2
ckt[1].nodeB=1
ckt[1].value=9000

and so on..
@bigearsbilly..

Thanks a lot for your help but am not getting the program at all that you wrote..:(
I need to write a program that can extract first four words of each line of my file which i then want to store in my object..can you possible tell me how to manage that??
Aditi wrote:

My input file is like this
as 1 0 34
b3 2 1 9000 in=0
c3 3 2 4.56


Then I think the code I posted above should work. So read up on std::getline() and std::istringstream;
owzat?
probably overkill with the regex but I am working on it for my personal library.
I'd pack the split template off to a separate header file and include it.

probably more terse to use a sstream as galik said
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

#include <iostream>
#include <vector>
#include <string>
#include <boost/regex.hpp>

using namespace std;

template <class T>
T split(string s, string pat="(\\s+)" )
{
    boost::regex reg(pat);
    boost::sregex_token_iterator it(s.begin(), s.end(), reg, -1);
    boost::sregex_token_iterator end;
    T container;

    for ( ;it != end; ++it ) {
        container.insert(container.end(),*it);
    }
    return container;
}

typedef  vector<string> lvec;
typedef vector<lvec> vec;

struct circuit /*storing all the data from the file in a matrix*/
{
string device;
string nodeA;
string nodeB;
string value;
}ckt[100];

int main(int argc, char ** argv) {

    string s;
    vec v;

    while(getline(cin, s)) {

	v.push_back( split<lvec>(s) );

    }
    // lines are split on space
    // and you now have a vector of vector<string>

    vec::iterator p = v.begin();
    int count = 0;
    for( ; p != v.end(); ++p) {

	p->resize(4);
	ckt[count].device = (*p)[0];
	ckt[count].nodeA  = (*p)[1];
	ckt[count].nodeB  = (*p)[2];
	ckt[count].value  = (*p)[3];
	count++;
    }
    for(int n=0; n < count; n++) {

	cout << "line:" << n << endl;
	cout << "\tdevice:" << ckt[n].device << endl;
	cout << "\tnodeA:" << ckt[n].nodeA << endl;
	cout << "\tnodeB:" << ckt[n].nodeB << endl;
	cout << "\tvalue:" << ckt[n].value << endl;

    }
    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ ./1 < data   

line:0
        device:as
        nodeA:1
        nodeB:0
        value:34
line:1
        device:b3
        nodeA:2
        nodeB:1
        value:9000
line:2
        device:c3
        nodeA:3
        nodeB:2
        value:4.56
$

Last edited on
@galik: Thanks a lot it worked!!! thanks again:)
@galik: Thanks a lot it worked!!! thanks again:)
Topic archived. No new replies allowed.