C++, Can you help me please? I have to create a program that does this: http://screencast.com/t/VpKYkgfMf5
// // main.cpp // week5-3 #include <iostream> using namespace std; int main() { int option,option2; cout << "Is it a dog, cat, bird, rept or other"; cin >> option; if (option == 'dog' || option == 'cat' || option == 'bird' || option == 'rept' ) { if (option == 'dog' || option == 'cat') { if (option == 'dog') { cout << "Is it neutered or not?"; cin >> option2; if (option2 == 'yes') cout << "50 euros." << endl; else if (option2 == 'no') cout << "80 euros."; } if (option == 'cat') { cout << "Is it neutered or not?"; cin >> option2; if (option2 == 'yes') cout << "40 euros."; else if (option2 == 'no') cout << "60 euros."; } if (option == 'bird' || option == 'rept') cout << "10 euros."; } else cout << "Error!" << endl; return 0; }
Where is my mistake in the code? The code doesn't run.
i donno much about c++ but i think i can point you out to a few errors i dont think u can equate strings that way try using strcmp instead and dog is string if i has double quotes "dog" not 'dog'
hope that helps
That didn't fix it I am afraid but thx for trying to help :/
option is a string? o.o
I dont know what is a string :S I am sorry
c++ has support for strings? else you got to declare an array of chars
just replace int with string the first line of main block
still doesnt work.
You can not input a string into an integer. Also, you are not following the instructions. It says to take in one letter for if it is a cat, dog, etc.
I know pretty well, that I donnot follow the exact instructions. But I still don't get why it's not running. Even the way it is now.
cout << "What type of pet do you have" << \n << "D for Dog, C for Cat, R for Reptile, B for Bird or O for Other" << \n; Because the directions were for a reason. You are taking in text and assigning it to an integer! That does not work. You need to take in just one charater and put it in as a character.
oooh hold on lemme try
c++ is strongly typed. If this was a loosly typed language, like Python, it would play nice with loosely typed data entry. But in c++ it fights back if there is a type conflict.
ok great with this now, i almost every error. everything but this: http://screencast.com/t/g023jZVP8ha
i fixed almost every*
Don't forget to change bird and rept below.
yeah right! But still.
Without seeing the debug log, it is hard to tell what the error is relating to. However, try fixing those and then see if it still happens.
Hmm... you have #include <iostream> using namespace std; so it should not be something undefined.
#include <iostream> using namespace std; char option,option2; int main() { cout << "Is it a dog, cat, bird, rept or other"; cin >> option; if (option == 'd' || option == 'c' || option == 'b' || option == 'r' ) { if (option == 'd') { cout << "Is it neutered or not?"; cin >> option2; if (option2 == 'y') cout << "50 euros." << endl; else if (option2 == 'n') cout << "80 euros.";} if (option == 'c') { cout << "Is it neutered or not?"; cin >> option2; if (option2 == 'y') cout << "40 euros."; else if (option2 == 'n') cout << "60 euros.";} if (option == 'b' || option == 'r') cout << "10 euros."; } else cout << "Error!" << endl; return 0; }
There it is. Can you check if it runs for you??
endl or \n your cout! It is needed!
so it works for you?
After I added an endl
hmmm.... which makes little sense. So I removed it and it ran again.
which compeller are you using?
compiler**
MS VisualStudio 2010.
Wait... are those manually added breakpoints? If so, it will halt on them!
Welp, I must go. Clear the break points and that part of the code should run. Then you can clean it up so that it actually does what the assignment asks, like take in C and c, not just one.
Thank you dude.
Join our real-time social learning platform and learn together with your friends!