Help with reading in file to c string

Oct 22, 2017 at 4:17pm
closed account (NCRLwA7f)
Hello,
I'm to read in a file with element symbols and atomic weights and store it in a linked list to compare to a formula to determine molecular weight, but am having trouble extracting the data correctly.
We are learning in class about c strings and that is what I need to use in conjunction with a linked list.

specifically, my problem is just trying to read in the data correctly.

here is what the data looks like:
Mg 24.305
H 1.0080
F 18.9984
Au 196.9665
I 126.9045
Fe 55.847
Th 232.0381

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
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cctype>

using namespace std;

struct node
{
	char eName[3];
	double eWght;
	node *point;
};
node *Alpha = nullptr;



	ifstream inFile;

int main()
{
	inFile.open("Elements.txt");
		
	node *elements = nullptr;
		
	inFile.getline(elements->eName, 3);
	inFile >> elements->eWght;
	
	inFile.close();

return 0;
}


I figured that I could read in the name of the elements as characters store them in eName, and then read in the weight as double and store that value into eWght. This would prevent having to convert the char numbers to actual digits(or so I thought).
any help is appreciated, Thank you.
Oct 22, 2017 at 4:30pm
I don't think you need getline() here. Getline would be useful if the string contained a space, but a chemical symbol doesn't contain spaces, does it?

You could just do
1
2
	inFile >> elements->eName;
	inFile >> elements->eWght;


A rather minimal example of reading the entire file in a loop.
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>

using namespace std;

struct node
{
    char eName[3];
    double eWght;
    node *point;
};

int main()
{
    ifstream inFile("Elements.txt");
		
    node *elements = new node;
    cout.precision(7); // to display all decimal places as input
		
    while ( inFile >> elements->eName >> elements->eWght )
    {
        cout << elements->eName << ' ' << elements->eWght << '\n';	
    }
	
}

Last edited on Oct 22, 2017 at 4:34pm
Oct 22, 2017 at 4:56pm
closed account (NCRLwA7f)
If I just inFile the data, would I be able to compare it to another input file.

For example I need to compare oxygen and hydrogen to a line of text like this
Ex. H(2)0

Would I need my data to be stored as a c string so I can count each instance I find H and O?
So basically, the number in the parentheses would tell me how many elements are in the formula.

And then the program would figure out molecular weight.
Oct 22, 2017 at 5:31pm
If I just inFile the data, would I be able to compare it to another input file.

Not if all you do is display it on the console as I did.

But you can use that loop structure as a basis. It is a good starting point. Where I have the cout, you would need to add the node to the list and so on.

Oct 22, 2017 at 6:44pm
OP’s use of inFile.getline() is more correct than inFile >>.

Personally, I would delimit on that space:

 
  inFile.getline( elements->eName, 3, ' ' );

Now your various nodes will have names of:

  "Mg"
  "H"
  "F"
  "Au"
  ...
 
instead of

  "Mg"
  "H "
  "F "
  "Au"
  ...
  
Using the >> operator guarantees buffer overrun at some point.
Oct 23, 2017 at 12:26am
closed account (NCRLwA7f)
Thank you! I was forgetting about that last entry
inFile.getline (elements->eName, 3, ‘ ’ )

the single quotes.
Will I still be able to use elements->eWght to store a double or would I also need to read the decimal number character by character?
Last edited on Oct 23, 2017 at 12:27am
Oct 23, 2017 at 2:13am
You can still input the weight using >>. Also, remember that C++ does not understand fancy quotes. Use ' .
Oct 23, 2017 at 3:54am
closed account (NCRLwA7f)
oh yeah, the fancy quote, I was texting from my phone and it would not do regular quotes.

But I got the program to enter all the values and working

Thank everyone for the help.
Oct 23, 2017 at 5:43pm
Ah, yes, the joy of using a mobile device with a programming site... LOL. You did better than I ever bothered...
Topic archived. No new replies allowed.