Like Tree4Likes
  • 3 Post By Salem
  • 1 Post By hk_mp5kpdw

strcmp with spaces

This is a discussion on strcmp with spaces within the C++ Programming forums, part of the General Programming Boards category; HI im trying to make a program where a user has to enter the department and the program will compare ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    1

    Post strcmp with spaces

    HI im trying to make a program where a user has to enter the department and the program will compare the department entered to redirect to its proper function.sadly some department have spaces in them which seems strcmp cant handle is there any other way? Here is a part of my code:
    Code:
    void database::eng(){
    system("cls");
    
    
    cout<<"______COLLEGE OF ENGINEERING______\n";       
    cout<<"__________________________________\n";       
    cout<<"which department?\n(case sensitive)\n";
    cout<<"Civil Engineering\n";char ce[]="Civil Engineering\0";
    cout<<"ECE and CpE\n"; char cp[]="ECE and CpE\n";
    cout<<"[Ba]ck\n";char b[]="Ba";
    cout<<"Choice: ";char input[20];
    cin>>input;
    if(strcmp(ce,input)==0){dep=ce;data.add();}
    if(strcmp(cp,input)==0){dep=cp;data.add();}
    if(strcmp(b,input)==0){data.college();}
    else { cout<<"Invalid choice!\nPress enter and re-enter choice";
                     getch(); data.eng();}
           }
    thank you for helping

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    It's not the strcmp with the issue, it's that cin >> var uses space as a separator.

    To read a string containing spaces, use getline().

    Also, you should really consider using std::string instead of char arrays.
    This gets you two very important things (at least)
    - total protection from buffer overruns (imagine someone typing 100 chars into your array of 20 - this is not good).
    - the ability to use == to compare strings (and not a function like strcmp)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    char ce[]="Civil Engineering\0";
    ...
    char cp[]="ECE and CpE\n";
    For the first, the \0 is not necessary.
    For the second, the \n is definitely not what you want in there.

    ...that said, I echo what Salem said about using std::string.
    iMalc likes this.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp fails with spaces
    By ThatDudeMan in forum C Programming
    Replies: 17
    Last Post: 11-07-2010, 04:03 PM
  2. Replies: 7
    Last Post: 10-03-2009, 11:58 PM
  3. strcmp()
    By norhos in forum C Programming
    Replies: 15
    Last Post: 03-15-2008, 10:15 AM
  4. C++ Map vs. strcmp
    By Scarvenger in forum C++ Programming
    Replies: 16
    Last Post: 09-15-2007, 12:24 PM
  5. strcmp help!!
    By apm in forum C Programming
    Replies: 14
    Last Post: 06-07-2007, 07:54 PM

Tags for this Thread


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