Page 1 of 2
1
2
LastLast
  1. #1

    Comparing stored data types in C++

    Hi, just recently getting into c++ and have a question:

    I'm currently trying to make an animal database that stores the data of animals and tells you what sounds they make.

    I got everything done besides comparing the size of animals

    I have this so far

    #include <iostream>
    #include <math.h>
    #include <string>
    #include <string.h>

    using namespace std;

    int main()
    {
    int catSize = 3;
    int dogSize = 4;
    int parrotSize = 2;
    int cowSize = 7;
    int mouseSize = 1;
    int dolphinSize = 6;
    int horseSize =8;
    int sheepSize = 5;

    int input = 0;

    string catNoise = "This animal goes Meow!";
    string dogNoise = "This animal goes Ruff!";
    string parrotNoise = "This animal goes Caw!";
    string cowNoise = "This animal goes Moo!";
    string mouseNoise = "This animal goes snfnsfznfnz!";
    string dolphinNoise = "This animal goes sneeeeh!";
    string horseNoise = "This animal goes Nayyy!";
    string sheepNoise = "This animal goes Bawww!";

    cout << "Welcome to the Animal Database!" << endl;
    cout << "Please choose one of the following animals to compare!" << endl;

    cout << "Cat = 1" << endl;
    cout << "Dog = 2" << endl;
    cout << "Parrot = 3" << endl;
    cout << "Cow = 4" << endl;
    cout << "Mouse = 5" << endl;
    cout << "Dolphin = 6" << endl;
    cout << "Horse = 7" << endl;
    cout << "Sheep = 8" << endl;

    cout << "Enter your number here: " << endl;
    cin >> input;

    if (input > 8)
    {
    cout << "You're an asshole!" << endl;
    system ("pause");
    }

    if (input < 1)
    {
    cout << "You're an asshole!" << endl;
    system ("pause");

    if (input = 1)
    {
    cout << catNoise << endl;
    system ("Pause");
    }


    return 0;
    }
    If they choose cat how do I make it compare the size and output the animal names like this:

    Cat is bigger then:
    Mouse, parrot

    Cat is smaller then:
    Cow, Dolphin, Horse, Sheep

    Dumbest Thread 2012

  2. #2
    Deleted
    Print cat is biggger than (THAN NOT THEN!) if there is something that's cat is bigger than, and then print each of them without pritning endline. Simple for() usage will do all you want.
    Same for smaller animals
    eg
    Code:
    cout << animalName << " is smaller than:" << endl;
    for (int i =0; i<MAX_ANIMAL;++i)
    {
    if (animalSize[i] < myAnimalSize)
    {
    if (i>0){ cout << ", ";}
    cout << animal[i];
    }
    cout <<endl;
    }
    but either way you data structure sucks completely, you'd better use some advantage of c++ and have your data organized like this
    Code:
    struct animal
    {
        char *name;
        int size;
        char *noiseString;
       
        animal(char *_name, int _size, char *_noiseString) : name(_name), size(_size), noiseString(_noiseString);
    };
    Learn about classes, access to them and their usage.

  3. #3
    Deleted
    @Burritaco isn't programming for very long, don't ya ? I would estimate about 2 to 3 Days.

    If fiddling around with classes is a bit too complex to you for now, maybe you will get better results with arrays. Organize your animals and their properties in structs (as suggested before) and all these nifty structs into arrays.

    This will make iterating through your animals much easier, producing much less code and will teach you a lot more, especially in direction to classes (which are very close to structs).

  4. #4
    Quote Originally Posted by Zalthor View Post
    @Burritaco isn't programming for very long, don't ya ? I would estimate about 2 to 3 Days.

    If fiddling around with classes is a bit too complex to you for now, maybe you will get better results with arrays. Organize your animals and their properties in structs (as suggested before) and all these nifty structs into arrays.

    This will make iterating through your animals much easier, producing much less code and will teach you a lot more, especially in direction to classes (which are very close to structs).
    Lol yeah, less then that, started yesterday night.

    I tried this, but I don't know what's wrong, visual studio says:
    ]
    1> while trying to match the argument list '(std:stream, main::animal)'

    this is what I am trying:

    Code:
    #include <iostream>
    #include <string>
    #include <math.h>
    using namespace std;
    
    int main()
    {
    	struct animal {
    		int size;
    		string animalNoise;
    	} ;
    	
    	animal cat;
    	animal dog;
    	
    	cout << "enter dog, 15: " << endl;
    	getline (cin,animal.size[0]);
    	cout << dog << endl;
    	system ("pause");
    	
    	return 0;
    }
    Last edited by Burritaco; 2012-03-11 at 06:02 AM.

    Dumbest Thread 2012

  5. #5
    You aren't using structs properly.

    In your code, animal is not a variable, it is a data type. cat and dog are variable names of type animal (in this case, a struct containing an integer of size and a string of animalNoise)

    To input size data into a struct in this code, you want to store data to dog.size or cat.size.

    You also cannot just use

    cout << dog << endl;

    You have to specify which part of dog you want to cout. dog.size or dog.animalNoise.

    I'm rusty on getline so once I patch up the code I'll post it. But here it is with cin. (And by rusty I mean I've never used it and can't seem to get it to work for some reason I don't fully understand)

    Code:
    #include <iostream>
    #include <string>
    #include <math.h>
    using namespace std;
    
    int main()
    {
    	struct animal {
    		int size;
    		string animalNoise; };
    	
    	animal cat;
    	animal dog;
    	
    	cout << "enter dog, 15: ";
    	cin >> dog.size;
    	cout << dog.size << endl;
    	system ("pause");
    	
    	return 0;
    }
    Not claiming to be an expert, here. Just began C++ myself a few months ago and my class is moving at a snail's pace and I'm so bored I don't keep up on the nonassigned work, which will eventually bite me in the ass, I'm sure...


    Edit:

    getline(cin,var_here);

    only works with strings and characters. You cannot use getline to read in integer data, hence the breakage after fixing the other errors. Stick to cin for numbers.
    Last edited by Dirgon; 2012-03-11 at 07:37 AM.

  6. #6
    Mmm, you should have probably started with something simpler But yeah, let's talk about your code. A big problem is that you declare each animal as a separate variable, this means that you must add additional code when you add an animal. This is not very efficient. Note that I don't use C++ and I don't know the C++ class system, so its possible that I will do some mistakes in the code (I am not testing this), but its more about the idea. I suggest that you define the basic properties of animals in arrays, so that you have a single variable holding the properties of the animals. You can also use structs of arrays, I use two arrays for simplicity. Each animal can be then referred to as a unique number (osition in the arrays).

    Code:
    string animal_name[] = {"Cat", "Dog", "Cow"}
    string animal_sound[] = {"Meow", "Ruff", "Moo"}
    string animal_size[] = {2, 4, 20}
    
    #define NUMBER_OF_ANIMALS 3

    Then you can do something like

    Code:
    cout << "You have selected: " << animal_name[input] << ", it makes " << animal_sound[input] << endl;
    
    // fidn all which is smaller
    for(int i=0; i<NUMBER_OF_ANIMALS; i++)
    if(animal_size[i]<animal_size[input])
    cout << animal_name[input] << " it bigger than " << animal_name[i] << endl;
    
    // fidn all which is bigger
    for(int i=0; i<NUMBER_OF_ANIMALS; i++)
    if(animal_size[i]>animal_size[input])
    cout << animal_name[input] << " it smaller than " << animal_name[i] << endl;

    Hope it helps

  7. #7
    Deleted
    Code:
    while trying to match the argument list '(std:stream, main::animal)'
    You need to have stream function for your class, in this case it'd be something like
    Code:
    ostream& operator<<(ostream& ost,animal animal)
    {
          // <your output> eg. ost << animal.size;
     
        return ost;
    }
    Also while using class you should first create it's instance, in your case where you have static data you will likely do it just at the beginning of your code (and yes arrays are even better for this kind of static simple stuff), and that is why I put in constructor in the struct (animal(char *_name, int _size, char *_noiseString) : name(_name), size(_size), noiseString(_noiseString).
    Either way you should always start with lessons you can handle on your own.

  8. #8
    Well, i havent worked in C++ for a long time so i am rusty at writing the code in c++(there might be mistakes in code, but the general idea should be pretty visible). Not saying that the solutions other gave you are bad, but working with pointers, classes etc. might make you lose the will to do it, since you are new to c++.

    I know the code that i did some will consider bad, but i tried to make it as simple, so that someone who just started c++ can understand whats going on.


    Code:
    int y = 0;
    if (input == 1 )
      y = 3;
    else if (input == 2 )
     y = 4;
    else if (input == 3 )
     y = 2;
    else if (input == 4 )
     y = 7;
    else if (input == 5 )
     y = 1;
    else if (input == 6 )
     y = 6;
    else if (input == 7 )
     y = 8;
    else if (input == 8 )
     y = 5;
    
    
    if (input == 1 )
      string animal = ("Cat");
    else if (input == 2 )
     string animal = ("Dog");
    else if (input == 3 )
     string animal = ("Parrot");
    else if (input == 4 )
     string animal = ("Cow");
    else if (input == 5 )
     string animal = ("Mouse");
    else if (input == 6 )
     string animal = ("Dolphin");
    else if (input == 7 )
     string animal = ("Horse");
    else if (input == 8 )
     string animal = ("Sheep");
    
    if ( y == 1)
    cout << ("Mouse is not bigger then any animal"); << endl;
    else {
    cout << animal << " is bigger than:" << endl;
    for(i=0;i<y;i++){
    if (i == 1 )
      cout << ("Mouse ");
    else if (i == 2 )
     cout <<("Parrot ");
    else if (i == 3 )
     cout <<("Cat ");
    else if (i == 4 )
     cout <<("Dog ");
    else if (i == 5 )
     cout <<("Sheep ");
    else if (i == 6 )
     cout <<("Dolphin ");
    else if (i == 7 )
     cout <<("Cow ");
    }
    }
    
    if ( y == 8)
    cout << ("Horse is not smaller then any animal"); << endl;
    else {
    cout << animal << " is smaller than:" << endl;
    for(i=y+1;i<9;i++){
    if (i == 2 )
      cout << ("Parrot ");
    else if (i == 3 )
     cout << ("Cat ");
    else if (i == 4 )
     cout << ("Dog ");
    else if (i == 5 )
     cout << ("Sheep ");
    else if (i == 6 )
     cout << ("Dolphin ");
    else if (i == 7 )
     cout << ("Cow ");
    else if (i == 8 )
     cout << ("Horse ");
    }
    }
    Last edited by Itakas; 2012-03-11 at 02:35 PM.

  9. #9
    Yeah most of this stuff is going right over my head, where should I be starting?

    Dumbest Thread 2012

  10. #10
    Quote Originally Posted by Burritaco View Post
    Yeah most of this stuff is going right over my head, where should I be starting?
    With easier examples ^^ Get one of these "C in 21 days" or how they are called books and work through it. Variables, loops, arrays etc. Don't go into C++ stuff just yet, the OOP (object oriented) system of C++ is extremely messy and idiosyncratic, as it is not much more but a syntactic sugar over the underlaying C stuff. You can learn it after you are confident with basic C algorithms/memory management and things like that. It may also help to start learning OOP from a cleaner language like Java, Python or Ruby.

  11. #11
    I'm going to give this one last go, why isn't this working, this doesn't make any sense to me?

    Code:
    #include <iostream>
    #include <string>
    #include <math.h>
    using namespace std;
    
    int main ()
    {
    	struct animal {
    		int size;
    		string animalNoise;
    	} ; 
    
    	int ID = 0;
    	
    	animal cat;
    	animal dog;
    	animal dolphin;
    	animal mouse;
    	animal parrot;
    	animal horse;
    	animal sheep;
    	animal cow;
    
    	dog.size = 20;
    	dog.animalNoise = "I go Bark!";
    	
    	cat.size = 15;
    	cat.animalNoise = "I go Meow!";
    	
    	dolphin.size = 150;
    	dolphin.animalNoise = "I go LEEER!";
    	
    	mouse.size = 3;
    	mouse.animalNoise = "I go Snifnsnzns!";
    	
    	parrot.size = 4;
    	parrot.animalNoise = "I go Caww!";
    	
    	horse.size = 350;
    	horse.animalNoise = "I go Nayyy!";
    	
    	sheep.size = 120; 
    	sheep.animalNoise = "I go Baww!";
    	
    	cow.size = 200;
    	cow.animalNoise = "I go Mooo!";
    
    	cout << "Welcome to the Zoo! Please select from one of these animals to begin!\n" << endl;
    	cout << "Cat = 1" << endl;
    	cout << "Dog = 2" << endl;
    	cout << "Dolphin = 3" << endl;
    	cout << "Mouse = 4" << endl;
    	cout << "Parrot = 5"<<endl;
    	cout << "Horse = 6" << endl;
    	cout << "Sheep = 7" << endl;
    	cout << "Cow = 8" << endl;
    	
    	cin >> ID >> endl;
    
    
    	system ("pause");
    	
    	return 0;
    }
    The last line when I input ID is what is breaking the program and I don't understand why.

    This is what visual studio is giving me:

    Code:
    >          while trying to match the argument list '(std::basic_istream<_Elem,_Traits>, overloaded-function)'
    1>          with
    1>          [
    1>              _Elem=char,
    1>              _Traits=std::char_traits<char>
    1>          ]
    1>
    1>Build FAILED.
    1>
    1>Time Elapsed 00:00:00.53
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Last edited by Burritaco; 2012-03-11 at 06:30 PM.

    Dumbest Thread 2012

  12. #12
    endl isn't input operator, so don't type it with cin!
    Intel Core i5 2500k @ 4.7GHz | MSI GTX 980 Gaming 4G x2 in SLI | ASRock Extreme3 Gen3 Motherboard
    8 GB of Kingston HyperX DDR3 | Western Digital Caviar Green 1 TB | Western Digital Caviar Blue 1 TB
    2x Samsung 840 Pro 128 GB + Corsair Force 3 120 GB SSDs (three-way raid 0)
    Cooler Master HAF 912 plus case | Corsair AX1200 power supply | Thermaltake NiC C5 Untouchable CPU cooler
    Asus PG278Q ROG SWIFT (1440p @ 144 Hz, GSync + 3D vision)

  13. #13
    Quote Originally Posted by Sunius View Post
    endl isn't input operator, so don't type it with cin!
    It is still throwing the same error.

    Code:
    int ID = 0;
    cin >> ID >>

    Dumbest Thread 2012

  14. #14
    Try like this:

    Code:
    int ID = 0;
    cin >> ID;
    Intel Core i5 2500k @ 4.7GHz | MSI GTX 980 Gaming 4G x2 in SLI | ASRock Extreme3 Gen3 Motherboard
    8 GB of Kingston HyperX DDR3 | Western Digital Caviar Green 1 TB | Western Digital Caviar Blue 1 TB
    2x Samsung 840 Pro 128 GB + Corsair Force 3 120 GB SSDs (three-way raid 0)
    Cooler Master HAF 912 plus case | Corsair AX1200 power supply | Thermaltake NiC C5 Untouchable CPU cooler
    Asus PG278Q ROG SWIFT (1440p @ 144 Hz, GSync + 3D vision)

  15. #15
    Quote Originally Posted by Sunius View Post
    Try like this:

    Code:
    int ID = 0;
    cin >> ID;
    Well shit, I just messed up the easiest part of c++

    ---------- Post added 2012-03-11 at 01:57 PM ----------

    Okay I just wrote a for and if loop, but I don't know an easy way to make it compare to animals in the structure, this is what I made

    Code:
    	for (ID == 1; cat.size<400; cat.size++)
    	{
    		if (cat.size == horse.size)
    		{
    			cout << "I am smaller than: Horse." << endl;
    		}
    	}
    This was my first time making a loop, no idea if I could make it easier, but it did work, is there an easy way to add every animal to that without having to put it in myself?

    ---------- Post added 2012-03-11 at 02:17 PM ----------

    I finished it, but it's quite possible the worse coding in history

    Code:
    for (ID == 1; cat.size<400; cat.size++)
    	{
    		if (cat.size == 15)
    		{
    			cout << cat.animalNoise << endl;
    		}
    		if (cat.size == 16)
    		{
    			cout << "\nI am smaller than: \n" << endl;
    		}
    		if (cat.size == horse.size)
    		{
    			cout << "Horse!\n" << endl;
    		}
    		if (cat.size == sheep.size)
    		{
    			cout << "Sheep!" << endl;
    		}
    		if (cat.size == dog.size)
    		{
    			cout <<"Dog!" << endl;
    		}
    		if (cat.size == dolphin.size)
    		{
    			cout <<"Dolphin!"<<endl;
    		}
    		if (cat.size == cow.size)
    		{
    			cout <<"Cow!"<<endl;
    		}
    
    	}
    
    	for (ID ==1; cat.size>0; cat.size--)
    	{
    		if (cat.size == 14)
    		{
    			cout <<"I am bigger than: \n"<< endl;
    		}
    		if (cat.size == mouse.size)
    		{
    			cout <<"Mouse!" << endl;
    		}
    		if (cat.size == parrot.size)
    		{
    			cout <<"Parrot!" << endl;
    		}
    	}

    Dumbest Thread 2012

  16. #16
    Here you go, hope that helps!

    Code:
    #include <unordered_map>
    #include <algorithm>
    #include <iterator>
    #include <string>
    #include <cstddef>
    
    #include <boost/assign/list_of.hpp>
    #include <boost/algorithm/string.hpp>
    
    struct animal_info
    {
        std::size_t size;
        std::string noise;
    };
    
    int main()
    {
        typedef std::unordered_map<std::string, animal_info> zoo;
    
        zoo animals = boost::assign::map_list_of
            ("dog", animal_info{ 20, "Bark" })
            ("cat", animal_info{ 15, "Meow" })
            ("dolphin", animal_info{ 150, "LEER" })
            ("mouse", animal_info{ 3, "Snifnsnzns" })
            ("parrot", animal_info{ 4, "Caww" })
            ("horse", animal_info{ 350, "Nayyy" })
            ("sheep", animal_info{ 120, "Baww" })
            ("cow", animal_info{ 200, "Mooo" })
            ;
    
        std::string animal;
        do
        {
            std::cout << "Which animal are you interested in? " << std::flush;
            std::cin >> animal;
        } while (!animal.size());
    
        boost::algorithm::to_lower(animal);
    
        zoo::const_iterator iter = animals.find(animal);
        if (iter != animals.end())
        {
            animal_info const& self = iter->second;
            std::cout << "I go " << self.noise << "!\n\n";
    
            std::vector<std::pair<std::string, animal_info>> other_animals;
            std::remove_copy_if(std::begin(animals), std::end(animals),
                std::back_inserter(other_animals), [&](zoo::value_type const& other)
                {
                    return other.first == animal;
                });
    
            auto middle = std::partition(std::begin(other_animals),
                std::end(other_animals), [&](zoo::value_type const& animal)
                {
                    return animal.second.size > self.size;
                });
    
            auto show_info = [&](zoo::value_type const& animal)
                {
                    animal_info const& other = animal.second;
                    auto sizes = std::minmax(self.size, other.size);
                    std::cout << animal.first << " by " << sizes.second - sizes.first << " kg\n";
                };
    
            std::cout << "The following animals are heavier than me:\n";
            std::for_each(std::begin(other_animals), middle, show_info);
    
            std::cout << "\nThe following are lighter than me:\n";
            std::for_each(middle, std::end(other_animals), show_info);
    
            std::cout << std::endl;
        }
        else
        {
            std::cout << "There's no such thing as "
                << (boost::algorithm::is_any_of("aeiou")(animal.front()) ? "an" : "a")
                << ' ' << animal << '!' << std::endl;
        }
    }

  17. #17
    Good work ZyngaFail, way to confuse him...

  18. #18
    Quote Originally Posted by mafao View Post
    Good work ZyngaFail, way to confuse him...
    I think I picked up a few things from that,

    Code:
    std::flush;
    seems interesting

    also constant interation I think is something I could figure out.

    also he doesn't use

    Code:
    using namespace std;
    Inputting that directly into my visual studio didn't work either, so I must not have the header files.

    Also the way he maps the information, animal, size, noise makes sense
    Last edited by Burritaco; 2012-03-11 at 08:19 PM.

    Dumbest Thread 2012

  19. #19
    This is a working code of what you are trying to make. Its as simple as i could make it.

    Code:
    #include <iostream>
     #include <string>
    
     using namespace std;
    
     int main()
     {
     int catSize = 3;
     int dogSize = 4;
     int parrotSize = 2;
     int cowSize = 7;
     int mouseSize = 1;
     int dolphinSize = 6;
     int horseSize =8;
     int sheepSize = 5;
    
     int input = 0;
    
     string catNoise = "This animal goes Meow!";
     string dogNoise = "This animal goes Ruff!";
     string parrotNoise = "This animal goes Caw!";
     string cowNoise = "This animal goes Moo!";
     string mouseNoise = "This animal goes snfnsfznfnz!";
     string dolphinNoise = "This animal goes sneeeeh!";
     string horseNoise = "This animal goes Nayyy!";
     string sheepNoise = "This animal goes Bawww!";
    
     cout << "Welcome to the Animal Database!" << endl;
     cout << "Please choose one of the following animals to compare!" << endl;
    
     cout << "Cat = 1" << endl;
     cout << "Dog = 2" << endl;
     cout << "Parrot = 3" << endl;
     cout << "Cow = 4" << endl;
     cout << "Mouse = 5" << endl;
     cout << "Dolphin = 6" << endl;
     cout << "Horse = 7" << endl;
     cout << "Sheep = 8" << endl;
    
    cout << "Enter your number here: " << endl;
     cin >> input;
    
    int y = 0;
    if (input == 1 ){
      y = 3;
      cout << catNoise << endl;
    }
    else if (input == 2 ){
     y = 4;
    cout << dogNoise << endl;
    }
    else if (input == 3 ){
     y = 2;
     cout << parrotNoise << endl;
    }
    else if (input == 4 ){
     y = 7;
     cout << cowNoise << endl;
    }
    else if (input == 5 ){
     y = 1;
     cout << mouseNoise << endl;
    }
    else if (input == 6 ){
     y = 6;
     cout << dolphinNoise << endl;
    }
    else if (input == 7 ){
     y = 8;
     cout << horseNoise << endl;
    }
    else if (input == 8 ){
     y = 5;
     cout << sheepNoise << endl;
    }
    
    string animal =("");
    
    if (input == 1 )
      animal = ("Cat");
    else if (input == 2 )
     animal = ("Dog");
    else if (input == 3 )
     animal = ("Parrot");
    else if (input == 4 )
     animal = ("Cow");
    else if (input == 5 )
     animal = ("Mouse");
    else if (input == 6 )
     animal = ("Dolphin");
    else if (input == 7 )
     animal = ("Horse");
    else if (input == 8 )
     animal = ("Sheep");
    
    if ( y == 1)
    cout << ("Mouse is not bigger then any animal") << endl;
    else {
    cout << animal << (" is bigger than:") << endl;
    for(int i=0;i<y;i++){
    if (i == 1 )
      cout << ("Mouse ");
    else if (i == 2 )
     cout <<("Parrot ");
    else if (i == 3 )
     cout <<("Cat ");
    else if (i == 4 )
     cout <<("Dog ");
    else if (i == 5 )
     cout <<("Sheep ");
    else if (i == 6 )
     cout <<("Dolphin ");
    else if (i == 7 )
     cout <<("Cow ");
    }
    }
    
    if ( y == 8)
    cout << ("Horse is not smaller then any animal") << endl;
    else {
    cout << endl << animal << " is smaller than:" << endl;
    for(int i=y+1;i<9;i++){
    if (i == 2 )
      cout << ("Parrot ");
    else if (i == 3 )
     cout << ("Cat ");
    else if (i == 4 )
     cout << ("Dog ");
    else if (i == 5 )
     cout << ("Sheep ");
    else if (i == 6 )
     cout << ("Dolphin ");
    else if (i == 7 )
     cout << ("Cow ");
    else if (i == 8 )
     cout << ("Horse ");
    }
    }
    system ("pause");
    return 0;
    }
    Edit: Noticed you are including math.h and string.h and dont really see why, when you do not use any of those in your code.
    Last edited by Itakas; 2012-03-11 at 08:28 PM.

  20. #20
    Well, he seems to use the C++11 features (those lambda syntax looks horrible! now I am double happy that I don't have to use C++ ^^), I don't know whether your compiler supports this out of box. Other then that he uses the Boost libraries (http://www.boost.org/)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •