Need some help

This is a discussion on Need some help within the C++ Programming forums, part of the General Programming Boards category; I made this code that you type in your name and it replies back. I type in my name and ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    2

    Need some help

    I made this code that you type in your name and it replies back. I type in my name and it works, then I have to type in my last name and when I do and hit enter it closes. Can anyone fix this? Here is the code. This is a DOS program. Thanks!

    Code:
    #include <iostream.h>
    #include <string.h>
    int main()
    {
      char name[50];
      char lastname[50];
      cout<<"Enter your Name: ";
      cin.getline(name, 50, '\n');
    
      if(!strcmpi("Joey", name))
      {
        cout<<"Ha thats my name also."<<endl;
      }
      else
      {
         cout<<"Nope sorry thats not my name.";
      } 
       cout<<"This is your name in uppercase..."<<endl;
      strupr(name);
      cout<<name<<endl;               
      cout<<"And this is your name in lower case..."<<endl;
      strlwr(name);
      cout<<name<<endl;
      cout<<"Your name is "<<strlen(name)<<" letters long"<<endl;
    
      cout<<"Enter your lastname:";
    
      cin.getline(lastname, 50, '\n');
      strcat(name, " ");
      strcat(name, lastname);
      cout<<"Your full name is "<<name;
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    I havn't coded in DOS very much, but I beleive that its

    Code:
    system("PAUSE");
    so your program would look like
    Code:
    //---------------------------------------------------------------------------
    
    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>
    int main()
    {
      char name[50];
      char lastname[50];
      cout<<"Enter your Name: ";
      cin.getline(name, 50, '\n');
    
      if(!strcmpi("Joey", name))
      {
        cout<<"Ha thats my name also."<<endl;
      }
      else
      {
         cout<<"Nope sorry thats not my name.";
      } 
       cout<<"This is your name in uppercase..."<<endl;
      strupr(name);
      cout<<name<<endl;               
      cout<<"And this is your name in lower case..."<<endl;
      strlwr(name);
      cout<<name<<endl;
      cout<<"Your name is "<<strlen(name)<<" letters long"<<endl;
    
      cout<<"Enter your lastname:";
    
      cin.getline(lastname, 50, '\n');
      strcat(name, " ");
      strcat(name, lastname);
      cout<<"Your full name is "<<name << endl;
      system("PAUSE");
      return 0;
    }

    the endl; is so that the next line of text is on a new line, and the pause, well, pauses until you hit the "anykey"!

    Cheers

    Dw
    Last edited by Death_Wraith; 06-06-2004 at 03:33 PM.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    2
    Never mind it works! Thanks!
    Last edited by Ibenz; 06-06-2004 at 04:03 PM.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    That worked? Good to hear

    Cheers

    DW

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>
    You should note that these are deprecated. If you have a new enough compiler you should use iostream and cstdlib:

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    //...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

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