-interesting- have i over complicated a task is this route gonna work?

this is the practice challenge i have been trying to finish...


Write a program that asks the user to enter the number of pancakes eaten for breakfast by 10 different people (Person 1, Person 2, ..., Person 10)
Once the data has been entered the program must analyze the data and output which person ate the most pancakes for breakfast.


i think i keep over complicating it, i cant find a finished version,my attempts to use "if else" statements failed and were way too big,

i could get the highest number with arrays i just couldnt get the name associated with the number so this is the beginning of an attempt to have two different arrays have the same index numbers so it seems they correlate

but now how could i retrieve the data and manipulate it? i was fiddling with a function in the class trying to get the memory addresses for each index number but i confused myself, i cant help feeling there is a really simple way of doing this, the challenge stated what knowledge would be required and it never mentioned class

can you tell me why the cout in the getcake function in class gave me the addresses of name and pancake? easily rectified but never understood







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

using namespace std;

class fatty {
public:
int i;
string name [8] ;
int pancakes [8] ;
void getcake ()
{
cout << "the fat bastard called "<< *name <<" ate " << *pancakes << "pancakes!!"<< endl;
}
void eatcake ()
{
cout << "whats the fatties name?" << endl;
cin >> name [0];
name [i++];
cout << "how many cakes has he had? " << endl;
cin >> pancakes [0];
1+pancakes [0];

}

};


int main()

{
int panholder = 0;
string nameholder;
int useful3;


fatty fat1;
fat1.eatcake ();
fatty fat2;
fat2.eatcake ();
fatty fat3;
fat3.eatcake ();
fatty fat4;
fat4.eatcake ();
fatty fat5;
fat5.eatcake ();
fatty fat6;
fat6.eatcake ();
fatty fat7;
fat7.eatcake ();
fatty fat8;
fat8.eatcake ();



return 0;



}
Please use code tags - the <> button on the right.

You need an array of fatty not arrays within fatty. Then a loop to go through the array to decide who has eaten the most.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

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

using namespace std;

class fatty {
public:
int i;
string name [8] ;
int pancakes [8] ;
void getcake ()
{
cout << "the fat bastard called "<< *name <<" ate " << *pancakes << "pancakes!!"<< endl;
}
void eatcake ()
{
cout << "whats the fatties name?" << endl;
cin >> name [0];
name [i++];
cout << "how many cakes has he had? " << endl;
cin >> pancakes [0];
1+pancakes [0];

}

};


int main()

{
int panholder = 0;
string nameholder;
int useful3;


fatty fat1;
fat1.eatcake ();
fatty fat2;
fat2.eatcake ();
fatty fat3;
fat3.eatcake ();
fatty fat4;
fat4.eatcake ();
fatty fat5;
fat5.eatcake ();
fatty fat6;
fat6.eatcake ();
fatty fat7;
fat7.eatcake ();
fatty fat8;
fat8.eatcake ();



return 0;



}

Great - you have discovered code tags. Now change the code with my (and others) earlier suggestions.

Just a point - don't create a new thread for the same topic. The problem is people might write heaps into one thread, only to discover someone else has written heaps into the other one.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class fatty {
public:
    int i;
    string name [8] ;
    int pancakes [8] ;

    void getcake ()
    {
        cout << "the fat bastard called "<< *name <<" ate " 
          << *pancakes << "pancakes!!"<< endl;
    }

    void eatcake ()
    {
        cout << "whats the fatties name?" << endl;
        cin >> name [0];
        name [i++];
        cout << "how many cakes has he had? " << endl;
        cin >> pancakes [0];
        1+pancakes [0];
    }

};



Please use code tags - that's the <> button on the right.

On lines 4 and 5 of your class, you specify that each individual fatty object will have no less than eight different names, and eight separate counts of pancakes eaten.

More than likely that is not what was intended, as function eatcake only stores a value in the first occurrence. (By the way, lines 17 and 20 don't really do anything useful).

If we keep to the same class design for now, it follows that function getcake should also access the first occurrence of each value too, like this:

cout << "the fat bastard called "<< name[0] << " ate " << pancakes[0] << " pancakes!!"<< endl;

But still, a more sensible solution is to get rid of all those arrays.

On the other hand, your main() function could certainly use an array, something like this:

1
2
3
4
5
6
    fatty fatties[8];

    for (int i=0; i< 8; ++i )
    {
        fatties[i].eatcake();
    }
ahh thank you...the amazing chervil strikes again...there should be sum kinda point system like yahoo...id make you rich :)
oh yeah i was also worried about writing too much into the new topic section, after all i dont know how much of a new topic a completley different approach to the same end is, although i would have got warmer and warmer till i dun it...only the other attempt i just made and wrote on a new thread is completley different and just as hopeless :(
There is certainly a benefit in keeping everything in the same thread. Otherwise you may have replies which though valid in themselves, are recommending you do completely different things.

Pooling our collective ideas helps us all to focus on getting to the final goal.
someones goona have to show me the basic outline of an array of objects and calling their values i think heres something i failed earlier

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <string>

using namespace std;

class fatty{
public:
int name;
int pancake;

};
int main()

{

    int highcake = 0;
    string namebox;
    fatty fat [8];
    for (int q =0; q > 8; q++)
    {
        fatty fat [q];
        cout << "enter fatties name "<<endl;
        cin >> fat[q].name;
        cout << "how many pancakes has the fat bastard eaten?" << endl;
        cin >> fat[q].pancake;
        if (fat [q]pancake < highcake)
         {
         highcake = fat[q].pancake;
         namebox = fat [q].name;
        }
    }

cout << "fat bastard called " << namebox << "ate the most cake ( " << highcake << " pancakes )" <<endl;



return 0;
}
Switch the ">" with "<" on line 19 and you should be all set. Also, you need a "." after "]" on line 26

You can also do something like this:
1
2
3
4
5
6
7
8
9
10
int highest = 0;

//...

if (fat[q].pancake > fat[highest].pancake)
  highest = q;

//...

cout << "fat bastard called " << fat[highest] //... 
Last edited on
@LowestOne That's an interesting idea.

Just to clarify the meaning "highest" here is not the biggest number. Instead it is the index or subscript of the array item which has the highest value.


Changing topics now. Overall there is a different consideration. I don't know the precise requirements for this task. If we need to print out a list of all the items in the array, or do something which needs them all, then we definitely need an array or something similar.

But - if the only requirement is to print out a message at the end with the name and quantity eaten of just the one who consumed the most, then an array may not be required after all.

Apologies if I missed something as there have been several posts and I don't think I read all of them.

Edit From the opening post in this thread:
Once the data has been entered the program must analyze the data


That implies the data is stored (step 1) and then analysed (step 2). I was thinking of analysing it at the time it was read in (do everything in step 1). I'm probably wrong...
Last edited on
you know my code still dont work even with the tweaks (what lowest one said) ...i imagine it would be very hard to identify why it only asks for one name and no data
Last edited on
this code gets through the build but crashes!

i regret now starting several different posts am trying to catch you guys as much as possible...but then a learning process is a wierd and convoluted thing

i have learnt more than i though i would had the code worked i guess, although i have many un answered questions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>

using namespace std;

class fatty{
public:
string name;
int pancake;

};
int main()

{

    int highcake = 0;
    string namebox;
    fatty fat [8];
    fat[0].name = "elvis";
    fat [1].name = "yo momma";
    fat [2].name = "dr strangelove";
    fat [3].name = "bubbah";
    fat [4].name = "kobiyoshi";
    fat [5].name = "santa";
    fat [6].name = "cartman";
    fat [7].name = "kublah kahn";
    
    for (int q =0; q < 8; q++)
    {
        fatty fat[q];
        cout << "how many pancakes has "<< fat[q].name <<  "  eaten?" << endl;
        cin >> fat[q].pancake;
       if (fat[q].pancake > fat[highcake].pancake)
        {
            highcake = q;
        }
    }

    cout << "fat bastard called " << fat[highcake].name << " ate the most cake ( " << fat[highcake].pancake << " pancakes )" <<endl;




return 0;
}
I DID IT XD

thanks everyone...guess i didnt do it all by myself...the thing making it crash in my last code crash was line 30! i didnt need to declare it and so it confused everything :D

one last thing though line 39 how does this work??

( " << fat[highcake].pancake << " pancakes )
Topic archived. No new replies allowed.