-
December 3rd, 2010, 04:05 PM
#1
Can someone help with my C++ problem?
Hey all, I`m new to CodeGuru... And to C++... So I`m bound be be noob-like.
Anyways,
I`m in the process of making a program for my teacher, and I`m having problems with it.
It compiles just fine, but when I run it, it`s goes weird in some places.
This is the code:
Code:
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <string>
// This is a dichotomous key written in C++ since I didn't want
// to just write one the normal way. Enjoy, and make sure you give
// me 100%.
using namespace std;
int main(void)
{
system("title Dichotomous Key 1.0 by Jason TheGame Roddan");
system("color 04");
unsigned char input;
unsigned char input2;
cout << "This is a dichotomus key for Mrs. Peach" << endl;
cout << "Is the beetle hairy(a), or not(b)? (a/b): ";
do {
cin >> input;
if (input == 'b' || input == 'B') {
cout << "Is it black(a), or coloured(b)? (a/b): ";
cin >> input;
}
if (input == 'b' || input == 'b') {
cout << "Does it have spots(a), or not(b)? (a/b): ";
cin >> input;
}
if (input == 'b' || input == 'B') {
cout << "You have selected the Whirlgig Beetle.";
}
else
if (input == 'a' || input == 'A') {
cout << "Is it more than 2CM(a), or less(b)? (a/b): ";
cin >> input;
}
if (input == 'b' || input == 'B');
{
cout << "Is it dark(a), or light(b)? (a/b): ";
cin >> input;
}
if (input == 'b' || input == 'B') {
cout << "You have selected the drugstore beetle.";
}
if (input == 'a' || input == 'A') {
cout << "Does it have a long antenna(a), or a short one(b)? (a/b): ";
cin >> input;
}
if (input == 'b' || input == 'B') {
cout << "You have selected the Variegated beetle.";
system("pause");
system("exit");
}
if (input == 'a' || input == 'A') {
cout << "You have selected Predaceous diving beetle.";
}
if (input == 'a' || input == 'A') {
cout << "You have selected the Water Tiger beetle.";
}
if (input2 == 'a' || input2 == 'A') {
cout << "Is it more than 1 CM(a), or less(b)? (a/b): ";
cin >> input;
}
}
while (1);
system("pause");
return 0;
}
When I run the program, and I come to a "You have selected...." part, it outputs two "You have selected.." parts, with different answers.
Also, it skips some of the code when I make certain selections.... I know I can't explain my problem very well, nor can I program very well, for that matter. But I wanna get this done, and I want it to get done properly... SO I need help hahaha
THanks!
-
December 3rd, 2010, 05:37 PM
#2
Re: Can someone help with my C++ problem?
-
December 3rd, 2010, 05:37 PM
#3
Re: Can someone help with my C++ problem?
First of all, yikes on that formatting. Fix the indentation!
-
December 3rd, 2010, 05:38 PM
#4
Re: Can someone help with my C++ problem?
Originally Posted by Lindley
First of all, yikes on that formatting. Fix the indentation!
LOL. I used Dev++, it did it automatically.
-
December 3rd, 2010, 05:39 PM
#5
Re: Can someone help with my C++ problem?
Originally Posted by b000bies
Hey all, I`m new to CodeGuru... And to C++... So I`m bound be be noob-like.
Anyways,
I`m in the process of making a program for my teacher, and I`m having problems with it.
It compiles just fine, but when I run it, it`s goes weird in some places.
This is the code:
Code:
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <string>
// This is a dichotomous key written in C++ since I didn't want
// to just write one the normal way. Enjoy, and make sure you give
// me 100%.
using namespace std;
int main(void)
{
system("title Dichotomous Key 1.0 by Jason TheGame Roddan");
system("color 04");
unsigned char input;
unsigned char input2;
cout << "This is a dichotomus key for Mrs. Peach" << endl;
cout << "Is the beetle hairy(a), or not(b)? (a/b): ";
do {
cin >> input;
if (input == 'b' || input == 'B') {
cout << "Is it black(a), or coloured(b)? (a/b): ";
cin >> input;
}
if (input == 'b' || input == 'b') {
cout << "Does it have spots(a), or not(b)? (a/b): ";
cin >> input;
}
if (input == 'b' || input == 'B') {
cout << "You have selected the Whirlgig Beetle.";
}
else
if (input == 'a' || input == 'A') {
cout << "Is it more than 2CM(a), or less(b)? (a/b): ";
cin >> input;
}
if (input == 'b' || input == 'B');
{
cout << "Is it dark(a), or light(b)? (a/b): ";
cin >> input;
}
if (input == 'b' || input == 'B') {
cout << "You have selected the drugstore beetle.";
}
if (input == 'a' || input == 'A') {
cout << "Does it have a long antenna(a), or a short one(b)? (a/b): ";
cin >> input;
}
if (input == 'b' || input == 'B') {
cout << "You have selected the Variegated beetle.";
system("pause");
system("exit");
}
if (input == 'a' || input == 'A') {
cout << "You have selected Predaceous diving beetle.";
}
if (input == 'a' || input == 'A') {
cout << "You have selected the Water Tiger beetle.";
}
if (input2 == 'a' || input2 == 'A') {
cout << "Is it more than 1 CM(a), or less(b)? (a/b): ";
cin >> input;
}
}
while (1);
system("pause");
return 0;
}
When I run the program, and I come to a "You have selected...." part, it outputs two "You have selected.." parts, with different answers.
Also, it skips some of the code when I make certain selections.... I know I can't explain my problem very well, nor can I program very well, for that matter. But I wanna get this done, and I want it to get done properly... SO I need help hahaha
THanks!
Your indentation is messed up, but what you need to do is make sure you are using || (logical or) and && (logical and) where appropriate.
-
December 3rd, 2010, 05:43 PM
#6
Re: Can someone help with my C++ problem?
Originally Posted by jsg
Your indentation is messed up, but what you need to do is make sure you are using || (logical or) and && (logical and) where appropriate.
Okay, I cleaned it up.
It now looks normal hahaha,
But what do you meanuse it when appropriate?
I use the or operators for all of it... Perhaps I need to use the and instead?
Could you point me in the right direction?
THanks!
-
December 3rd, 2010, 06:00 PM
#7
Re: Can someone help with my C++ problem?
Originally Posted by b000bies
Okay, I cleaned it up.
It now looks normal hahaha,
But what do you meanuse it when appropriate?
I use the or operators for all of it... Perhaps I need to use the and instead?
Could you point me in the right direction?
THanks!
Forget what I said, that crazy indentation made it too difficult to follow the logic. Make sure all your ifs are actually testing the right conditions, and although I still see the indentation as being wonky, you may find it helpful to give your variables meaningful names that represent the data clearly. It seems you may be using input when you actually want to be using input2.
To be honest your code confuses me.
-
December 3rd, 2010, 06:06 PM
#8
Re: Can someone help with my C++ problem?
Originally Posted by jsg
Forget what I said, that crazy indentation made it too difficult to follow the logic. Make sure all your ifs are actually testing the right conditions, and although I still see the indentation as being wonky, you may find it helpful to give your variables meaningful names that represent the data clearly. It seems you may be using input when you actually want to be using input2.
To be honest your code confuses me.
Here is the cleaned up version... If that helps.
Code:
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <string>
// This is a dichotomous key written in C++ since I didn't want
// to just write one the normal way. Enjoy, and make sure you give
// me 100%.
using namespace std;
int main(void)
{
system("title Dichotomous Key 1.0 by Jason TheGame Roddan");
system("color 04");
unsigned char input;
unsigned char input2;
cout << "This is a dichotomus key for Mrs. Peach" << endl;
cout << "Is the beetle hairy(a), or not(b)? (a/b): ";
do {
cin >> input;
if (input == 'b' || input == 'B') {
cout << "Is it black(a), or coloured(b)? (a/b): ";
cin >> input;
}
if (input == 'b' || input == 'b') {
cout << "Does it have spots(a), or not(b)? (a/b): ";
cin >> input;
}
if (input == 'b' || input == 'B') {
cout << "You have selected the Whirlgig Beetle.";
}
else
if (input == 'a' || input == 'A') {
cout << "Is it more than 2CM(a), or less(b)? (a/b): ";
cin >> input;
}
if (input == 'b' || input == 'B');
{
cout << "Is it dark(a), or light(b)? (a/b): ";
cin >> input;
}
if (input == 'b' || input == 'B') {
cout << "You have selected the drugstore beetle.";
system("pause");
exit (1);
}
if (input == 'a' || input == 'A') {
cout << "Does it have a long antenna(a), or a short one(b)? (a/b): ";
cin >> input;
}
if (input == 'b' || input == 'B') {
cout << "You have selected the Variegated beetle." << endl;
system("pause");
exit (1);
}
if (input == 'a' || input == 'A') {
cout << "You have selected Predaceous diving beetle." << endl;
system("pause");
exit (1);
}
if (input == 'a' || input == 'A') {
cout << "You have selected the Water Tiger beetle." << endl;
}
if (input2 == 'a' || input2 == 'A') {
cout << "Is it more than 1 CM(a), or less(b)? (a/b): ";
cin >> input;
}
}
while (1);
system("pause");
return 0;
}
-
December 3rd, 2010, 06:19 PM
#9
Re: Can someone help with my C++ problem?
Egads man, you're killing me!
Code:
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <string>
// This is a dichotomous key written in C++ since I didn't want
// to just write one the normal way. Enjoy, and make sure you give
// me 100%.
using namespace std;
int main(void) {
system("title Dichotomous Key 1.0 by Jason TheGame Roddan");
system("color 04");
unsigned char input;
unsigned char input2;
cout << "This is a dichotomus key for Mrs. Peach" << endl;
cout << "Is the beetle hairy(a), or not(b)? (a/b): ";
do {
cin >> input;
if (input == 'b' || input == 'B') {
cout << "Is it black(a), or coloured(b)? (a/b): ";
cin >> input;
}
if (input == 'b' || input == 'b') {
cout << "Does it have spots(a), or not(b)? (a/b): ";
cin >> input;
}
if (input == 'b' || input == 'B') {
cout << "You have selected the Whirlgig Beetle.";
} else
if (input == 'a' || input == 'A') {
cout << "Is it more than 2CM(a), or less(b)? (a/b): ";
cin >> input;
}
if (input == 'b' || input == 'B');
{
cout << "Is it dark(a), or light(b)? (a/b): ";
cin >> input;
}
if (input == 'b' || input == 'B') {
cout << "You have selected the drugstore beetle.";
}
if (input == 'a' || input == 'A') {
cout << "Does it have a long antenna(a), or a short one(b)? (a/b): ";
cin >> input;
}
if (input == 'b' || input == 'B') {
cout << "You have selected the Variegated beetle.";
system("pause");
system("exit");
}
if (input == 'a' || input == 'A') {
cout << "You have selected Predaceous diving beetle.";
}
if (input == 'a' || input == 'A') {
cout << "You have selected the Water Tiger beetle.";
}
if (input2 == 'a' || input2 == 'A') {
cout << "Is it more than 1 CM(a), or less(b)? (a/b): ";
cin >> input;
}
} while (1);
system("pause");
return 0;
}
Edit: Okay the first thing that strikes me is: That's an awful lot of if statements for 2 variables..
Last edited by jsg; December 3rd, 2010 at 06:23 PM.
-
December 3rd, 2010, 06:47 PM
#10
Re: Can someone help with my C++ problem?
A million if/else statements is not how you get something like this done. Maybe you could condense you questions to ask down to a std::vector<std::string>, then iterate through the questions. That alone will kill 75% of your codes volume and make it easier to follow.
Next you could store the name of the bug with it's corresponding answers into a std::map like this:
Code:
std::map<std::string, std::string> answers;
answers["bbb"] = "Whirlgig Beetle";
answers["bba"] = "Drugstore Beetle";
etc...
You can just append the character the user enters to a string to be used as a key to the map. You can also use std::tolower to make sure it's lower case. If you do this, your code will probably be 1/5 it's current size, and will consist of one main loop that is easy to read. If all goes well, your program should only contain a couple if statements tops.
-
December 3rd, 2010, 07:35 PM
#11
Re: Can someone help with my C++ problem?
Originally Posted by Chris_F
A million if/else statements is not how you get something like this done. Maybe you could condense you questions to ask down to a std::vector<std::string>, then iterate through the questions. That alone will kill 75% of your codes volume and make it easier to follow.
Next you could store the name of the bug with it's corresponding answers into a std::map like this:
Code:
std::map<std::string, std::string> answers;
answers["bbb"] = "Whirlgig Beetle";
answers["bba"] = "Drugstore Beetle";
etc...
[...]
A really good idea, but I'm afraid it's not the final solution either. IMO the problem essentially boils down to traversing a tree structure, one step per question/answer pair. That's because the questions to ask depend on the answers given so far.
I actually have written something similar (the idea, not the resulting code ) myself many years ago. But as it was meant to be sample code to demonstrate the usage of trees, of course it used a tree right from the start.
Essentially, the keys in your map describe the path to reach the respective leaf in the tree. But of course there also needs to be some sort of container for the questions to ask. The OP could, following your model, use the same map for that as well, with the keys to the questions just being partial paths. The values could then be structures containing a boolean flag indicating whether the associated string is a question or a final answer. But maybe the reason behind that structure would be less obvious compared to an actual tree.
However, to make the tree solution work, the OP would not only need to face the general techniques of dealing with a tree, but also, more or less, serialization and deserialization of the tree...
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
December 4th, 2010, 11:05 AM
#12
Re: Can someone help with my C++ problem?
At the top of your program you test for lowercase b twice in the same line. It's trivial compared to the rest of your problem. But I though I'd point it out to you anyway.
Code:
if (input == 'b' || input == 'b')
-
December 6th, 2010, 02:08 PM
#13
Re: Can someone help with my C++ problem?
Thanks for all the help guys!
I guess this is a little too complex for me yet. But hopefully I'll get it all soon!
-
December 7th, 2010, 01:56 PM
#14
Re: Can someone help with my C++ problem?
Originally Posted by b000bies
Thanks for all the help guys!
I guess this is a little too complex for me yet. But hopefully I'll get it all soon!
You may think of using a switch statement. Though that is not the most elegant way it is straight forward and you probably wouldn't struggle with that approach as you do now.
Code:
...
// for first input use variable input
cin >> input;
// for second input use variable input2
switch(input)
{
case 'a':
case 'A': // the following sequence will be used for either 'a' or 'A'
{
cout << "Is it more than 2CM(a), or less(b)? (a/b): ";
cin >> input2;
switch (input2)
{
case 'a':
case 'A':
{
// add here the code for input=='a' and input2=='a'
}
break; // that leaves the switch statement for input2
case 'b':
case 'B':
{
// add here the code for input=='a' and input2=='b'
}
break;
}
}
break; // that leaves the switch statement for input=='a'
case 'b':
case 'B':
{
// and so on ...
}
break; // that leaves the switch statement for input=='b'
// more valid cases?
// case 'c':
// case 'C':
// invalid case
default:
{
cout << "wrong input " << endl;
}
break;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|