Ask your own question, for FREE!
Computer Science 17 Online
OpenStudy (christos):

C++, Can you help me please? I have to create a program that does this: http://screencast.com/t/VpKYkgfMf5

OpenStudy (christos):

// // 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; }

OpenStudy (christos):

Where is my mistake in the code? The code doesn't run.

OpenStudy (anonymous):

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'

OpenStudy (anonymous):

hope that helps

OpenStudy (christos):

That didn't fix it I am afraid but thx for trying to help :/

OpenStudy (anonymous):

option is a string? o.o

OpenStudy (christos):

I dont know what is a string :S I am sorry

OpenStudy (anonymous):

c++ has support for strings? else you got to declare an array of chars

OpenStudy (anonymous):

just replace int with string the first line of main block

OpenStudy (christos):

still doesnt work.

OpenStudy (e.mccormick):

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.

OpenStudy (christos):

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.

OpenStudy (e.mccormick):

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.

OpenStudy (christos):

oooh hold on lemme try

OpenStudy (e.mccormick):

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.

OpenStudy (e.mccormick):

http://www.cplusplus.com/doc/tutorial/variables/

OpenStudy (christos):

ok great with this now, i almost every error. everything but this: http://screencast.com/t/g023jZVP8ha

OpenStudy (christos):

i fixed almost every*

OpenStudy (e.mccormick):

Don't forget to change bird and rept below.

OpenStudy (christos):

yeah right! But still.

OpenStudy (e.mccormick):

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.

OpenStudy (e.mccormick):

Hmm... you have #include <iostream> using namespace std; so it should not be something undefined.

OpenStudy (christos):

#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; }

OpenStudy (christos):

There it is. Can you check if it runs for you??

OpenStudy (e.mccormick):

endl or \n your cout! It is needed!

OpenStudy (christos):

so it works for you?

OpenStudy (e.mccormick):

After I added an endl

OpenStudy (e.mccormick):

hmmm.... which makes little sense. So I removed it and it ran again.

OpenStudy (christos):

which compeller are you using?

OpenStudy (christos):

compiler**

OpenStudy (e.mccormick):

MS VisualStudio 2010.

OpenStudy (e.mccormick):

Wait... are those manually added breakpoints? If so, it will halt on them!

OpenStudy (e.mccormick):

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.

OpenStudy (christos):

Thank you dude.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!