Why is my code showing this error?

Discussion in 'C++' started by eli m, Mar 5, 2013.

  1. eli m

    eli m Guest

    My compiler is showing this error:

    Run-Time Check Failure #3 - The variable 'math' is being used without being initialized.

    Here is my code:

    // CMD Application
    // Created by Eli. M

    #include <iostream>
    using namespace std;

    int main ()
    {
    char function;
    char math = math;
    char mathop;
    cout << "Type in help for a list of functions";
    cout << "Type in a function";
    cin >> function;
    if (function == math)
    {
    cout << "What math operation do you want to use?";
    cin >> mathop;
    }

    }

    What am i doing wrong?
    eli m, Mar 5, 2013
    #1
    1. Advertising

  2. eli m

    Geoff Guest

    On Mon, 4 Mar 2013 21:03:06 -0800 (PST), eli m <>
    wrote:

    >My compiler is showing this error:
    >
    >Run-Time Check Failure #3 - The variable 'math' is being used without being initialized.
    >
    >Here is my code:
    >
    >// CMD Application
    >// Created by Eli. M
    >
    >#include <iostream>
    >using namespace std;
    >
    >int main ()
    >{
    > char function;
    > char math = math;


    You forgot quotes around the string "math". If you are going to
    initialize a string like this, you should specify these variables as
    C++ string objects instead of C's char objects.

    > char mathop;
    > cout << "Type in help for a list of functions";
    > cout << "Type in a function";
    > cin >> function;
    > if (function == math)


    This will fail, you can't compare strings this way.

    > {
    > cout << "What math operation do you want to use?";
    > cin >> mathop;
    > }
    >
    >}
    >
    >What am i doing wrong?
    Geoff, Mar 5, 2013
    #2
    1. Advertising

  3. eli m

    Öö Tiib Guest

    On Tuesday, 5 March 2013 07:03:06 UTC+2, eli m wrote:
    > My compiler is showing this error:
    >
    > Run-Time Check Failure #3 - The variable 'math' is being used without being initialized.
    >
    > Here is my code:
    >
    > // CMD Application
    > // Created by Eli. M
    >
    > #include <iostream>
    > using namespace std;


    See
    http://www.parashift.com/c -faq/using-namespace-std.html

    > int main ()
    > {
    > char function;


    You probably wanted std::string not char here

    > char math = math;


    That is something you probably did not think through clearly what you want.

    > char mathop;


    Again string most likely.

    > cout << "Type in help for a list of functions";
    > cout << "Type in a function";
    > cin >> function;
    > if (function == math)


    Here you likely wanted "math" not math.

    > {
    > cout << "What math operation do you want to use?";
    > cin >> mathop;
    > }
    >
    > }
    >
    > What am i doing wrong?


    You are applying woo-doo programming system to learning C++.
    You type something in and see what happens. Better pick a book
    or tutorial and try lessons or examples from it.
    Öö Tiib, Mar 5, 2013
    #3
  4. eli m

    Geoff Guest

    On Tue, 5 Mar 2013 07:42:08 +0000 (UTC), Juha Nieminen
    <> wrote:

    >eli m <> wrote:
    >> using namespace std;

    >
    >Why does this always creep up into beginners' code? Where do they learn
    >this stuff?
    >
    >--- news://freenews.netfront.net/ - complaints: ---


    it sows up because lazy programmers coding snippets and samples always
    seem to start with it. Cargo cult.
    Geoff, Mar 5, 2013
    #4
  5. eli m

    James Kanze Guest

    On Tuesday, 5 March 2013 07:42:08 UTC, Juha Nieminen wrote:
    > eli m <> wrote:


    > > using namespace std;


    > Why does this always creep up into beginners' code? Where do they learn
    > this stuff?


    The same place they learn `while ( !cin.eof() )`? There are two
    or three such things which seem to continually pop up. (And
    I too wonder where they are learning it.)

    --
    James
    James Kanze, Mar 5, 2013
    #5
  6. eli m

    Bill Gill Guest

    On 3/5/2013 7:49 AM, Geoff wrote:
    > On Tue, 5 Mar 2013 07:42:08 +0000 (UTC), Juha Nieminen
    > <> wrote:
    >
    >> eli m <> wrote:
    >>> using namespace std;

    >>
    >> Why does this always creep up into beginners' code? Where do they learn
    >> this stuff?
    >>
    >> --- news://freenews.netfront.net/ - complaints: ---

    >
    > it sows up because lazy programmers coding snippets and samples always
    > seem to start with it. Cargo cult.
    >

    It shows up because many C++ books show it that way. Some of them
    show it as "using namespace std; and then in the same book show it
    as "std::cin;". In fact "Programming Principles and Practice" by
    Bjarne Stroustrup uses "using namespace" frequently. I think
    that Stroustrup should know something about C++.

    So my question: What specifically is wrong with "using namespace"?
    Both ways work and in fact it appears that "using namespace" would
    be a bit more efficient for the programmer. If you go with "std::cin"
    formation you save 5 key strokes each time you use a standard
    function.

    Now let the flame war begin.

    Bill
    Bill Gill, Mar 5, 2013
    #6
  7. eli m

    Stefan Ram Guest

    What specifically is wrong with "using namespace"? (was: Why is my code showing this error?)

    Bill Gill <> writes:
    >So my question: What specifically is wrong with "using namespace"?


    Nothing when used in small tutorial examples or ad-hoc
    code that does not have to be maintained costly.

    It is not to be recommended for large library-grade code
    that is expected to have to be maintained or for code
    that has to have as few potential errors as possible,
    because the author might not be aware of all imported
    names or the headers might add more name to ::std in
    the future.
    Stefan Ram, Mar 5, 2013
    #7
  8. eli m

    Jorgen Grahn Guest

    On Tue, 2013-03-05, James Kanze wrote:
    > On Tuesday, 5 March 2013 07:42:08 UTC, Juha Nieminen wrote:
    >> eli m <> wrote:

    >
    >> > using namespace std;

    >
    >> Why does this always creep up into beginners' code? Where do they learn
    >> this stuff?

    >
    > The same place they learn `while ( !cin.eof() )`? There are two
    > or three such things which seem to continually pop up. (And
    > I too wonder where they are learning it.)


    A lot more than 2--3 if you count bad influences from C and Java.

    My theory is that the teachers and books out there are worse than
    you'd think, when you know the language already and are past the
    beginner's books.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Mar 5, 2013
    #8
  9. eli m

    Paul N Guest

    On Mar 5, 6:40 pm, Bill Gill <> wrote:

    > So my question:  What specifically is wrong with "using namespace"?


    There have been occasions where "using namespace std" has brought in a
    little-known name which clashes with a name used in the program, with
    the result that the cut-down code posted to comp.lang.c++ (which uses
    "using namespace std") fails to work for a completely different reason
    from why the poster's actual code (which doesn't) fails to work. Fun
    all round.
    Paul N, Mar 5, 2013
    #9
  10. eli m

    eli m Guest

    Sorry guys, i just started learning c++ yesterday and i am coming from python.
    eli m, Mar 5, 2013
    #10
  11. On 3/5/2013 6:22 PM, eli m wrote:
    > Sorry guys, i just started learning c++ yesterday and i am coming from python.


    <shrug> You sound like you've just started learning French coming from
    Portuguese. Get a decent book. You can't learn C++ by "trial and
    error" method.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Mar 6, 2013
    #11
  12. eli m

    Bill Gill Guest

    On 3/5/2013 4:46 PM, Juha Nieminen wrote:

    > Do you understand why namespaces exist in the first place? Do you
    > understand why "using namespace" completely nullifies those benefits?
    >
    >

    Well, actually I don't think I do. I'm not sure that it has ever
    been explained to me. Mostly it has just been said, "this is how
    namespaces work". Admittedly I am not very advanced in C++, maybe
    everybody is taking it slow to introduce the basic concepts until
    the student has learned the mechanics of the language.

    Bill
    Bill Gill, Mar 6, 2013
    #12
  13. eli m

    Bill Gill Guest

    On 3/6/2013 1:46 AM, Juha Nieminen wrote:
    > Bill Gill <> wrote:
    >>> Do you understand why namespaces exist in the first place? Do you
    >>> understand why "using namespace" completely nullifies those benefits?
    >>>

    >> Well, actually I don't think I do. I'm not sure that it has ever
    >> been explained to me. Mostly it has just been said, "this is how
    >> namespaces work". Admittedly I am not very advanced in C++, maybe
    >> everybody is taking it slow to introduce the basic concepts until
    >> the student has learned the mechanics of the language.

    >
    > Let me explain it with an actual example that has come up in actual
    > projects.
    >
    > Especially in the past many C++ projects had "using namespace std;"
    > and "using namespace boost;" everywhere as a matter of course. The
    > projects that used "shared_ptr" broke once compilers started adding
    > support for C++11 (and before the Boost library was fixed to take
    > this exact situation into account.)
    >
    > The namespace "std" is reserved for the standard library, and as long
    > as you don't start putting your own stuff inside it, you can be sure
    > that no naming collisions will happen with standard stuff. If you never
    > use "using namespace std;" of course.
    >
    > In principle this exact problem could be solved by standardizing certain
    > prefixes, such as stating that any name starting with "std_" is reserved
    > for the compiler and shouldn't be used by user code. However, namespaces
    > offer flexibility that simple name prefixes don't (such as namespace
    > aliases, and importing namespaces into other namespaces.)
    >
    > --- news://freenews.netfront.net/ - complaints: ---
    >

    But that doesn't address the things you asked me about.

    "Do you understand why namespaces exist in the first place? Do you
    understand why "using namespace" completely nullifies those benefits?"

    I still don't know why they exist and what the benefits are.

    Bill
    Bill Gill, Mar 6, 2013
    #13
  14. On 3/6/2013 9:13 AM, Bill Gill wrote:
    >[..]
    > I still don't know why they exist and what the benefits are.


    At the risk of stating pretty much the obvious, namespaces are designed
    to help in developing large systems with substantial number of
    third-party components in use, and essentially the sole purpose of the
    namespaces is to provide a simpler way to manage possible name conflicts
    between different components.

    Imagine that you're writing some kind of library (for internal
    consumption, say) that, along with uniquely named functions and classes,
    contains names like 'min' or 'max'. In order to afford your code to use
    the specific 'min' (class or function) in your code it is recommended
    that you put *your* 'min' into a namespace of your choice, and then in
    the module that is only to use your 'min' you could bring that name (and
    all names from your own namespace) into the scope by means of the
    'using' directive. Or, alternatively, when you need to use both the
    standard 'min' and your own, specify which one the compiler is to
    consider with the help of the namespace prefix.

    If that's something you already knew, then perhaps I didn't understand
    your problem, sorry.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Mar 6, 2013
    #14
  15. eli m

    Bill Gill Guest

    On 3/6/2013 8:31 AM, Victor Bazarov wrote:
    > On 3/6/2013 9:13 AM, Bill Gill wrote:
    >> [..]
    >> I still don't know why they exist and what the benefits are.

    >
    > At the risk of stating pretty much the obvious, namespaces are designed
    > to help in developing large systems with substantial number of
    > third-party components in use, and essentially the sole purpose of the
    > namespaces is to provide a simpler way to manage possible name conflicts
    > between different components.
    >
    > Imagine that you're writing some kind of library (for internal
    > consumption, say) that, along with uniquely named functions and classes,
    > contains names like 'min' or 'max'. In order to afford your code to use
    > the specific 'min' (class or function) in your code it is recommended
    > that you put *your* 'min' into a namespace of your choice, and then in
    > the module that is only to use your 'min' you could bring that name (and
    > all names from your own namespace) into the scope by means of the
    > 'using' directive. Or, alternatively, when you need to use both the
    > standard 'min' and your own, specify which one the compiler is to
    > consider with the help of the namespace prefix.
    >
    > If that's something you already knew, then perhaps I didn't understand
    > your problem, sorry.
    >
    > V

    Thanks, that was pretty much what I was wondering. It makes sense to
    think of it that way. Now all I have to do is to figure out when
    to use "using namespace" and when to use "namespace::" which is
    basically going to be an individual decision based on the circumstances.

    Bill
    Bill Gill, Mar 6, 2013
    #15
  16. On 3/6/2013 11:29 AM, Bill Gill wrote:
    > On 3/6/2013 8:31 AM, Victor Bazarov wrote:
    >> On 3/6/2013 9:13 AM, Bill Gill wrote:
    >>> [..]
    >>> I still don't know why they exist and what the benefits are.

    >>
    >> At the risk of stating pretty much the obvious, namespaces are designed
    >> to help in developing large systems with substantial number of
    >> third-party components in use, and essentially the sole purpose of the
    >> namespaces is to provide a simpler way to manage possible name conflicts
    >> between different components.
    >>
    >> Imagine that you're writing some kind of library (for internal
    >> consumption, say) that, along with uniquely named functions and classes,
    >> contains names like 'min' or 'max'. In order to afford your code to use
    >> the specific 'min' (class or function) in your code it is recommended
    >> that you put *your* 'min' into a namespace of your choice, and then in
    >> the module that is only to use your 'min' you could bring that name (and
    >> all names from your own namespace) into the scope by means of the
    >> 'using' directive. Or, alternatively, when you need to use both the
    >> standard 'min' and your own, specify which one the compiler is to
    >> consider with the help of the namespace prefix.
    >>
    >> If that's something you already knew, then perhaps I didn't understand
    >> your problem, sorry.
    >>
    >> V

    > Thanks, that was pretty much what I was wondering. It makes sense to
    > think of it that way. Now all I have to do is to figure out when
    > to use "using namespace" and when to use "namespace::" which is
    > basically going to be an individual decision based on the circumstances.


    Right. IME the 'using namespace <myown>;' most often finds its place in
    the implementation of myown library functions, with all other namespaces
    (including 'std') actually spelled out. Anything without the namespace
    is clearly "this one", everything else has a namespace prefix (empty for
    the global names, of course).

    Another approach I've seen was only to use namespace prefix when a
    conflict is discovered. I do however view it as deficient because,
    especially in template code, there can be no conflict and still you may
    not necessarily be fully aware which namespace is used to resolve a
    particular name. Sometimes very weird bugs can be introduced by that.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Mar 6, 2013
    #16
  17. eli m

    Bill Gill Guest

    On 3/6/2013 10:48 AM, Victor Bazarov wrote:
    > On 3/6/2013 11:29 AM, Bill Gill wrote:
    >> On 3/6/2013 8:31 AM, Victor Bazarov wrote:
    >>> On 3/6/2013 9:13 AM, Bill Gill wrote:
    >>>> [..]
    >>>> I still don't know why they exist and what the benefits are.
    >>>
    >>> At the risk of stating pretty much the obvious, namespaces are designed
    >>> to help in developing large systems with substantial number of
    >>> third-party components in use, and essentially the sole purpose of the
    >>> namespaces is to provide a simpler way to manage possible name conflicts
    >>> between different components.
    >>>
    >>> Imagine that you're writing some kind of library (for internal
    >>> consumption, say) that, along with uniquely named functions and classes,
    >>> contains names like 'min' or 'max'. In order to afford your code to use
    >>> the specific 'min' (class or function) in your code it is recommended
    >>> that you put *your* 'min' into a namespace of your choice, and then in
    >>> the module that is only to use your 'min' you could bring that name (and
    >>> all names from your own namespace) into the scope by means of the
    >>> 'using' directive. Or, alternatively, when you need to use both the
    >>> standard 'min' and your own, specify which one the compiler is to
    >>> consider with the help of the namespace prefix.
    >>>
    >>> If that's something you already knew, then perhaps I didn't understand
    >>> your problem, sorry.
    >>>
    >>> V

    >> Thanks, that was pretty much what I was wondering. It makes sense to
    >> think of it that way. Now all I have to do is to figure out when
    >> to use "using namespace" and when to use "namespace::" which is
    >> basically going to be an individual decision based on the circumstances.

    >
    > Right. IME the 'using namespace <myown>;' most often finds its place in
    > the implementation of myown library functions, with all other namespaces
    > (including 'std') actually spelled out. Anything without the namespace
    > is clearly "this one", everything else has a namespace prefix (empty for
    > the global names, of course).
    >
    > Another approach I've seen was only to use namespace prefix when a
    > conflict is discovered. I do however view it as deficient because,
    > especially in template code, there can be no conflict and still you may
    > not necessarily be fully aware which namespace is used to resolve a
    > particular name. Sometimes very weird bugs can be introduced by that.
    >
    > V

    I think I will skip the waiting to find a problem method. That sounds
    like a recipe for disaster. What I may start doing is to use
    'using namespace std;' for the standard libraries. There shouldn't
    be any conflicts there, well there shouldn't be, maybe I will go ahead
    and use the <myown>::func(), or whatever for everything that isn't
    locally declared. I am not really planning to working on any major
    projects with many people involved. But still you never know, and if
    I get in the habit I will not accidentally miss it if I do get into
    a bigger project. That is one place where habit can work for you.
    If you get in the habit of doing things the safest way you won't get
    bit later.

    Bill
    for
    Bill Gill, Mar 6, 2013
    #17
  18. eli m

    Jorgen Grahn Guest

    On Wed, 2013-03-06, Bill Gill wrote:
    ....
    > But that doesn't address the things you asked me about.
    >
    > "Do you understand why namespaces exist in the first place? Do you
    > understand why "using namespace" completely nullifies those benefits?"
    >
    > I still don't know why they exist and what the benefits are.


    Something like this:

    Namespaces replace the C convention of adding prefixes to names to:
    - mark them as related (e.g. come from the same library)
    - mark them as part of a bigger thing (e.g. the Foo library)
    - avoid name clashes ("I cannot use library Foo, because I already
    use Bar, and both contain a function barney()")

    While they were at it, they added obvious enhancements like:
    - you don't have to mention the namespace prefix all the time
    /inside/ the namespace itself
    - some other "it should be obvious which one I mean" situations
    - you can say "okay, within this context, whenever I say
    string, I mean std::string"
    - "using namespace", for when you just don't care at all

    (And then there's the anonymous namespaces, and probably other things
    I forgot.)

    I notice "using namespace" is hardest to motivate. I suspect the main
    reason for adding it was to help people migrate to a standard library
    with the std:: namespace.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Mar 9, 2013
    #18
  19. Juha Nieminenæ–¼ 2013å¹´3月5日星期二UTC+8下åˆ3時42分08秒寫é“:
    > eli m wrote:
    >
    > > using namespace std;

    >
    >
    >
    > Why does this always creep up into beginners' code? Where do they learn
    >
    > this stuff?
    >
    >
    >
    > --- news://freenews.netfront.net/ - complaints: ---


    How about using several libraries with many functions ?

    Because the way of c++ resolves the function identity,
    it is better to write c++ with some assistant tools to look
    up function declarations carefully.
    88888 Dihedral, Mar 10, 2013
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ray Booysen
    Replies:
    2
    Views:
    633
    Ray Booysen
    Mar 28, 2006
  2. Samuël van Laere

    IE downloading, not showing. Why?

    Samuël van Laere, Jan 3, 2005, in forum: HTML
    Replies:
    12
    Views:
    810
    Jeffrey Silverman
    Jan 5, 2005
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    861
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,761
    Smokey Grindel
    Dec 2, 2006
  5. PerfectDayToChaseTornados

    why is error not showing on rhtml page

    PerfectDayToChaseTornados, Mar 29, 2007, in forum: Ruby
    Replies:
    1
    Views:
    75
    PerfectDayToChaseTornados
    Mar 30, 2007
Loading...

Share This Page