Thread: strings

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    465

    strings

    I've been reading more of the tutorials.

    Code:
    #include <iostream> //For cout
    #include <cstring>  //For the string functions
    
    using namespace std;
    
    int main()
    {
      char name[50];
      char lastname[50];
      char fullname[100]; // Big enough to hold both name and lastname
      
      cout<<"Please enter your name: ";
      cin.getline ( name, 50 );
      if ( strcmp ( name, "Julienne" ) == 0 ) // Equal strings
        cout<<"That's my name too.\n";
      else                                    // Not equal
        cout<<"That's not my name.\n";
      // Find the length of your name
      cout<<"Your name is "<< strlen ( name ) <<" letters long\n";
      cout<<"Enter your last name: ";
      cin.getline ( lastname, 50 );
      fullname[0] = '\0';            // strcat searches for '\0' to cat after
      strcat ( fullname, name );     // Copy name into full name
      strcat ( fullname, " " );      // We want to separate the names by a space
      strcat ( fullname, lastname ); // Copy lastname onto the end of fullname
      cout<<"Your full name is "<< fullname <<"\n";
      cin.get();
    }
    Could someone please explain to me what this line of code is for:

    Code:
    fullname[0] = '\0';
    My computer is awesome.

  2. #2
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    It guarantees that fullname is a valid null terminated string. If it isn't then strcat will search through memory until it finds '\0', which could potentially exceed the boundaries of the array.
    Kampai!

  3. #3
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    strcat() appends a string to the end of an existing string, so it looks for the null terminating character, and overwrites that null character with the first element of the new string that it appends, then terminates the new appended string with a '\0'.

    fullname[0] = '/0' ensures the null terminating character is the first element of the string, which effectively clears the string.

    Alternatively, in that above example, the string name could have been placed in fullname with strcpy(oldstring, newstring) which would have copied name into fullname beginning with the first element regardless of what was already there.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
     int lol=rand()%3;
     char name[50];
     
     cout<<"Enter your name.";
     cin.getline (name, 50 );
     
     if (lol==1 && strcmp (name, "tim")==0 )
        cout<<"really?";
        
     if (lol==2 && strcmp (name, "tim")==0 )
        cout<<"your my friend";
        
     if (lol==3 && strcmp (name, "tim")==0 )
        cout<<"no one cares";
     
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    I'm trying to have the person type their name in then have it pick
    one of the three responses, but each tiem it displays your my friend.
    I've tried putting lol==1 in different places.
    My computer is awesome.

  5. #5
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    try reading this FAQ on random numbers and see if you find the answers you want.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Alright I wondered why it wasn't working I forgot about srand. Edit: nevermind

    Is there any way I can put that in a switch?
    My computer is awesome.

  7. #7
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Post the updated code please.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Quote Originally Posted by cerin
    Edit: nevermind

    Is there any way I can put that in a switch?
    I just didn't do it enough times...
    My computer is awesome.

  9. #9
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    There's a logic error in this code
    Code:
    int lol=rand()%3;
    Think about what three possible values that statement will provide, and what three values you are comparing against.

    And yes, you can place it in a switch. Switch handles chars and ints.

  10. #10
    Banned
    Join Date
    Oct 2004
    Posts
    250
    There's a error in your program,
    Code:
    int lol=rand()%3;
    generates a random number between '0' and 3 so if the number ganerated is 0 your going to have a problem.

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Would it look like:

    Code:
      switch(lol)(
      case '1':
      cout<<"you suck";
      break;
    What would I put in order for it to check two conditions?

    Code:
      switch(lol, name){
      case 1 && tim:
      cout<<"you suck"
      break;
    Please explain how I would do it.
    My computer is awesome.

  12. #12
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Either nested cases or an if statement in there somewhere. Please post your code and explain more clearly what you are trying to do.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  13. #13
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Quote Originally Posted by cgod
    Code:
    int lol=rand()%3;
    generates a random number between '0' and 3 so if the number ganerated is 0 your going to have a problem.
    Wow, two seperate messages on this topic, and both are wrong....

    rand() % 3 will generate a pseudo random number in the range 0...2.

  14. #14
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    I have a lot of problems.
    code:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
     srand ((unsigned int)time(0));
     int lol=rand()%3;
     char name[50];
     
     cout<<"Enter your name.";
     cin.getline (name, 50 );
     switch(lol,name){
     case ('1,"tim");
        cout<<"really?";
        
     case ('2',"tim");
        cout<<"your my friend";
        
     case ('3',"tim");
        cout<<"no one cares";
     
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Errors:
    Missing terminating char
    [warning]char constant too long for its type
    in function 'int main(int,char**)
    syntax error before '<<' token
    syntax error before ';' token
    syntax error at end of input

    Care to help out?
    My computer is awesome.

  15. #15
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I imagine most of your problems are coming from your switch/case statement. The semicolons after your case statements should be colons. And then there's the arguments used in your switch/case block. Unless I'm really missing something about STL strings, you can't use them in switch/case statements like that - you have to use an integer value. And I've never seen more than one argument used at a time. Are you sure you can do that? I'm pretty sure you can't.

    edit: And don't forget to break out of each case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM