Thread: Cant use as function...?

  1. #31
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    K well how do i get it to read out 1 char at a time with strings

  2. #32
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Nathan the noob View Post
    K well how do i get it to read out 1 char at a time with strings
    What do you think "read out 1 char at a time" means?

  3. #33
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    it means that it reads out each letter one at a time but i cant use printf with strings is it the same for cout

  4. #34
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    If I can offer a peice of constructive critisism to your overall code style..

    The way it generally looks is a bit strange, as you are mixing C and C++ code together to create what im gathering is a text adventure game.

    General practice resides in suggestion never mix to languages. It is either going to be C or C++ not both. C++ has backwards compatabiliy with C hence why the C style techniques work on your C++ compiler.

    I would suggest looking at you want to acheive, then setting up a sense of structure first and for most. The reason you are running into so many problems is due to mainly throwing new ideas onto code, there is nothing wrong with this as it proves you are willing to learn new things and add them to existing code, which is what programming is all about.

    The very reason I and others have introduced you to classes is you seem to understand the logic of fundemental OO design theorys, or seem to want to understand it. Here is a step plan to what I suggest you take a look at doing.

    #1 Pick the language you want to learn, C or C++ for this project and try to stick with it

    #2 If it is C++, I would seriously consider doing the following:

    Learn more about OO design, even very basic classes. For example, do you understand this:

    Code:
    class Cat {
       public:
          Cat():  m_Name("") {}
    
          std::string returnName() { return m_Name; }
          void setName() { m_Name = "Felix"; }
    
       private:
          std::string m_Name;
    };
    Once you have understood basic OO design, you can begin creating your player class as you have been doing, with a lot more knowledge on how it all works. Tabstop very righty pointed out about the std::string issue, and if you do want to use C++ then character arrays to represent the names of players, enemys or items should be avoided. A std::string has a lot of funtions built in such as size(), strrev() all helpful to you.

    I would also advise sketching out a rough idea about what you want each function of the code to do, before you code it, that way, you have plan already installed. If you have a book on C++ read it and see what tips you can pick up, or even look at the online tutorials, if you find them useful. Of course, still ask questions here when you get stuck, but I think the main problem you are running into is trying to learn two quite different languages at once.
    Double Helix STL

  5. #35
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Ive been learning from a 2008 book on c++ called absolute c++ i dont no how most my code is c :P guess it just happened. in that piece of code u have their you made the class Cat then under publici u gave cat the member n_Name then u got the return value for it and set the name to felix and then under private u got the name? But grrrr hmmm.

    can you show me how i would impliment string[maxsize] then find size of string and print out the string 1 character at a time.

  6. #36
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Nathan the noob View Post
    it means that it reads out each letter one at a time but i cant use printf with strings is it the same for cout
    You don't printf one character at a time either so I'm not sure exactly what your question is. You won't see one character at a time anyway unless you get very fancy about how the output is buffered.

  7. #37
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    With the printf it did print 1 char at a time :P

    it went through first got size of the char[] and then had a for loop were x = 0 and the loop ran until x !> size and well this was happening printf was printing 1 char out everytime
    Last edited by Nathan the noob; 02-17-2009 at 05:57 PM.

  8. #38
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    The only way you can do it is by using the size() if I am thinking you want this:

    T wait H wait E wait N

    Where "wait" is a time delay?
    Double Helix STL

  9. #39
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    ya but with strings not chars it works differently for some reason or at least doesnt work with printf

  10. #40
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    The easiest way to acheive it is to make use of the size() function as I said.

    Think about how you would code it in printf. You need a loop to iterate through the size of the string untill it reaches the end, then I guess you could use Sleep() to act as the wait period for a quick fix.
    Double Helix STL

  11. #41
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    hmm but wat do i use to make it read out 1 char at a time or does size acomplish that for me i already figured out how to do the rest
    Code:
    size=size(string);??
    for(x=0;x<size;x++)
    {
         Sleep(40)
         cout << string?;
    }
    string.length() < does that acomplish samething
    Last edited by Nathan the noob; 02-17-2009 at 06:12 PM.

  12. #42
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Your on the right track.

    You do not need strlen(), you only need size(). You need to understand how string functions work. You seem to again be contenplating the C style of doing things.

    Code:
     std::string theWord = ""; // do not need an array at all
    
    std::cout << "Enter a word to print 1 character at a time: ";
    std::cin >> theWord;
    
    for ( int i = 0; i < theWord.size(); i++ ) {
            std::cout << theWord[ i ] << "";
            Sleep(1000);
        }
    May accomplish what you need. What you have to understand and remember, is a "string" is a collection of characters. The clever thing about the string object is it doesnt need a fixed size to use it. But, as shown above, you can still use it as an array with the array subscript [] as you would do in C or regular array.

    I do not know why your book is teaching you C strings over C++.
    Double Helix STL

  13. #43
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Maby im over thinking stuff.

    I was unaware that it was teaching me c to lol well i also get help from other people and code snippets i got printf from code snippets cause it printed out 1 char at a time thats why i used it :P

  14. #44
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    And yes It accomplishes wat i need just a little different 1 less line to right....

    Il have to read up some more in my book and hurry up and get
    to the class chapter tho :P classes are quite helpful in cutting down code



    Wait!!! i found a bug with that formula...

    wats a way to get input were it doesnt skip blank spaces cause the program ends the for loop if theirs a blank space
    Last edited by Nathan the noob; 02-17-2009 at 06:29 PM.

  15. #45
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Do not rush the book. Take is slow, if you get to pointers before classes than it will take you a while to understand how pointers work, and they are very useful in more complex classes.

    It is better to have a solid understanding of functional or precedrual programming first, so when you really get your teeth into objects and classes, the theorys of function calling and organisation will not be as daunting. If I were you I would probaby get a better book too if you feel its a load of rubbish teaching you old outdated styles of coding.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM