C++ difficulty

Pages: 123
Jan 11, 2013 at 7:46pm
Ah so it looks like my only guess was sort of correct. Sometimes the backwards compatibility with C seems a little ridiculous...
Jan 12, 2013 at 1:03am
Hi this is my code but it`s showing still some erorrs.

What is wrong with my program?

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <string>;
#include <iostream>;

using namespace std;



struct event_t {

    event_t( std::string const &title, unsigned const tickets, unsigned const tickets_available, event_t *next = nullptr)
    :   title( title ),
        tickets( tickets ),
        tickets_available( tickets_available ),
        next( next )
    {}

    bool is_sellout() const { return tickets_available == 0; }

    std::string title;
    unsigned tickets;
    unsigned tickets_available;

    event_t *next;
};

std::ostream & operator<<( std::ostream & os, event_t const &event )
{
    os << "Event \"" << event.title << "\"\nTickets:\t" << event.tickets << "\nTickets available:\t" << event.tickets_available;
    return os;
}



void print_event( event_t const & event )
{
    std::cout << event << '\n';
}



void print_event_list( event_t const & first_event )
{
    event_t const *current = &first_event;

    print_event( *current );
    return current->next ? print_event_list( *current->next ) : 0;
}



void print_event_list_sellout( event_t const & first_event )
{
    event_t const *current = &first_event;

    if( current->is_sellout() ) {

        print_event( *current );
    }

    return current->next ? print_event_list_sellout( *current->next ) : 0;
}



int main()
{
    event_t event_four( "foobarbaz", 2000, 2000 );
    event_t event_three( "foobar", 1000, 0, &event_four );
    event_t event_two( "bar", 5000, 4500, &event_three );
    event_t event_one( "foo", 10000, 0, &event_two );

    std::cout << "event_two:\n";
    print_event( event_two );

    std::cout << "\nEvent list:\n";
    print_event_list( event_one );

    std::cout << "\nEvent list sellout:\n";
    print_event_list_sellout( event_one );
}


Can please someone correct my programm so that it works?

Jan 12, 2013 at 5:27am
Hi this is my code but it`s showing still some erorrs.

What is wrong with my program?


Should we break out the crystal ball so we can figure out what kind of errors you're getting? Post relevant information.

"Hey! My program doesn't work! Can you fix it?"

Probably. But, will someone take the time if you can't take the time to say what's wrong?
Jan 12, 2013 at 9:40am
C:\CodeBlocks\12\main.cpp|16|warning: identifier 'nullptr' is a keyword in C++11 [-Wc++0x-compat]|
C:\CodeBlocks\12\main.cpp|16|error: 'nullptr' was not declared in this scope|
C:\CodeBlocks\12\main.cpp||In function 'void print_event_list(const event_t&)':|
C:\CodeBlocks\12\main.cpp|56|error: second operand to the conditional operator is of type 'void', but the third operand is neither a throw-expression nor of type 'void'|
C:\CodeBlocks\12\main.cpp|56|error: return-statement with a value, in function returning 'void' [-fpermissive]|
C:\CodeBlocks\12\main.cpp||In function 'void print_event_list_sellout(const event_t&)':|
C:\CodeBlocks\12\main.cpp|72|error: second operand to the conditional operator is of type 'void', but the third operand is neither a throw-expression nor of type 'void'|
C:\CodeBlocks\12\main.cpp|72|error: return-statement with a value, in function returning 'void' [-fpermissive]|
C:\CodeBlocks\12\main.cpp||In function 'int main()':|
C:\CodeBlocks\12\main.cpp|80|error: call to 'event_t::event_t(const string&, unsigned int, unsigned int, event_t*)' uses the default argument for parameter 4, which is not yet defined|
||=== Build finished: 6 errors, 1 warnings (0 minutes, 0 seconds) ===|


That is the list of errors .

I would be glad if someone could help me.
Jan 12, 2013 at 2:27pm
Has somebody an idea how I can correct my Code?
Jan 12, 2013 at 3:05pm
The line numbers are off, ¿is so hard to copy-paste?

> warning: identifier 'nullptr' is a keyword in C++11 [-Wc++0x-compat]|
Compile with c++11 support if you plan to use it. By instance `-std=c++11'

> In function 'void print_event_list(const event_t&)':
> In function 'void print_event_list_sellout(const event_t&)':
>> error: return-statement with a value, in function returning 'void' [-fpermissive]|
You promise that you wouldn't return anything.
Jan 12, 2013 at 3:42pm
Can somebody correct my Code so that it Works ?
Jan 12, 2013 at 10:10pm
Yea you can.
Jan 12, 2013 at 10:15pm
I posted my Code but I am Not Finding the mistakes . That s why I am asking .


Is here any good Programmer who can correct my mistakes . Because I have just started Programming.
Jan 13, 2013 at 1:05pm
Hello can someone please help me I am stucking with this task for some days now.

Or has somebody any idea where the mistakes are than I can probaly try tosolve them.

Please help me
Jan 13, 2013 at 4:14pm
Hello all experts, I corrected a little the code but the programm is still not working.

Where is the problem?

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <string>
#include <iostream>


struct event_t {

    event_t( std::string const &title, unsigned const tickets, unsigned const tickets_available, event_t *next = nullptr)
    :   title( title ),
        tickets( tickets ),
        tickets_available( tickets_available ),
        next( next )
    {}

    bool is_sellout() const { return tickets_available == 0; }

    std::string title;
    unsigned tickets;
    unsigned tickets_available;

    event_t *next;
};

std::ostream & operator<<( std::ostream & os, event_t const &event )
{
    os << "Event \"" << event.title << "\"\nTickets:\t" << event.tickets << "\nTickets available:\t" << event.tickets_available;
    return os;
}



void print_event( event_t const & event )
{
    std::cout << event << '\n';
}



void print_event_list( event_t const & first_event )
{
    event_t const *current = &first_event;

    print_event( *current );
    if current->next ? print_event_list( *current->next );
    else return;
}


void print_event_list_sellout( event_t const & first_event )
{
    event_t const *current = &first_event;

    if( current->is_sellout() ) {

        print_event( *current );
    }

    if current->next ? print_event_list( *current->next );
    else return;
}



int main()
{
    event_t event_four( "foobarbaz", 2000, 2000 );
    event_t event_three( "foobar", 1000, 0, &event_four );
    event_t event_two( "bar", 5000, 4500, &event_three );
    event_t event_one( "foo", 10000, 0, &event_two );

    std::cout << "event_two:\n";
    print_event( event_two );

    std::cout << "\nEvent list:\n";
    print_event_list( event_one );

    std::cout << "\nEvent list sellout:\n";
    print_event_list_sellout( event_one );
}
Last edited on Jan 13, 2013 at 4:15pm
Jan 13, 2013 at 4:29pm
closed account (zb0S216C)
Those "if" statements on lines 43 and 57 contain incomplete ternary statements. Besides, those ternaries are not needed. It should be:

1
2
3
4
if( current->next )
    print_event_list( *current->next );

// No need for an "else" branch as the function will return anyway. 

In the future, posting a wall of code with a corresponding "Where is the problem?" will only get you ignored.

Wazzak
Last edited on Jan 13, 2013 at 4:30pm
Jan 13, 2013 at 5:21pm
Hello Framework thank you . But is this the only mistake in my Code?

Can you please also Tell me Wehre the Otter mistakes are?

Because otherwise I will Never be finnisches with this task .
This task is making me Crazy.
Last edited on Jan 13, 2013 at 6:02pm
Jan 13, 2013 at 6:05pm
Oh hello all experts .

My Programm is showing me only one error.

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <string>
#include <iostream>


struct event_t {

    event_t( std::string const &title, unsigned const tickets, unsigned const tickets_available, event_t *next = nullptr)
    :   title( title ),
        tickets( tickets ),
        tickets_available( tickets_available ),
        next( next )
    {}

    bool is_sellout() const { return tickets_available == 0; }

    std::string title;
    unsigned tickets;
    unsigned tickets_available;

    event_t *next;
};

std::ostream & operator<<( std::ostream & os, event_t const &event )
{
    os << "Event \"" << event.title << "\"\nTickets:\t" << event.tickets << "\nTickets available:\t" << event.tickets_available;
    return os;
}



void print_event( event_t const & event )
{
    std::cout << event << '\n';
}



void print_event_list( event_t const & first_event )
{
    event_t const *current = &first_event;

    print_event( *current );
    if (current->next) print_event_list( *current->next );

}



void print_event_list_sellout( event_t const & first_event )
{
    event_t const *current = &first_event;

    if( current->is_sellout() ) {

        print_event( *current );
    }

    if (current->next) print_event_list( *current->next );

}



int main()
{
    event_t event_four( "foobarbaz", 2000, 2000 );
    event_t event_three( "foobar", 1000, 0, &event_four );
    event_t event_two( "bar", 5000, 4500, &event_three );
    event_t event_one( "foo", 10000, 0, &event_two );

    std::cout << "event_two:\n";
    print_event( event_two );

    std::cout << "\nEvent list:\n";
    print_event_list( event_one );

    std::cout << "\nEvent list sellout:\n";
    print_event_list_sellout( event_one );
}


My ide is showing me an error on line 7.

Thar are the errors.

Can please someone help me?
C:\CodeBlocks\12\main.cpp|16|warning: identifier 'nullptr' is a keyword in C++11 [-Wc++0x-compat]|
C:\CodeBlocks\12\main.cpp|16|error: 'nullptr' was not declared in this scope|
C:\CodeBlocks\12\main.cpp||In function 'int main()':|
C:\CodeBlocks\12\main.cpp|82|error: call to 'event_t::event_t(const string&, unsigned int, unsigned int, event_t*)' uses the default argument for parameter 4, which is not yet defined|
||=== Build finished: 2 errors, 1 warnings (0 minutes, 0 seconds) ===|

Last edited on Jan 13, 2013 at 6:08pm
Jan 13, 2013 at 6:19pm
ne555 wrote:
> warning: identifier 'nullptr' is a keyword in C++11 [-Wc++0x-compat]|
Compile with c++11 support if you plan to use it. By instance `-std=c++11'


From a previous post which you didn't pay attention to.
Jan 13, 2013 at 6:23pm
How should I write this thing in another way that it works?


Sorry I have no idea.

Jan 13, 2013 at 6:31pm
event_t * next = 0
Jan 13, 2013 at 6:37pm
closed account (zb0S216C)
Or, for the future:

1
2
3
4
5
6
7
8
9
10
11
12
13
// From: http://www.stroustrup.com/C++11FAQ.html#11
// In C++11 the macro __cplusplus will be set to a value that differs from (is greater than)
// the current 199711L.
#if( __cplusplus > 199711L )
    #define NULL nullptr
#else
    #define NULL 0
#endif

int main( )
{
    int *Pointer_( NULL ); // 0 if C++11 is not enabled; "nullptr" if it is enabled.
}

Wazzak
Jan 13, 2013 at 6:40pm
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <string>
#include <iostream>



struct event_t {

    event_t( std::string const &title, unsigned const tickets, unsigned const tickets_available, event_t * next = 0)
      : title( title ),
        tickets( tickets ),
        tickets_available( tickets_available ),
        next( next )
    {}

    bool is_sellout() const { return tickets_available == 0; }

    std::string title;
    unsigned tickets;
    unsigned tickets_available;

    event_t *next;
};

std::ostream & operator<<( std::ostream & os, event_t const &event )
{
    os << "Event \"" << event.title << "\"\nTickets:\t" << event.tickets << "\nTickets available:\t" << event.tickets_available;
    return os;
}



void print_event( event_t const & event )
{
    std::cout << event << '\n';
}



void print_event_list( event_t const & first_event )
{
    event_t const *current = &first_event;

    print_event( *current );
    if (current->next) print_event_list( *current->next );

}



void print_event_list_sellout( event_t const & first_event )
{
    event_t const *current = &first_event;

    if( current->is_sellout() ) {

        print_event( *current );
    }

    if (current->next) print_event_list( *current->next );

}



int main()
{
    event_t event_four( "foobarbaz", 2000, 2000 );
    event_t event_three( "foobar", 1000, 0, &event_four );
    event_t event_two( "bar", 5000, 4500, &event_three );
    event_t event_one( "foo", 10000, 0, &event_two );

    std::cout << "event_two:\n";
    print_event( event_two );

    std::cout << "\nEvent list:\n";
    print_event_list( event_one );

    std::cout << "\nEvent list sellout:\n";
    print_event_list_sellout( event_one );
}




Oh thank you very much cire . I posted you my code .

Its working now.

Only a such a small mistake with such a effect.

Cire have I now solved my task rightly or are there some mistakes .

Only that I can be sure.


Last edited on Jan 13, 2013 at 6:41pm
Jan 13, 2013 at 6:47pm
haha this thread was pretty funny
Pages: 123