How to check Cin input against two statements; numerical value and string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to check Cin input against two statements; numerical value and string

22nd Dec 2016, 3:46 PM
TaimuKo
7 Answers
+ 4
More clarification is required regarding your query. From what I understand, you are trying to validate user input by checking it as a string, and also being able to check the string numerical value. E.g. string str = "123"; and you want to make sure that str is greater than some integer or not. A way to do this would be to utilise the string conversion functions in <cstdlib> header. E.g. string str = "123"; int integer = atoi(str.c_str()); Doing this will store 123 into integer. You can then validate integer based on numerical value. If you have questions regarding the above code, please refer to my codes on string conversion to integer. Hope I helped.
22nd Dec 2016, 4:24 PM
Hatsy Rei
Hatsy Rei - avatar
+ 4
I see. I have actually posted a similar response to another uncannily similar question somewhere here. The solution would be to declare the search term as string. Your conditional statements would then consider two conditions to print the corresponding info. E.g. string searchkey; cin >> searchkey; if (searchkey == "7" || searchkey == "Pikachu") { //cout } This way, you wouldn't have to worry about checking if it's int or string.
22nd Dec 2016, 4:51 PM
Hatsy Rei
Hatsy Rei - avatar
+ 4
I have just tested the code you posted via Code Playground and it works just fine. One thing I can think of is that the first letter of the Pokemon name has to be in caps for user input, following the string format listed in your conditional statement.
22nd Dec 2016, 5:05 PM
Hatsy Rei
Hatsy Rei - avatar
+ 4
No problem :>
22nd Dec 2016, 5:08 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
Thank you my code afterwards which wasnt altered was stopping it from working. Thank you again :)
22nd Dec 2016, 5:07 PM
TaimuKo
0
Basically it is a Pokedex where if you input the number 2 you bring up Ivysaurs information but you should also be able to type in ivysaur to bring up that information. Example of the code is: else if (UserPokemon == 2) { cout << "No. 2 Ivysaur" <<endl; cout << "Type: Grass & Posion" <<endl; cout << "There is a bud on this Pokémon's back. To support its weight, Ivysaur's legs and trunk grow thick and strong. If it starts spending more time lying in the sunlight, it's a sign that the bud will bloom into a large flower soon." <<endl; cout << "Weaknesses: Fire, Flying, Ice & Psychic" << endl; cout << "Evolves into Venasuar at Level 32" << endl; }
22nd Dec 2016, 4:47 PM
TaimuKo
0
When the User inputs the name the information doesnt appear but have done what you have said: string UserPokemon; cin >> UserPokemon; if (UserPokemon == "1" || UserPokemon == "Bulbasaur") { cout << "No. 1 Bulbasaur" <<endl; cout << "Type: Grass & Posion" <<endl; cout << "Bulbasaur can be seen napping in bright sunlight. There is a seed on its back. By soaking up the sun's rays, the seed grows progressively larger." << endl; cout << "Weaknesses: Fire, Flying, Ice & Psychic" <<endl; cout << "Evolves into Ivysaur at Level 16" << endl; }
22nd Dec 2016, 5:01 PM
TaimuKo