CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Dec 2008
    Posts
    27

    C++ code problem

    i am just getting started with using C++ and i was wandering if somebody could tell me were i went wrong in my code and show me how it is supposed to be done any help would be great. here is the code

    Code:
    #include<iostream.h>
    
    //declares as type int
    int First,Middle,Last
    //main program starts
     int main ()
    {
     char first,middle,last;
    
    cout << "enter your name: \n" ;
    cout << "\ First: " ;
    cin >> First;
    cout << "\ Middle: " ;
    cin >> Middle ;
    cout << "\ Last: " ;
    cin >> Last ;
    cout << " welcome," "<<First <<Middle <<Last \n" ;
    //main program end
    return 0;
    }
    Last edited by twistedmike; December 25th, 2008 at 10:30 PM.

  2. #2
    Join Date
    Feb 2007
    Location
    Craiova, Romania
    Posts
    326

    Re: C++ code problem

    first, middle and last should be character arrays (or strings), since they can hold one more than 1 character.
    I suggest using std::string:
    Code:
    #include <string>
    ...
    string first,middle,last;
    Also, you should drop the global variables First, Middle and Last because they seem useless.

  3. #3
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: C++ code problem

    1) Please EDIT your post to use code tags. They are covered in the "BEFORE you post announcement".
    2) What book are you learning from. The very first line if NOT compliant C++ code. If that is what is in the book. GET a NEW BOOK!

    Adter taking care of those two items, we can get to more details...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  4. #4
    Join Date
    Dec 2008
    Posts
    27

    Re: C++ code problem

    well i tried redoing it the way you said this is the code
    Code:
    #include<iostream.h>
    #include<string.h>
    
    //declares as type int
    string First,Middle,Last
    //main program starts
     int main ()
    {
     string first,middle,last;
    
    cout << "enter your name: \n" ;
    cout << "\ First: " ;
    cin >> First;
    cout << "\ Middle: " ;
    cin >> Middle ;
    cout << "\ Last: " ;
    cin >> Last ;
    cout << " welcome," "<<First <<Middle <<Last \n" ;
    //main program end
    return 0;
    }
    i compiled it with dev C++ 4.9.9.2
    and this is the message i got when i compiled it


    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "F:\Apps\Dev-Cpp\COMPILED\name input.cpp" -o "F:\Apps\Dev-Cpp\COMPILED\name input.exe" -g3 -I"F:\Apps\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"F:\Apps\Dev-Cpp\include\c++\3.4.2\backward" -I"F:\Apps\Dev-Cpp\include\c++\3.4.2\mingw32" -I"F:\Apps\Dev-Cpp\include\c++\3.4.2" -I"F:\Apps\Dev-Cpp\include" -L"F:\Apps\Dev-Cpp\lib" -g3
    In file included from F:/Apps/Dev-Cpp/include/c++/3.4.2/backward/iostream.h:31,
    from F:\Apps\Dev-Cpp\COMPILED\name input.cpp:1:
    F:/Apps/Dev-Cpp/include/c++/3.4.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
    F:\Apps\Dev-Cpp\COMPILED\name input.cpp:5: error: `string' does not name a type

    F:\Apps\Dev-Cpp\COMPILED\name input.cpp:12:9: warning: unknown escape sequence: '\040'
    F:\Apps\Dev-Cpp\COMPILED\name input.cpp:14:9: warning: unknown escape sequence: '\040'

    F:\Apps\Dev-Cpp\COMPILED\name input.cpp:16:9: warning: unknown escape sequence: '\040'

    Execution terminated

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: C++ code problem

    What part of:
    The very first line if NOT compliant C++ code
    And you Still have that as your first line of code.

    Once again (and for the last time) I will ask:

    What book are you learning from.

    and if that is what the book is showing you to include; then GET a NEW BOOK!
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  6. #6
    Join Date
    Dec 2008
    Posts
    27

    Re: C++ code problem

    i am not learning from a book i am learning from online sources.but besides the fact could you please help me with fixing the code

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: C++ code problem

    Quote Originally Posted by twistedmike View Post
    i am not learning from a book i am learning from online sources.but besides the fact could you please help me with fixing the code
    It is virtually impossible to learn C++ that way. And there are too many things wrong with the code (and probably your basic knowledge).


    1) Get a GOOD Book (Yes, the kind on printed paper)
    2) Read EVERY word...in order..no browsing or skimming
    3) Manually Type In EVERY line of code (even if it comes with a CD/DVD
    4) Step through every line of code with the debugger.
    5) At the end of each chapter write your own (Silly) program that uses the topic. Step through EVERY line.
    6) When you are sure that you have mastered EVERYTHING in the chapter, then and only then do you move to the next chapter.

    C++ is a very detailed oriented language. Many times there will be a single sentence in a book that is critical to understanding, and if you skip any of the above steps you are likely to make false assumptions (especially if you HAVE programmed in another language). This error will corrupt the remainder of your thinking process.

    Based on people I have trained over the last 15 years in C++; plan on spending about 200-300 hours in order to become a "novice" C++ programmer.

    ANY good book, would have told you that standard C++ header files DO NOT have a .h.

    EVEN the compiler output, told you that you are not using a standard header.
    Code:
    In file included from F:/Apps/Dev-Cpp/include/c++/3.4.2/backward/iostream.h:31,
    This means that you are using some type of non-standard complaint header which will have functionallity that can vary from compiler to compiler.

    Also, and book that covered STL (which is the portion of the language that string and iostream are part of) would have properly explained the use of namespaces.

    The list goes, on and on......

    Remember that it is estimated that 90%++ of the information on the internet is not correct [ie the page contains at least one error or incomplete item]. You can NOT learn from this.

    When you have a good textbook, and are following it according to the information posted above, then you can AUGMENT this with web based material, always remembering to treat the book as the authority, and items on the web as just hints.

    Most of the people who POST C++ code, do NOT know how to write proper, com0pliant code.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++ code problem

    Quote Originally Posted by twistedmike View Post
    i am not learning from a book i am learning from online sources.
    I'll second what TheCPUWizard has told you.

    You can't learn C++ that way. Many of those "online sources" are just plain wrong, or are so outdated, the material given to you is obsolete and useless to you.
    but besides the fact could you please help me with fixing the code
    The very first line is wrong:
    Code:
    #include <iostream.h>  // wrong!!
    #include <iostream>  // correct
    Given that the very first line you wrote was wrong, what chance do you really have in getting anything else correct? C++ was standardized in 1998. Any website showing <iostream.h> is either wrong, or is woefully behind the times (more than 10 years).

    If you want to see a website that has a proper FAQ concerning correct C++ programming, go here:

    http://www.parashift.com/c++-faq-lite/
    http://www.research.att.com/~bs/bs_faq.html
    http://www.research.att.com/~bs/bs_faq2.html
    http://www.research.att.com/~bs/glossary.html

    Regardless of the links above, you need to learn from a good C++ book. (A book that is up to date (nothing before 1998) and not a book that starts you off coding 'C' without showing any C++ whatsoever until the umpteenth chapter).

    Books are peer-reviewed, websites are not, that is why you must use books to learn the language. As to websites, unless the website material comes from one of the well-known authorities of C++ (Stroustrup, Myers, Alexandrescu, Sutter, etc. or one of the ANSI comittee members), or one that receives many accolades (the one run by Marshall Cline for example), there is a high probability that the information given to you is wrong. That is why here on CodeGuru, many members do not teach a poster C++ when a good book that explains what they are asking about does a much better job.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: C++ code problem

    Paul,

    Thanks for the "Kudos"

    The only difference I would make in the post would be the timeframe for the book. I personally recommend something that was (at least updated) in 2004 or later because of the '03 changes in the specification.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++ code problem

    Quote Originally Posted by twistedmike View Post
    i am just getting started with using C++ and i was wandering if somebody could tell me were i went wrong in my code and show me how it is supposed to be done any help would be great. here is the code
    BTW, here is a proper "Hello World" program written in C++:
    Code:
    #include <iostream>
    int main()
    {
       std::cout << "Hello World";
    }
    So even a simple "Hello World" program deviates from what you posted in critical areas. First, the correct header is used (iostream). Second, the introduction of a "namespace" qualifier (in this case, "std::") is required to properly access cout.

    All of these are shown in chapter 1 of any good C++ book, and it was entirely missed by whatever website you're learning from. The first program that a C++ programmer should see is something similar to the above (the namespace may be specified with "using namespace std;", but that would be the only difference).

    Regards,

    Paul McKenzie

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: C++ code problem

    Quote Originally Posted by twistedmike View Post
    well i tried redoing it the way you said this is the code
    Code:
    #include<iostream.h>
    #include<string.h>
    
    //declares as type int
    string First,Middle,Last
    //main program starts
     int main ()
    {
     string first,middle,last;
    
    cout << "enter your name: \n" ;
    cout << "\ First: " ;
    cin >> First;
    cout << "\ Middle: " ;
    cin >> Middle ;
    cout << "\ Last: " ;
    cin >> Last ;
    cout << " welcome," "<<First <<Middle <<Last \n" ;
    //main program end
    return 0;
    }
    i compiled it with dev C++ 4.9.9.2
    and this is the message i got when i compiled it


    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "F:\Apps\Dev-Cpp\COMPILED\name input.cpp" -o "F:\Apps\Dev-Cpp\COMPILED\name input.exe" -g3 -I"F:\Apps\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"F:\Apps\Dev-Cpp\include\c++\3.4.2\backward" -I"F:\Apps\Dev-Cpp\include\c++\3.4.2\mingw32" -I"F:\Apps\Dev-Cpp\include\c++\3.4.2" -I"F:\Apps\Dev-Cpp\include" -L"F:\Apps\Dev-Cpp\lib" -g3
    In file included from F:/Apps/Dev-Cpp/include/c++/3.4.2/backward/iostream.h:31,
    from F:\Apps\Dev-Cpp\COMPILED\name input.cpp:1:
    F:/Apps/Dev-Cpp/include/c++/3.4.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
    F:\Apps\Dev-Cpp\COMPILED\name input.cpp:5: error: `string' does not name a type

    F:\Apps\Dev-Cpp\COMPILED\name input.cpp:12:9: warning: unknown escape sequence: '\040'
    F:\Apps\Dev-Cpp\COMPILED\name input.cpp:14:9: warning: unknown escape sequence: '\040'

    F:\Apps\Dev-Cpp\COMPILED\name input.cpp:16:9: warning: unknown escape sequence: '\040'

    Execution terminated
    The others are right, that you can't learn just from reading online stuff.

    The compiler is being pretty specific about what's wrong. You should pay attention to what it's telling you.

  12. #12
    Join Date
    Dec 2008
    Posts
    27

    Re: C++ code problem

    Ok guys thanks for the brutal input i will do what u said for me to then try coding it all over again.but thanks for the help anyways.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured