What did i do wrong on this code? 81.2 practice c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What did i do wrong on this code? 81.2 practice c++

so this is the practice question, ive been stuck on it for a little and have been playing around. This code seems to work best, i get 4 of the 5 Test cases correct. the problem is that the case that is wrong is hidden so i can't see what they inputted to help me fix the code for that situation. You are making a digital menu, which takes a number as user input, and outputs the corresponding menu item. However, the users can input anything, even numbers that are not present in the menu. Handle wrong user input, by throwing an exception with code 4040 and outputting "404 - not found", if it is out of range of the given menu. Note that the menu is given as an array so you need to output the input index, if it exist. #include <iostream> using namespace std; int main() { string menu[] = {"fruits", "chicken", "fish", "cake"}; try { int x; cin >> x; //your code goes here if(x>0 && x<=3){ cout << menu[x] << endl; } else{ throw(x); } } catch(int num){ //and here cout<<"404 - not found"; } }

6th Jan 2022, 1:41 AM
Genaro Melendez-Ruiz
Genaro Melendez-Ruiz - avatar
3 Answers
+ 1
Tip: Remember that arrays are zero-indexed. If I enter zero, I would expect "fruits" as the output.
6th Jan 2022, 1:50 AM
Shadow
Shadow - avatar
0
Shadow thank you so much! I feel like an idiot😅 all i did was forger a "=" sign. I was over sweating bullets trying to workout what was wrong.
6th Jan 2022, 1:53 AM
Genaro Melendez-Ruiz
Genaro Melendez-Ruiz - avatar
- 2
Things that happen, once I was up to the last minute doing a college homework until I realized that I was missing a "*"...
6th Jan 2022, 2:01 AM
CGM
CGM - avatar