Struct declaration and function returning struct question

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 <cmath.h>
using namespace std;

struct element
{
	string name;
	double weight;
} data;

element elementinfo (string element)
{
	if(element == "H")
	{
		element hydrogen = {"Hydrogen", 1.00794};
		return hydrogen;
	}
	else if(element == "He")
	{
		element helium = {"Helium", 4.002602};
		return helium;
	}
}

int main ()
{
	cout << "Welcome to the percent composition calculator!" << endl;
	string input;
	cin >> input;
	data = elementinfo(input);
	cout << data.weight << endl;
	return 0;
}


When I compile it gives a bunch of errors. What am I doing wrong?
It would be nice if you would tell us what the errors are. Sometimes they are self-explanatory.

One thing I notice is that you are trying to name a string "element" when element is already the name of a type.
Line 2, correct header is #include <cmath>

You're missing a #include <string>

Line 15, You're confusing the type name (struct element) and the argument name (element). You can't use the same name for both.



Last edited on
Ok thanks! I saw an example code somewhere that did that for the headers, so I just always do that for cmath only for whatever reasons. I'll change it.
And so I changed elementinfo argument to string abbr like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
element elementinfo (string abbr)
{
	if(abbr == "H")
	{
		element hydrogen = {"Hydrogen", 1.00794};
		return hydrogen;
	}
	else if(abbr == "He")
	{
		element helium = {"Helium", 4.002602};
		return helium;
	}
}


And I'm still getting errors. Zhuge the compile errors say I have a syntax error on line 15 and that it "can't convert between "char *" and elementinfo(string)".
After making the change you did with the name and adding #include <string> as AbstractionAnon suggested, it compiles fine for me.
yeah I did... this code exactly compiles fine for you guys?
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 <cmath>
#include <string>
using namespace std;

struct element
{
	string name;
	double weight;
} data;

element elementinfo (string abbr)
{
	if(abbr == "H")
	{
		element hydrogen = {"Hydrogen", 1.00794};
		return hydrogen;
	}
	else if(abbr == "He")
	{
		element helium = {"Helium", 4.002602};
		return helium;
	}
}

int main ()
{
	cout << "Welcome to the percent composition calculator!" << endl;
	string input;
	cin >> input;
	data = elementinfo(input);
	cout << data.weight << endl;
	return 0;
}
Yup, it compiled for me with no errors. Try copying and pasting the exact and full error message(s) the compiler is giving you.
well to be honest I'm running a pretty ghetto compiler at the moment through the terminal so i cant copy and paste it. I've been meaning to get around to getting a good one though, so I think I'll go do that now. Any suggestions?
Topic archived. No new replies allowed.