Results 1 to 10 of 10

Thread: A buncha newbie questions

  1. #1
    zchoyt
    Guest

    A buncha newbie questions

    Hi, I am a newbie at C++, and kind of a newbie at C, so I am gonna ask some real simple questions now........

    What do I need to include so that I can use string type? like this:
    string TempString;

    What do I need to include to read and write to a basic text file?
    Any links with good examples would be cool as well.







    I will ask more later.

  2. #2
    zchoyt
    Guest
    I figured that out. Just #include <string.h>....but

    What is the differance between these?:
    string TempString;
    char TempString;

    Also, What is a good way to make the console stay open until the user closes it. I have been doing this:
    PHP Code:
    void main()
    {
        
    //do stuff........

        //endless loop (keeps main from terminating
        
    while(1)
        {
        }


  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Let's see...
    don't start with bad practices now when there's still time!
    j/k, you shouldn't include <string.h>, it's deprecated. In modern C++ apps the headers don't have a .h ending:
    #include <string>
    Since in the new headers all symbols are in the namespace std, you must either always reference the namespace:
    std::string TempString;
    or import some symbols from std:
    using std::string;
    or iport the whole namespace (what most people do, especially in simple apps):
    using namespace std;
    Stay with the last for now, just insert it after all your header includes.

    The difference:
    string is a class that handles memory management, operator overloading and all these nice things.
    char is a single character.
    char * TempString;
    would mean a pointer to a character, usually the character is followed by others to make a C-style string. Don't use it, it's a real trap for newbies and string is so easy.
    When you use string and need a char* for a function, call the c_str() method.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    zchoyt
    Guest
    Hey thanks for the tip. I have heard that before (about the .h), but when I use this:

    #include "stdafx.h"
    #include <string>
    #include <windows>
    #include <winbase>
    #include <stdio>
    using namespace std;

    It will give me the following error:"Cannot open include file: 'windows': No such file or directory"

    and then if I put the .h back like this:
    #include "stdafx.h"
    #include <string.h>
    #include <windows.h>
    #include <winbase.h>
    #include <stdio.h>

    using namespace std;

    I get this error:"'std' : does not exist or is not a namespace"


    I think that it does not know where to look for "std". How can I tell it?

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You hardly ever need to explicitly include <winbase.h>. Mostly, just <windows.h> is enough but for a console program you shouldn't use it -- stick to the standard headers.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    zchoyt
    Guest
    OK. I am just seeing what is out there and how to use it thigs. I have only programmed C for PIC microcontrollers, so I am just getting used to talking to windows OS.

    Can you tell me why my compiler doesn't recognize "using namespace std;"?

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If you haven't included any headers that define the namespace, then the compiler won't be able to recognise the name.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
        cout << "Hello!" << endl;
    }
    That should work. What compiler are you on?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    zchoyt
    Guest
    Alright. Thanks for all the help and tips. Are there any other C forums that are more popular than this one? I Have lots of questions to ask, so I need lots of people to answer them for me.

  9. #9
    zchoyt
    Guest

    here is another question

    I am trying to port some API calls from VB over to C. Here is one that I cannot get to work. It gets "'MakeSureDirectoryPathExists' : undeclared identifier" error, but MakeSureDirectoryPathExits shows up in List Members.

    PHP Code:
    #include "stdafx.h"
    #include <windows.h>


    int main(int argccharargv[])
    {
        
    int Result;

        
    Result MakeSureDirectoryPathExists("c:\\test1\\test2\\");
        if (
    Result != 0)
        {
            
    MessageBox(0"Path Created"""MB_OK);
        }
        else
        {
            
    MessageBox(0"Error Creating Path"""MB_OK);
        }

        return 
    0;


    thanks

  10. #10
    zchoyt
    Guest
    I found the answer on another forum.
    I had to add imagehlp.lib to the links path in project settings and also had to add #include <imagehlp.h>. How is someone supposed to find these things out? Why did I have to add all that stuff if the compiler knew what it was anyway (it was in the list members list)


    here is what the code looks like now.
    PHP Code:
    #include "stdafx.h"
    #include <windows.h>
    #include <imagehlp.h>    //also had to add imagehlp.lib in link path in project settings
    #include <string>
    using namespace std;



    int main(int argccharargv[])
    {
        
    int Result;

        
    Result MakeSureDirectoryPathExists("c:\\test1\\test2\\");
        if (
    Result != 0)
        {
            
    MessageBox(0"Path Created"""MB_OK);
        }
        else
        {
            
    MessageBox(0"Error Creating Path"""MB_OK);
        }

        return 
    0;


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