Totally Stuck

Pages: 12
Sep 18, 2012 at 6:00pm
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?
Sep 18, 2012 at 6:25pm
you can use std::list.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

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.
Sep 19, 2012 at 2:46pm
ok so I got one problem...
when I type Sort.pushback(todo[0][0]); the compiler tells me that STD::list has no member pushback...
how do I fix that?
Last edited on Sep 19, 2012 at 2:51pm
Sep 19, 2012 at 4:47pm
Sep 19, 2012 at 4:53pm
yes i did... and i got the "no matching function for call to..." error message
Sep 19, 2012 at 5:26pm
It's push_back()

There's an underscore there.
Sep 19, 2012 at 8:19pm
that is what i try... both with and without the underscore...
Sep 19, 2012 at 8:40pm
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.
Sep 19, 2012 at 9:09pm
todo is an string type of array
shoudl I use std::list< string > Sort;??
Sep 19, 2012 at 9:24pm
Yes :)
Sep 19, 2012 at 9:49pm
still got the same error...
Sep 19, 2012 at 9:59pm
Could you post all the relevant pieces of your code then please?
Sep 19, 2012 at 10:06pm
ok so this is the main code...
see anything wrong??
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
using namespace std;

string todo[2][3];
string Nombres, Edad;

int main()
{
    std::list<string> Sort;
    Sort.push_backback(todo[0][0]);
    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++;
    }
    if(todo[1][y] > *Sort.end())
   Sort.pushback(todo[1][y]);
Sep 20, 2012 at 4:48am
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:

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
#include <iostream>
#include <list>
#include <string>
using namespace std;

string todo[2][3];
string Nombres, Edad;

int main()
{
    std::list<string> Sort;  //having std:: is not necessary when using namespace std globally
    Sort.push_back(todo[0][0]);
    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++;
    }
    int y=1;
    for(int b=1;b<4;b++){
        cout<<"How old are you?"<<endl<<endl;
        getline(cin,Edad);
        todo[1][y]=Edad;
        cout<<endl;y++;
    }
    if(todo[1][y] > *Sort.end())
        Sort.push_back(todo[1][y]);
    return 0;
}
Last edited on Sep 20, 2012 at 12:55pm
Sep 20, 2012 at 9:35am
Did anyone include the <algorithm> header?

Why do the for loops start at 1?

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.

Hope this gives clues to improve the code.
Last edited on Sep 20, 2012 at 9:43am
Sep 20, 2012 at 12:58pm
Why do the for loops start at 1?


I had not noticed that. Nice observation.

Mario you may want to check out the tutorial on here about arrays:
http://cplusplus.com/doc/tutorial/arrays/
Sep 20, 2012 at 1:39pm
Mario you may want to check out the tutorial on here about arrays:
http://cplusplus.com/doc/tutorial/arrays/


i think he knew it, this is just a logic error...

Why do the for loops start at 1?

didn't noticed it too... this is so much shameful...
Sep 20, 2012 at 3:09pm
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::

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 <stdio.h>
#include <fstream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <conio.h>
#include <list>
#include <algorithm>

using namespace std;

string todo[2][3];
string Nombres, Edad;
int b=1;
int x=1;
int main()
{
    list<string> Sort;
    Sort.push_back(todo[0][0]);
    for(int a=0;a<3;a++){
        cout<<"What is your name?"<<endl<<endl;
        getline(cin,Nombres);
        todo[0][x]=Nombres;
        cout<<endl;x++;
    }
    for(int b=0;b<3;b++){
        cout<<"How old are you?"<<endl<<endl;
        getline(cin,Edad);
        todo[1][b]=Edad;
        cout<<endl;b++;
    }
    if(todo[1][b] > *Sort.end())
   Sort.push_back(todo[1][b]);

    return 0;
}

PS. I use all of those #include because I will need them later.... as weird as it may sound...
Sep 20, 2012 at 3:24pm
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...)
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 <stdio.h>
#include <fstream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <conio.h>
#include <list>
#include <algorithm>

using namespace std;

string todo[2][3];
string Nombres, Edad;
int c=1;
int x=1;
int main()
{
    list<string> Sort;
    Sort.push_back(todo[0][0]);
    for(int a=0;a<3;a++){
        cout<<"What is your name?"<<endl<<endl;
        getline(cin,Nombres);
        todo[0][x]=Nombres;
        cout<<endl;x++;
    }
    for(int b=0;b<3;b++){
        cout<<"How old are you?"<<endl<<endl;
        getline(cin,Edad);
        todo[1][c]=Edad;
        cout<<endl;c++;
    }
    if(todo[1][c] > *Sort.end())
   Sort.push_back(todo[1][c]);
   cout<< "The oldest is" <<*Sort.end()<<endl;
    return 0;
}
Sep 20, 2012 at 3:24pm
//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.
Last edited on Sep 20, 2012 at 3:26pm
Pages: 12