Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Don't fear the penguins!
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    arborlon.gingle.net
    Posts
    147
    Rep Power
    12

    Unhappy Get beginning characters?


    Hey, could someone please tell me how I can get, say the first 3 characters from the beginning of the string 'cArgv[iCount]'? Thanks, I'm new to C++.

    Oh yeh, I'm using Visual C++ .NET, fyi.
    Every morning, I get up and look through the Forbes list of the richest people in America. If I'm not there, I go to work.

    May your Tongue stick to the Roof of your Mouth with the Force of a Thousand Caramels.

    To the systems programmer, users and applications serve only to provide a test load.
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    here's one way:
    Code:
    char array[] = "Hello World";
    char my_array[10] = "";
    
    strncpy(my_array, array, 3);
    
    cout << my_array << endl;
    -will output Hel
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Hey, could someone please tell me how I can get, say the first 3 characters from the beginning of the string 'cArgv[iCount]'?
    If you had a char array like this:

    char text[10];

    how would you access the first three characters? You would do that using their index positions:

    text[0], text[1], and text[2].

    Now, if you have string named cArgv[iCount], you can still use array notation to access the characters in the string--just substitute the name cArgv[iCount] for the name text above:

    cArgv[iCount][0], cArgv[iCount][1], and cArgv[iCount][2]

    If you want to store those characters in another char array and at some point display them, you need to be sure to tack on a '\0' at the end otherwise the << operator(as in cout<<first3;) won't know when the string ends.

    Code:
    #include <iostream>
    using namespace std;
    
    int main () 
    {
    	
    	char* argv[3];
    	
    	char string0[]="hello ";
    	char string1[]="the ";
    	char string2[]="world";
    
    	argv[0]=string0;
    	argv[1]=string1;
    	argv[2]=string2;
    
    	
    	//Declare a char array to hold the first
    	//three chars and one additional place
    	//for a terminating '\0':
    	char first3[4];
    	
    	//Assign the first three chars of argv[2]
    	//to the variable first3:
    	for(int i=0; i<3; i++)
    		first3[i] = argv[2][i];
    
    	//Tack on a '\0' to the end of the three chars
    	//to signal the end of the string:
    	first3[3]='\0';
    
    	//Display the string:
    	cout<<first3<<endl;
    
    	return 0;
    }
    Last edited by 7stud; May 27th, 2003 at 09:07 PM.
  6. #4
  7. Don't fear the penguins!
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    arborlon.gingle.net
    Posts
    147
    Rep Power
    12

    Smile


    Ah, ok. I get it now. Thanks for all the help everyone!
    Every morning, I get up and look through the Forbes list of the richest people in America. If I'm not there, I go to work.

    May your Tongue stick to the Roof of your Mouth with the Force of a Thousand Caramels.

    To the systems programmer, users and applications serve only to provide a test load.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,155
    Rep Power
    2222

    Re: Get beginning characters?


    Originally posted by Jair
    Hey, could someone please tell me how I can get, say the first 3 characters from the beginning of the string 'cArgv[iCount]'? Thanks, I'm new to C++.

    Oh yeh, I'm using Visual C++ .NET, fyi.
    Since I've not done anything with .NET, I need to ask what the data type of cArgv[iCount] is. The other replies work great for a char array, which is what the standard C/C++ definition of argv[] uses. However, if .NET uses CString or string (as per STL), then you will have other options available. Besides, if it uses CString, then you would need to use the GetBuffer method to actually get to the char array for their methods to work.

    CString has a Left() method, so if cArgv[] is a CString type:
    Code:
    CString left3 = cArgv[iCount].Left(3);
    The STL string type has a substring method, substr, that returns the portion of the string the length of the second parameter starting at the first parameter, so if cArgv[] is an STL string:
    Code:
    string left3 = cArgv[iCount].substr(1,3);
  10. #6
  11. Don't fear the penguins!
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    arborlon.gingle.net
    Posts
    147
    Rep Power
    12
    Ok, this is what I have:

    Code:
    #include <stdio.h>
    
    void main(int iArgc, char *cArgv[])
    {
    int iCount;
    char first3[4];
    
    for(iCount = 0; iCount < iArgc; iCount++)
    {
    	for(int i=0; i<3; i++)
    		first3[i] = cArgv[iCount][i];
    	
    	first3[3]='\0';
    	printf("%s\n", first3); 
    }
    
    }
    This works perfectly. :D However, dwise1_aol, I'm curious about those other methods that you listed. I don't really understand what the CString and STL stuff is about :confused:. So, is there any chance of getting it explained in newb terms? Thanks.
    Every morning, I get up and look through the Forbes list of the richest people in America. If I'm not there, I go to work.

    May your Tongue stick to the Roof of your Mouth with the Force of a Thousand Caramels.

    To the systems programmer, users and applications serve only to provide a test load.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Here is a demonstration of the string type defined in the standard library header file <string>, which is much easier to use than a string stored in a char type array:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main () 
    {
    	string text;
    	text="Hello";
    
    	for(int i=0; i<text.length(); i++)
    		cout<<text[i];
    	cout<<endl;
    
    	//starting at index position 1 display
    	//two characters;
    	cout<<text.substr(1,2)<<endl;
    
    	text = text + " world";
    	cout<<text<<endl;
    
    	cout<<text.find('e')<<endl;
    	
    	return 0;
    }
    Last edited by 7stud; May 28th, 2003 at 03:12 PM.
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,155
    Rep Power
    2222
    Originally posted by Jair
    Ok, this is what I have:

    Code:
    #include <stdio.h>
    
    void main(int iArgc, char *cArgv[])
    {
    ...
    This works perfectly. :D
    Yes, as it should with cArgv defined as it normally is in C, as an array of char pointers. I just wasn't sure if .NET had messed that up or not.

    Originally posted by Jair
    However, dwise1_aol, I'm curious about those other methods that you listed. I don't really understand what the CString and STL stuff is about :confused:. So, is there any chance of getting it explained in newb terms? Thanks.
    Part of the business of programming in C++ is creating and using objects, AKA "object-oriented programming". It is not uncommon to create objects that handle some messy part of programming and providing you with a simpler or more familiar interface to work with.

    Central to Visual C++ are the Microsoft Foundation Classes (MFC) while encapsulate much of Windows programming, making the job of writing a Windows application much simpler. One of the "helper classes" it provides is CString, which is a character-string class that you can assign strings to (eg, CString s = "a string"; ), resize dynamically, and do other string-handling stuff so much more easily, including some old BASIC functions like Left and Right. To find out more about CString, look it up in the Visual C++ help.

    An addition to C++ is the idea of templates, in which you can define a generic object, usually a container or data structure (eg, linked lists, binary trees), that will work for any object. When you use a template in a declaration, you give it a class to insert into the template and it creates a class for that. Then some group -- I don't remember who -- devised a standard set of templates to handle the most common programming needs, such as linked lists, and called it the Standard Template Library (STL). The advantage to the STL is that it can be used on any platform that has a C++ compiler -- Windows, UNIX, Linux, Mac, whatever -- ; in contrast, CString will only work on Windows and only with a compiler that supports MFC.

    The STL provides us with a string type, string, that also makes it much easier to work with character strings. To learn more about it, you'll need to look online. Visual C++ help is rather sketchy about its STL entries.
    Last edited by dwise1_aol; May 28th, 2003 at 03:18 PM.
  16. #9
  17. Don't fear the penguins!
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    arborlon.gingle.net
    Posts
    147
    Rep Power
    12

    Lightbulb


    @ 7stud: I think that I'm starting to understand this now! :) Thanks for that example (its probably headed off other questions ;) ).

    @ dwise1_aol: Thanks for clarifying that. So, if I have any intention of porting my app to another os, STL is the one I should use?
    Every morning, I get up and look through the Forbes list of the richest people in America. If I'm not there, I go to work.

    May your Tongue stick to the Roof of your Mouth with the Force of a Thousand Caramels.

    To the systems programmer, users and applications serve only to provide a test load.
  18. #10
  19. Don't fear the penguins!
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    arborlon.gingle.net
    Posts
    147
    Rep Power
    12
    Oh, 7stud, what does the line 'using namespace std;' mean?
    Every morning, I get up and look through the Forbes list of the richest people in America. If I'm not there, I go to work.

    May your Tongue stick to the Roof of your Mouth with the Force of a Thousand Caramels.

    To the systems programmer, users and applications serve only to provide a test load.
  20. #11
  21. Don't fear the penguins!
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    arborlon.gingle.net
    Posts
    147
    Rep Power
    12
    Ok, I was rewriting things to do it like in 7stud's example, and now when I try to build it spits back:

    c:\Documents and Settings\jair\My Documents\Visual Studio Projects\winslook\winslook.cpp(7): error C2065: 'sArgv' : undeclared identifier
    c:\Documents and Settings\jair\My Documents\Visual Studio Projects\winslook\winslook.cpp(7): error C2065: 'string' : undeclared identifier
    c:\Documents and Settings\jair\My Documents\Visual Studio Projects\winslook\winslook.cpp(7): error C2146: syntax error : missing ';' before identifier 'sArgv'
    c:\Documents and Settings\jair\My Documents\Visual Studio Projects\winslook\winslook.cpp(8): error C2065: 'sFirst3' : undeclared identifier
    c:\Documents and Settings\jair\My Documents\Visual Studio Projects\winslook\winslook.cpp(8): error C2146: syntax error : missing ';' before identifier 'sFirst3'
    c:\Documents and Settings\jair\My Documents\Visual Studio Projects\winslook\winslook.cpp(13): error C2228: left of '.substr' must have class/struct/union type


    Heres the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void main(int iArgc, char *cArgv[])
    {
    int iCount;
    string sArgv;
    string sFirst3;
    
    for(iCount = 0; iCount<iArgc; iCount++)
    {
    	sArgv = cArgv[iCount];
    	sFirst3 = sArgv.substr(0,3);
    	printf("%s", sFirst3);
    }
    }
    *sigh*, sometimes it seems as if I'll never learn... :rolleyes:
    Every morning, I get up and look through the Forbes list of the richest people in America. If I'm not there, I go to work.

    May your Tongue stick to the Roof of your Mouth with the Force of a Thousand Caramels.

    To the systems programmer, users and applications serve only to provide a test load.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    #include <string.h>

    That's not the header file I specified, the string type is defined in the header file <string>. C++ or any computer language is very exacting. Almost right doesn't work. You have to get the syntax right, you can't misspell variable names, or generally be sloppy.

    Also, there are no header files under the new standard for C++ that end in .h. Header files with a .h extension are included for backwards compatibility, but at any time that could end, so you should try to learn C++ as it is, not as it was, if you want your programs to function correctly. None of your programs should have header files that end in .h. If you are studying a book that has those, it is outdated, and I suggest you replace it with a book that is current. If you are studying a tutorial that has those, abandon it.

    Read about "namespaces" to find out what:

    using namespace std;

    does.
    Last edited by 7stud; May 28th, 2003 at 07:34 PM.
  24. #13
  25. Don't fear the penguins!
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    arborlon.gingle.net
    Posts
    147
    Rep Power
    12
    When I removed the '.h' from it, it still does not compile. Does it look like I screwed somthing else up?
    Every morning, I get up and look through the Forbes list of the richest people in America. If I'm not there, I go to work.

    May your Tongue stick to the Roof of your Mouth with the Force of a Thousand Caramels.

    To the systems programmer, users and applications serve only to provide a test load.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Did you investigate what namespaces are?

    Examine your code, and then examine the code I posted and start at the top and go line by line and see if there are any differences.
  28. #15
  29. Don't fear the penguins!
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    arborlon.gingle.net
    Posts
    147
    Rep Power
    12
    Ok, I included iostream, and viola! it compiled. Didn't include it at first cause I was using printf instead of cout. Also, I did read up on namespaces, so I belive I understand that part as well now. Thanks a lot for all the help!
    Every morning, I get up and look through the Forbes list of the richest people in America. If I'm not there, I go to work.

    May your Tongue stick to the Roof of your Mouth with the Force of a Thousand Caramels.

    To the systems programmer, users and applications serve only to provide a test load.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo