I read it. It only tell how to get a value from an array. But all I want to do it is store the values in an array and then use them in the list below it ( Referring to my program )
Try this, hopefully this will give you a little more clarity. If you have any questions about the code just send me a message. However, your comment "Please just fix the program. They way it is supposed to be" I would think would put people off helping you.
The guys on here were doing it the right way and merely pointing you in the the right direction so that you would discover your mistakes, and hopefully by discovering them yourself you would learn from it.
If people simply code for you, you will learn nothing.
Anyway.. here is the code, hope you learn from it.
#include <iostream>
usingnamespace std;
constint NUM_NAMES = 5; // here for simplicity I am assuming your storing 5 names and how many pancakes they ate.
// structure for our array
struct users
{
char Name[50]; // max 50 characters for name.
int Pancakes; // number he/she ate
}; struct users Names[NUM_NAMES];
int main()
{
// We will loop 5 times updating the index of our array so that
// each name is stored in the correct position.
for (int count = 0; count < NUM_NAMES; count++) {
cout << "Enter Name (" << count + 1 << "): ";
cin >> Names[count].Name;
cout << "How many pancakes for " << Names[count].Name << "? ";
cin >> Names[count].Pancakes;
}
// we now have the names and pancakes stored, lets show them.
for (int count = 0; count < NUM_NAMES; count++) {
cout << "Name: " << Names[count].Name << " has eaten " << Names[count].Pancakes << "." << endl;
}
return 0;
}
I think you may have hijacked some other question with yours :)
Anyway, a structure is a way of grouping data elements "members" under one tidy name (i.e. arrays, integers etc).
I have just looked at the structures section you referred to and its very well written and explains structures well - for that reason I am not going to repeat any of it here.
Its normal that it sometimes doesn't become clear when reading it so I would suggest reading it over and over until you do - try some examples in a IDE and experiment.
Softrix what are you doing? Using a char array to get a string and what is struct user Names[5];
try this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//#includes
int main ()
{
int pancakes [5]; //create an array that can store 5 pancakes (0 - 4)
int person = 1; //initialize your variables, variable for person
for (person; person < 5; ++person)
{
cout << "How many pancakes did person" << person << " eat: ";
cin >> pancakes [person]; //the [person] increases by 1 to store pancakes data everytime the loop runs
}
for (person; person < 5; ++person)
{
cout << "Person: " << person << "ate " << pancakes [person] << " pancake(s) "; //access all values stored in pancakes with [person] and print it
}
return 0;
}
lol, I am wondering that myself after reading your reply and looking at the original post... for some odd reason (who knows why) I had it in my head he wanted to store names alongside the number of pancakes that person ate... its clearly one of those days....
oh well he got more that what he asked for.. cant complain at that.. now i need a lie down ^^
Nathan2222: You program really helped me. So thank you so much for that.
And thank you everyone else too.
Can't believe I finally made it. I've been trying to make this program for a week now and I was getting really frustrated. I feel like I've won an Oscar ! :D
lol mine was a little over the top from what you wanted because for some reason I had it in my head you wanted to store names and the number of pancakes they ate... but obviously not and realised once Nathan pointed it out lol.
I'm sure my code will come in handy for something as you work on new projects.
I tried your program too but I was a lil complex for a beginner like me :)
I actually started doing the practice exercises on the site. Which is really helping me alot. I read it first. At first I try to make it myself, then watch the videos on youtube on the related topic and even then if I couldn't understand I post it here.
Am I going on the right path? Please suggest what else shoudl i do to make my coding better. Will really appreciate it :)
Read lots and lots of tutorials :). And yeah you are going on the right path I guess. You might want to start a project of your own though soon. Try to apply whatever you have learnt.
You are going on the right path and I had way more serious problems with arrays and for loops.
I started 4 months ago and i'm almost done with 3 books. Just make sure you finish the books and practice, practice, practice (don't forget to have fun).
My plan is once i finish the books (maybe by the end of this month), i make a huge program that uses all i've learnt and ask for code reviews.
I also started I guess 2 months ago. But I've been quite lazy and because of that I forget stuff. And then I have to watch the videos again.
Btw. I'm not learning from a book. Should I? Because I find it easier to learn from videos.
In this same program I'm also required to finf the max and min number of pancakes eaten by a person. I was able to make it but the code is not giving the correct value of max. Min is coming out just fine.
#include <iostream>
usingnamespace std;
int main()
{
int person;
int pancakes[11];
int MAX;
int MIN;
for (person =1; person <= 10; person++)
{
cout << "Enter the number of pancakes eaten by person " << person << endl;
cin >> pancakes [person];
}
cout << "Person - Pancakes\n";
for (person=1 ;person <= 10; person++)
{
cout << person << " - "<< pancakes [person] << endl;
}
MAX = pancakes[0];
MIN = pancakes[0];
for (person =1; person <= 10; person++)
{
if (pancakes[1] > MAX)
{
MAX = pancakes[1];
}
}
for (person =1; person <= 10; person++)
{
if (pancakes[1] < MIN)
{
MIN = pancakes[1];
}
}
cout << "Maximum number of pancakes eaten by a person are "<< MAX << endl;
cout << "Maximum number of pancakes eaten by a person are "<< MIN << endl;
return 0;
}
// #includes
int main()
{
int pancakes [10];
int person = 1;
int min = 0;
int max = 0;
for (person; person < 10; ++person)
{
cout << "Person " << person << " ate how many pancakes: ";
cin >> pancakes [person];
min = pancakes [0]; // min is equal to the no. of pancakes eaten by the 1st person
max = pancakes [0]; // max is equal to the no. of pancakes eaten by the 1st person
for (int i = 0; i < 10; ++i)
{
if (max < pancakes[i]) // if the no. of pancakes eaten by the 1st person (max) is less than the no. of pancakes eaten by the next person . .
{
max = pancakes [i]; // . . . max is equal to that higher no.
}
elseif (min > pancakes[i]) // else if the no. of pancakes eaten by the 1st person (min) is greater than the no. of pancakes eaten by next person . . .
{
min = pancakes [i]; // . . . min is equal to that lower no.
}
}
}
for (person; person < 10; ++person)
{
cout << "Person " << person << " ate " << pancakes [person] << " pancakes" << endl;
}
cout << "Maximum number of pancakes eaten is: " << max << endl;
cout << "Minimum number of pancakes eaten is: " << min;
return 0;
}
Is it ok?
I prefer books because i don't like pausing/playing when i'm learning except it's 3d modelling.
#include <iostream>
usingnamespace std;
int main()
{
int person;
int pancakes[11];
int MAX;
int MIN;
for (person=1; person <= 10; person++)
{
cout << "Enter the number of pancakes eaten by person " << person << endl;
cin >> pancakes [person];
}
cout << "Person - Pancakes\n";
for (person=1;person <= 10; person++)
{
cout << person << " - "<< pancakes [person] << endl;
}
MAX = pancakes[0];
MIN = pancakes[0];
for (int i=1; i <= 10; i++)
{
if (pancakes[i] > MAX)
{
MAX = pancakes[i];
}
elseif (pancakes[i] < MIN)
{
MIN = pancakes[i];
}
}
cout << "Maximum number of pancakes eaten by a person are "<< MAX << endl;
cout << "Minimum number of pancakes eaten by a person are "<< MIN << endl;
return 0;
}
That's weird because i just ran it.
I realised in the code i posted, person = 1 but int i = 0 so it doesn't check it correctly.
Does my code show a large negative number?