C++ help

Dnsgm

Baseband Member
Messages
61
It's a simple program but somehow it doesn't go on after the switch command. Ya, so some help would be great.


Code:
#include <iostream>
using namespace std;

class cat
{
      public:
             char name[50];
             char eyeclr[15];
             char fur[15];
             
             int itsage;
             float itsweight;
             
             void meow();
};
void meow(){
     cout<< "Meow\n\n";
};                              

int addition (int a, int b)
{
    int c;
    c = a + b;
}
int subtraction (int a, int b)
{
    int c;
    c = a - b;
}
int multiply (int a, int b)
{
    int c;
    c = a * b;
}

int division (int a, int b)
{
    int c;
    c = a / b;
}

int main()
{
    cat cx;
    char action[30];
    
    cout<< "Make your own cat.\n" <<endl;
    cout<< "What's its name?: ";
    cin>>cx.name;
    cout<< endl;
    
    cout<< "What's its eye color?: ";
    cin>>cx.eyeclr;
    cout<<endl;
    
    cout<< "What's its fur color?: ";
    cin>>cx.fur;
    cout<< endl;
    
    cout<< "Hi, " <<cx.name << " who has the eye color of ";
    cout<< cx.eyeclr << " and who has " << cx.fur << " colored fur.\n";
    
    cout<< "How old is it?: ";
    cin>>cx.itsage;
    cout<< endl;
    
    cout<< "How much does it weigh?: ";
    cin>>cx.itsweight;
    cout<< endl;
    
    cout<< "So it's " <<cx.itsage << " and it's " << cx.itsweight << ", cool.\n";
    cout<< endl;
    
    cout<< "What would you like " << cx.name << " do?: \n";
    cout<< "bark\nmeow\nor quack\n";
    cin>> action;
    cin.ignore();
    cout<< endl;
    
    if (action == "bark" || "BARK" || "Bark") cout<< "Cats can't bark, dogs can!\n";
    else if (action == "meow" || "MEOW" || "Meow") cout<< "Meow\n";
    
    else {
         cout<< "Cats can't quack! Ducks can!\n";
         }
}
    
    char ans1[10];
    
    cout<< "Would you like to teach mathematics to it? (y or n): \n";
    cin>> ans1;
    
    if (ans1 == "y") {
             
             char form;
             int t1, t2, t3, t4, t5, t6, t7, t8, t9, t10;
             char learn[10];
             
             
             cout<< "Choose a formula: ";
             cout<< endl << "addition\n";
             cout<< "subtraction\n";
             cin>> form;
             cin.ignore();
             
             switch ("addition" || "subtraction")
             {
                    case 0:
                         cout<< "Let's just stick between 1 and 10. (1+1=2, 1+2=3 format only): \n";
                         cin>>t1;
                         cout<< endl;
                         cin>>t2;
                         cout<< endl;
                         cin>>t3;
                         cout<< endl;
                         cin>>t4;
                         cout<< endl;
                         cin>>t5;
                         cout<< endl;
                         cin>>t6;
                         cout<< endl;
                         cin>>t7;
                         cout<< endl;
                         cin>>t8;
                         cout<< endl;
                         cin>>t9;
                         cout<< endl;
                         cin>>t10;
                         cout<< endl;
                         cout<< endl;
                         
                         cout<< cx.name << " has learned all that, but sadly I don't think he can learn anymore.\n";
                         cout<< "would you like to see what he has learned? (y or n)\n";
                         cin>>learn;
                         
                         if (learn == "y"){
                                   cout<< "Show us what you know " <<cx.name;
                                   
                                   void meow();
                                   
                                   cout<<t1;
                                   cout<< endl;
                                   cout<<t2;
                                   cout<< endl;
                                   cout<<t3;
                                   cout<< endl;
                                   cout<<t4;
                                   cout<< endl;
                                   cout<<t5;
                                   cout<< endl;
                                   cout<<t6;
                                   cout<< endl;
                                   cout<<t7;
                                   cout<< endl;
                                   cout<<t8;
                                   cout<< endl;
                                   cout<<t9;
                                   cout<< endl;
                                   cout<<t10;
                                   cout<< endl;
                                   cout<< endl;
                                   }
                         else {
                              cin>>learn;
                         }
                              
                         cout<< "I think " <<cx.name << " is getting sleepy now.\n Bye-bye!";
                         
                         break;
                         
                    case 1:
                         cout<< "Let's just stick between 1 and 10. (10-1=9, 10-2=8 format only): \n";
                         cin>>t1;
                         cout<< endl;
                         cin>>t2;
                         cout<< endl;
                         cin>>t3;
                         cout<< endl;
                         cin>>t4;
                         cout<< endl;
                         cin>>t5;
                         cout<< endl;
                         cin>>t6;
                         cout<< endl;
                         cin>>t7;
                         cout<< endl;
                         cin>>t8;
                         cout<< endl;
                         cin>>t9;
                         cout<< endl;
                         cin>>t10;
                         cout<< endl;
                         cout<< endl;
                         
                         cout<< cx.name << " has learned all that, but sadly I don't think he can learn anymore.\n";
                         cout<< "would you like to see what he has learned? (y or n)\n";
                         cin>>learn;
                         
                         if (learn == "y"){
                                   cout<< "Show us what you know " <<cx.name;
                                   
                                   void meow();
                                   
                                   cout<<t1;
                                   cout<< endl;
                                   cout<<t2;
                                   cout<< endl;
                                   cout<<t3;
                                   cout<< endl;
                                   cout<<t4;
                                   cout<< endl;
                                   cout<<t5;
                                   cout<< endl;
                                   cout<<t6;
                                   cout<< endl;
                                   cout<<t7;
                                   cout<< endl;
                                   cout<<t8;
                                   cout<< endl;
                                   cout<<t9;
                                   cout<< endl;
                                   cout<<t10;
                                   cout<< endl;
                                   cout<< endl;
                                   }
                         else {
                              cin>>learn;
                         }
                              
                         cout<< "I think " <<cx.name << " is getting sleepy now.\n Bye-bye!";
                         
                         break;
                         
                         
                    default:
                            cout<< "Choose a formula: ";
                            cout<< endl << "addition\n";
                            cout<< "subtraction\n";
                            cout<< "multiplication\n";
                            cout<< "division\n";
                            cin>> form;
                            cin.ignore();
                            
                    }
             }
             

         
         cin.get();
         return 0;
}
 
Just a couple of quick suggestions. First, break out some more functions from main, it will make it less confusing to debug and easier for the rest of us to understand. Second, I don't really understand from your post whether you're getting a compile error or a runtime. If you're seeing a runtime, then the code you're compiling from is different from that you've posted, since I can see at least 4 errors that would stop compile. Post a bit more information also, such as os and compiler next time please. Best I can tell, you need to look at braces and semi-colons first, they aren't where they oughta' be in the posted code. I hope this helps. :)
 
Well it works, but the problem is that when it reaches to the 'switch' command, the program gets terminated. Thats pretty much the only problem.
 
you need a char array or a string, not a single char...


char form; //needs to be char form [buffer]; or include strings and use string
int t1, t2, t3, t4, t5, t6, t7, t8, t9, t10;
char learn[10];


cout<< "Choose a formula: ";
cout<< endl << "addition\n";
cout<< "subtraction\n";
cin>> form;
cin.ignore();

switch ("addition" || "subtraction") /*char only takes the first letter from a string so it'd never make either condition true, you could try using "s" and "a"*/
 
Back
Top Bottom