Two string questions

This is a discussion on Two string questions within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by 7stud Using a string type would have saved you that hour. Do you still think char arrays ...

  1. #16
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    Quote Originally Posted by 7stud
    Using a string type would have saved you that hour. Do you still think char arrays are easier to use? I predict many wasted hours in your future.
    Well as a Visual Basic guy, I wanted to use strings. I have been using string since I started in c++.

    But for this project it looked like I was forced to use char.

    Strings are easier then char like vb is easier then c++. You just save hours of needless fiddling. No?

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> But for this project it looked like I was forced to use char.

    First, there is a different between a char and a character array. A character array is a way of representing 0 or more characters that make up a string. A char is a datatype that holds single character. There is also the C++ string class which is another datatype that holds 0 or more characters that make up a string and makes string handling easier.

    So if you think you were forced to use character arrays, the question is, why? There is nothing in your code that requires character arrays. Using char with getch and the string class to hold the characters is perfectly valid.

  3. #18
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *'
    Question:

    I am using char *x;, how would i go about converting (so I can compare them) a char to char *?

    edit: I am forced to use char because there are requirements which need to be done in my coding. Those requirements include using char arrays.

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I posted just before you did, but the same thing applies here. Learn the difference between a character and an array of characters.

    >> I am forced to use char because there are requirements which need to be done in my coding. Those requirements include using char arrays.

    Unless it is an assignment that requires you to use char arrays over C++ strings, I doubt you ever need to use char arrays. The string class does virtually everything a char array can do. If it is a requirement of the assignment, then just remember that in C++ you should be using a string class for when you are programming on your own.
    Last edited by Daved; 02-15-2006 at 04:58 PM.

  5. #20
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    Well, the first program is done. I still need to polish the code (throw it into mandatory functions and make sure all the spacing looks good) but other then that 99% as well as I could want.

    Quote Originally Posted by Daved
    Also, toupper is a function that converts a character to upper case.
    I have been goggling that...

    Code:
    string FirstName = "john";
    I want firstName to be upper case. Is that possible using string?
    Last edited by Zeratulsdomain; 02-16-2006 at 04:02 PM.

  6. #21
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Not in a single call, at least not without using Boost.String_Algo. But it's simple to write.

    http://www.boost.org/
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #22
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    Quote Originally Posted by CornedBee
    Not in a single call, at least not without using Boost.String_Algo. But it's simple to write.

    http://www.boost.org/
    I don't know hat you just gave me.... But it sounds complicated. After I morning test, I spent 3-4 hours working on the program I just finished today. I got to finish another one right now.... Then <code> up some excel... So I guess ill just go with char's...

    tnx anyway, ill look at it later.
    Last edited by Zeratulsdomain; 02-16-2006 at 04:12 PM.

  8. #23
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> So I guess ill just go with char's...

    As usual, you can use strings with tolower the same way you use char arrays. CornedBee was just referring to a more advanced one-line solution.

    However you solved it with char arrays, do the same with the string and you are done.

  9. #24
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    Quote Originally Posted by Daved
    >> So I guess ill just go with char's...

    As usual, you can use strings with tolower the same way you use char arrays. CornedBee was just referring to a more advanced one-line solution.

    However you solved it with char arrays, do the same with the string and you are done.

    What?

    I actually ditched pointer arrays (there goes a couple points).

    I am using string. But I am back in the same problem.

    My second program is now 100% done, with the exception of the capitalization.

    I am going to stat my excel work, if I finish early, how long would it take to figure this stuff out so I can capitalize the first letter in a variable?

  10. #25
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    If you know that the string is at least one character long, the easiest way to capitalize the first letter of it is
    Code:
    str[0] = toupper(str[0]);
    Now, to capitalize a whole string, there are two general approaches. One is to use Boost:
    Code:
    #include <boost/algorithm/string/case_conv.hpp>
    
    to_upper(str);
    The other is to capitalize each character individually. There are several methods here.
    Code:
    for(std::string::iterator it = str.begin(); it != str.end(); ++it) {
      *it = toupper(*it);
    }
    Code:
    std::transform(str.begin(), str.end(), str.begin(), toupper);
    ...

    Take your pick. The Boost method is definitely the most elegant, but you're probably not allowed to use external libraries.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #26
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    Quote Originally Posted by CornedBee
    If you know that the string is at least one character long, the easiest way to capitalize the first letter of it is
    Code:
    str[0] = toupper(str[0]);
    Using string I get this errro:
    error C2664: 'toupper' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'int
    Changing all strings to pointer arrays my program crashes when I run this (without giving me any error or warnings)
    *Noun[1] = toupper(*Noun[1]);
    Yes, I have two duplicates program with two different variable type.

  12. #27
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Why did you add the * and why did you change it from 0 to 1?

  13. #28
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    Quote Originally Posted by Daved
    Why did you add the * and why did you change it from 0 to 1?
    It is a pointer array.

    If I do not put "*" id get the whole "string" capitalized . But, if I do remove it, I get this error:
    Code:
     C:\Documents and Settings\chris\Desktop\Homework\c++\Assignment 2\temp\temp.cpp(52) : error C2664: 'toupper' : cannot convert parameter 1 from 'char *' to 'int'
    This whole function does not seem to work very well ;P

    O. and I put 1 (as opposed to 0) randomly, the value containing the array in my program is actually in a variable (I also put 1 to test it was not the variable), that pointer array:

    Code:
     char *Noun[5]


    Anyway, I am going to bed now (already took the NyQuil) so I will not see any replies until tomorrow (and tomorrow ill only spend 30 minute max on this, because I did not start my excel work yet).

  14. #29
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I'd suggest you pick strings or char arrays. Either will work, so pick one, then post the best code you can that shows the problem. toupper(Noun[1][0]) will capitalize the first character of the second string whether it is an array of char arrays or an array of strings. If that doesn't work, then post more code so we can understand what you are really doing.

  15. #30
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    Quote Originally Posted by Daved
    I'd suggest you pick strings or char arrays. Either will work, so pick one, then post the best code you can that shows the problem. toupper(Noun[1][0]) will capitalize the first character of the second string whether it is an array of char arrays or an array of strings. If that doesn't work, then post more code so we can understand what you are really doing.
    Noun[1][0] Also crashes.

    But, I got lucky with this program. The data that matters ends up in a array (dont know how I missed that yesterday). So I just had to add one extra line and now it is truly 100% done.

    Time for Excel now... Tnx for your help.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21