<space> delimiter in string class causes program to crash when " " charachter is ent

This is a discussion on <space> delimiter in string class causes program to crash when " " charachter is ent within the C++ Programming forums, part of the General Programming Boards category; I am trying to run a standard win32 console application, e.g. simple "Hello World" style app just to test this ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    11

    Question <space> delimiter in string class causes program to crash when " " charachter is ent

    I am trying to run a standard win32 console application, e.g. simple "Hello World" style app just to test this stupid "bug" or whatever it is.

    Does anyone know how to use the cin >> statement to store a " " character into a string variable

    e.g.

    #include <iostream>
    #include <string>
    using namespace std;

    void main ()
    {
    string somestring;
    cin >> somestring;
    cout << somestring;
    char somechar;
    cin >> somechar;
    }

    when the cin statement asks for text it will go fine unless a space is entered into the string. When a space is present hitting enter after the string crashes the program and it does not even run the cout statement. There is no error, it just exits. I tried it with a character array too and it did the same thing.

    I am using Microsoft Visual Studio.net through my school and it is a horrible program. I see source code all the time where this problem isn't an issue, should i switch compilers?
    Any Suggestions?
    --Cid666

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    use int main()
    not void
    !
    try cin.getline()
    instead of cin>>
    !
    try msvc++6
    ?

  3. #3
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    It doesn't crash, it just exits main() normally after reading one string and one char.

    cin >> stops reading at a whitespace.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  4. Runtime error in Class, string based program.... plz help
    By PotitKing in forum C++ Programming
    Replies: 2
    Last Post: 12-24-2001, 08:15 AM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM

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