Hi I have some trouble finding what to do... This is my initial code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
{
for(int a=1;a<4;a++){
cout<<"What is your name?"<<endl<<endl;
getline(cin,Nombres);
int x=1;
todo[0][x]=Nombres;
cout<<endl;x++;
}
for(int b=1;b<4;b++){
cout<<"How old are you?"<<endl<<endl;
getline(cin,Edad);
int y=1;
todo[1][y]=Edad;
cout<<endl;y++;
}
what I want to do is to compare Y1, Y2 and Y3 and let the program get the bigger one.
how can I do that?
Do I use a if statement... or what do I use?
std::list<int> Sort;
//load the first entry
Sort.pushback(todo[0][0]);
//loop through adding elements to the array,
//pushing them to the back if they are greater then the last entry
for(int x = 1;x<4; x++)
{
for(int y = 1; y<4;y++)
{
if(todo[x][y] > *Sort.end())
Sort.pushback(todo[x][y]);
}
}
//then get the last element, which is the largest.
cout<<The oldest is "<<*Sort.end()<<endl;
some things to note about the syntax:
std::list.end() returns an iterator to the last element in the list, so to access the item you must dereference the iterator, thus:
*Sort.end(); //dereference the return value of std::list.end()
consider using the variables that you declared in the for loop (a, and b) instead of the locally declared variables of x and y.
What type of array is todo?
I'm gonna assume its a char***?
You need to make the list with type char* since todo[i][j] represents a char*
So instead of std::list< int > Sort; try std::list< char* > Sort;
The reason there is no matching function call is cause the new element must be the same type that the list is holding.
I just had to fix push_backback to push_back
and pushback to push_back
It also appeared that y may be declared in the wrong scope? Or lines 24 and 25 in your post belong in the same scope as where y is declared?
Here is what I did:
C:\Users\Kevin\Desktop\cplusplus.com\Totally Stuck>g++ pushback.cpp
pushback.cpp: In function 'int main()':
pushback.cpp:12:10: error: 'class std::list<std::basic_string<char> >' has no member named 'push_back
back'
pushback.cpp:27:16: error: 'y' was not declared in this scope
pushback.cpp:28:9: error: 'class std::list<std::basic_string<char> >' has no member named 'pushback'
C:\Users\Kevin\Desktop\cplusplus.com\Totally Stuck>g++ pushback.cpp
C:\Users\Kevin\Desktop\cplusplus.com\Totally Stuck>
Here is the code with those three changes I made and it does compile:
With the for loops, why not make use of variable b, instead of using y.
Both of the for loops should be combined together. At the moment, the question "What is your name?" is asked 3 times in succession, and so is the age question.
thanks everyone I just notice the backback thing... and obiously I am ashamed for that, as well as for the for loop...
already change the variables to globally instead of locally...
as well as the pushback underscore...
the
//having std:: is not necessary when using namespace std globally
will save me a lot of time in the future... thanks
again Thanks every one
Here is the already working code if you guys want to check it out for possible non-harming mistakes that can save beginners like me a lot of time... like the std::
I just got one new problem...
when I run the code...
and enter the ages...
as soon as it is supposed to tell me who is the oldest...
I got a bunch of words... and characters...
if this were to be a terror movie... it would be at this time when everyone starts to die...
here is the code once again.. (made some small changes...)
//having std:: is not necessary when using namespace std globally
will save me a lot of time in the future... thanks
It's not really good practice to use namespaces globally. Beats the whole purpose of having namespaces, actually. Can lead to naming clashes in bigger projects.
PS. I use all of those #include because I will need them later.... as weird as it may sound...
Why would you need stdio.h AND iostream? You also have fstream included twice. Also don't see the need of conio.h in a c++ program.