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);
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
usingnamespace 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;
}
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??
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.
#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;
}
}
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
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??
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