Thread: Two string questions

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    20

    Two string questions

    Two string questions (not char)

    1# When the user type in data when I cout how would I go about running code between each and every change? (ie, if they type h-e-l-l-o, it would run at the h, then the he, and the hel, etc, I also want to have that value in the variable)

    That way, I can cout data to other parts of the screen when they type[e in something specific (without them having to hit enter while also let them keep editing the string),

    2# using string variables. What site would you recommend which has all the string manipulations? For examples in vb there is mid(), left(), right(), len() etc I know some of them (and can find some of this on google), but I am looking for a good recommended reference site.

    I would really appreciate help.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    what comes to number 2, this site seems promising: http://www.cppreference.com

  3. #3
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    1. Try reading in each character individually and running code after each read, something like
    Code:
    while(wordnotdone)
    {
      cin.get(c);
      s += c;
      DoFunction();
    }
    2. I quite like this page

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    1. Not possible in a portable way. The problem is that the moment your program asks the console controller for a character, the controller is free to wait as long as it wants to actually return it. And on both Linux and Windows, the default setting is for the controller to return immediately if something is cached, but otherwise wait for the user to hit enter.
    This behaviour can be changed in platform-dependent ways. I think the FAQ lists the way for Windows.

    2. C++ std::string, or C-style char*? And what is "all"?
    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

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    Lets say I want to add text as the user is writing it... How would I do that?

    example: phone number: 5551212
    and I format it so it injects a "-" as the user types it?
    Ex: After the third character entered it throws a - in there.

    Also on a side note, I looked over some of the links, but it looks like its better to use char with strings then actually using string... Am I right?

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Also on a side note, I looked over some of the links, but it looks like its better to use char with strings then actually using string... Am I right?
    Nope. It's much easier to use the C++ string type. Go ahead and use char arrays if you think they look easier, but you will soon find out otherwise.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> it looks like its better to use char with strings then actually using string... Am I right?

    If you are talking about char arrays versus C++ strings, then in general, no. It is better to use the string class. Some of the functions in the FAQ that you'll need to accomplish what you are trying to do deal with a single character at a time. So what you should do is add each character to your string as you read them in.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Lets say I want to add text as the user is writing it... How would I do that?

    example: phone number: 5551212
    and I format it so it injects a "-" as the user types it?
    What purpose would that serve?
    Also on a side note, I looked over some of the links, but it looks like its better to use char with strings then actually using string... Am I right?
    Nope. It's much easier to use the C++ string type. Go ahead and use char arrays if you think they look easier, but you will soon find out otherwise. For instance, suppose you want to combine two strings:
    Code:
    #include <iostream> 
    #include <cstring> //strcat()
    #include <string>
    using namespace std;
    
    int main()
    {
    	//char arrays:
    	char str1[] = "hello";
    	char str2[] = " world!";
    
    	const int combined_size = (sizeof str1/sizeof str1[0]) + (sizeof str2/sizeof str2[0]) + 1;
    
    	char combined[combined_size] = {0};
    	strcat(combined, str1);
    	strcat(combined, str2);
    	cout<<combined<<endl;
    
    	//string type:
    	string str3 = "goodbye";
    	string str4 = " world!";
    
    	string combined2 = str3 + str4;
    	cout<<combined2<<endl;
    	
    	return 0;
    }
    Which do you think is easier?
    Last edited by 7stud; 02-15-2006 at 11:44 AM.

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    wow... last time it took 2 hours to get a reply :P tnx for the replies.

    Anyway, I finally found a way 2 do this (I did not think I was going to get a reply so fast)
    Code:
    int main ()
    {
    
    	int Counter = 0, x = 0;
    	char c, d[8];
    	
    
    	do 
    	{
    		c=getch();
    		putchar (c);
    		Counter++;
    
    		
    		d[x] = c;
    		x++;
    		
    		if (Counter == 3) 
    		{
    			putchar ('-');
    			
    			d[x] = '-';
    			x++;
    		}
    
    //		if (c == '\b')
    //			gotoxy(2,1);
    	
    	} while (Counter != 7);
      
    	d[x] = '\0';
    	cout << "\n" << d;
    	return 0;
    }}}
    7stud you example does make string look easier. But does it have the power? I mean, if you look at the code above, can it do that? I still also have to make sure the user can backspace (will need to use gotoxy (hence keep track of where I am at all times, and also derease my conuter))... And still got like one other big things 2 do.... I would not even know where to start if I were to use string.h

    ---
    I also will need to learn how to capitalize letters (ie if someone input there name, I want to capitalize the first letter (this can be done after it stored in a variable though)), but I could probably find that on google (or these boards) when I need 2 do it.


    --

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    To use the string class instead of a character array, you would remove the declaration of d[8] and add string d(8); to the line below. Everything else should just work. There are also other ways of using the string class that might be even easier, like using the += operator to add each character to the string.

    I'm not sure what the difference between x and Counter is, but if they backspace you can just decrement the counter. Also, toupper is a function that converts a character to upper case.

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    I am having lots of trouble...

    C++ is a nightmare when it comes to this stuff

    ... If I were to restart everything... How would I go about doing the 555-1212 stuff using cin with string?

    I want to use cin, problem is I still don’t know how to add the "-" while the user is in "cin"...

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    How would I go about doing the 555-1212 stuff using cin with string?
    You can treat a string type as if it were a char array, so there would be no difference in your code. Changing the type from a char array to a string is not going to help you alter what the user types in and display it in real time.

    You still have not stated why you want to add dashes in real time while the user types in their phone number. What purpose does that serve?
    Last edited by 7stud; 02-15-2006 at 02:10 PM.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I still don’t know how to add the "-" while the user is in "cin"...

    You can't. You have to use getch() or some other non-standard method.

  14. #14
    Registered User
    Join Date
    Oct 2005
    Posts
    20
    7stud: It is part of the guidelines I need to follow.


    Daved: So you cannot use getch and cin() together?

    Using getch() to put data in variables is the only way to put string

    Code:
    	
    cout << endl << endl << d << "-" << endl << dd << "-" << endl;
    if (dd == d)
    cout << "hey";
    They both show up exactly the same in the cout, but "hey" is never outputted. If I compare it individually (ie d[0] and dd[0] it will say "hey"...

    edit: I added the "-" at the end to make sure there is no hidden spaces.

    edit; ah, you need to use strcmp (arg, there went a other hour wasted)
    Last edited by Zeratulsdomain; 02-15-2006 at 03:08 PM.

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    edit; ah, you need to use strcmp (arg, there went a other hour wasted)
    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.

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, 10: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