Please someone help me to print carbon's atomic weight in c++... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Please someone help me to print carbon's atomic weight in c++...

#include <iostream> using namespace std; int main() { string element ; string carbon = "Atomic Weight : 12"; cin >> element ; if (element ==carbon){ cout << carbon <<endl; } return 0; }

23rd Sep 2018, 7:35 AM
Aryan Gupta
Aryan Gupta - avatar
3 Answers
+ 1
change if ( element == carbon ) to if ( element.compare( 0, 6, "carbon" ) == 0 ) // quotes are needed It should now run when you type the word "carbon" (w/o the quotes) If you meant Carbon or CARBON (etc) as well you will need to create a case insensitive compare for each character
23rd Sep 2018, 8:27 AM
Nestor
+ 1
i googled c# string compare. normally i would have used string.compare( string1, string2 ) but i was not sure if the cin would include the \n, so to play safe, i only wanted to compare the 1st 6 characters.
23rd Sep 2018, 7:10 PM
Nestor
0
Thanks Nestor for your help ....I have a question .....where did you learn to type this code ?
23rd Sep 2018, 9:06 AM
Aryan Gupta
Aryan Gupta - avatar