- 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; }
3 Antworten
+ 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
+ 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.
0
Thanks Nestor for your help ....I have a question .....where did you learn to type this code ?