Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

thinkpad20

  • 4 years ago

Help with entering a string that includes spaces in C++. I'm doing the first problem of problem set 1 for Stanford CS106B. The goal is to remove all instances of a target string from a source string and return the source. Here's the code I have: http://pastebin.com/4YuNADaa It works for all "solid" strings but crashes when I put something in with spaces. Any help?

  • This Question is Open
  1. shahzadjalbani
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Yes you must write gets(variable); to input spaces as well .

  2. thinkpad20
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I don't understand what you mean. Is gets a function? Perhaps you could show me with code.

  3. shahzadjalbani
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    I want to say this: #include<iostream.h> #inlcude<conio.h> #inlucde<conio.h> int main() { char name[20]; gets(name); cout<<"\n"<<name; return 0; getch(); }

  4. thinkpad20
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Hmm, so we're using the char type... any reason for that? Can I use the same find functions, etc for a char that I can for a string? Or might I be able to convert the char to a string? I'm new to C++.

  5. shahzadjalbani
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Yes you can use this to string as well.

  6. thinkpad20
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    OK, I used the string() function and it seems I'm done! Thanks a bunch! Seems like an unnecessary step... but I'm used to Python, hehe. I'm sure this way has its reasons/advantages. Anyway, thanks again.

  7. thinkpad20
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ^ I mean to say I combined what you suggested to get the character input and then put that in the string function to convert to a string ;)

  8. shahzadjalbani
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 1

    I need your help in python.

  9. bmp
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Only avoid using gets(). Such a bad function for generic I/O. Use fgets() instead. Read more about it here: http://www.gidnetwork.com/b-56.html

  10. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy