Functions Help! Beginner Codelab C++

Some Useful Functions:


Later on you will write a utility program mw that calculates the molecular weight of a molecule based on its chemical formula. In this homework you will just write a few functions that will be useful when writing that program. So here you are just writing functions... not a whole program!


The first function to define is: isdigit. This function receives a char argument and returns a bool value. It returns true if the argument it receives is a digit ('0', '1', '2', ... '9'). Otherwise it returns false. For this function, you should be able to write a single return statement where the expression being returned is just a long disjunction.


The second function to define is: aw. This function receives a char argument and returns a double value. If the argument is 'H' it returns the value 1.008. If the argument is 'C' it returns the value 12.01. An argument of 'N' results in 14.01 being returned, and an 'O' leads to a return value of 16.00. (What is going on is that the aw function is returning the atomic weight of H,C,N,O [hydrogen, carbon, nitrogen, oxygen]. If the argument is not one of those atoms, then aw returns -99999.0. For this function, you should be able to write a single return statement where the expression being returned is a nested conditional expression.


The third function to define is: digitval. This function receives a char argument and returns an int value. If its argument is the character '0' then it returns the integer 0. If its argument is the character '1' then it returns the integer 1. And so on for '2', '3', ..., '9'. If its argument is not one of those digits it can return anything. For this function too, you can write a single return statement where the expression being returned is a nested conditional expression. But read on for a cooler way of doing this.


HERE IS WHAT I HAVE (Not looking for answers but direction on where to proceed)

bool isdigit(char x){
return((x>='1'&&x<='9')?true:false);
}


double aw(char x){
return((x=='H')? 1.008:((x=='C')? 12.01:((x=='N')? 14.01:((x=='O')? 16.00:-99999.0))));
}

int digitval(char x){
return ((x=='0')?0:((x=='1')?1:((x=='2')?2:((x=='3')?3:((x=='4')?4:((x=='5')?5:((x=='6')?6:((x=='7')?7:((x=='8')?8:((x=='9')?9:))))))))));
}


main.cpp: In function 'int main()':
main.cpp:16: warning: deprecated conversion from string constant to 'char*'
main.cpp:17: warning: deprecated conversion from string constant to 'char*'
main.cpp:21: warning: deprecated conversion from string constant to 'char*'
main.cpp:26: warning: deprecated conversion from string constant to 'char*'
main.cpp:27: warning: deprecated conversion from string constant to 'char*'
main.cpp:28: warning: deprecated conversion from string constant to 'char*'
main.cpp:29: warning: deprecated conversion from string constant to 'char*'
main.cpp:30: warning: deprecated conversion from string constant to 'char*'
main.cpp: In function 'int digitval(char)':
main.cpp:48: error: expected primary-expression before ')' token
You need another number after that last colon in digitval. Your instructions say it can return any number if x is not a digit.

Your isdigit function returns false if x is '0'.

But read on for a cooler way of doing this.

You should definitely read on and find out what this is talking about, since I'm sure that the alternative to nesting ternary operations like this is so much more legible.

The warnings you are getting are from main(), and have to do with mixing string constants (i.e. "hello world") with char pointers. Maybe something like the following?
1
2
char* str = "hello world"; // Wrong
const char* str = "hello world"; // Right 
Last edited on
Thanks alot for the comment.... I left out a number (0) after that last colon.
Topic archived. No new replies allowed.