My simple program isn't working right....

When I make a string to hold a set of characters in and prompt the user with an extraction operator, A.K.A. cin >> whatever the program freaks out when I enter some thing with spaces, or acts weird. How do I stop that?

Like on prompt, I check to be certain the program is stable under any possible user circumstances or choices so I know the program is stable and has good logic and control meaning a good interface.

A bad interface would be a program that fails to do some thing just because there's a space in it.

So how can I end the "space" problem when entering characters in a string?

And is my question asked correct?
Last edited on
Hello again, spoon licker.

Well one thing you can do is make a simple function like this:

1
2
3
4
5
6
7
8
9
10
11
12
string GetAllCin() {
  string sendbuf = "";
  string tempstr = "";
  do {
    if(cin.peek() == ' ')
      sendbuf += " ";
    cin >> tempstr;
    sendbuf += tempstr;
  }
  while(cin.peek() == ' ');
  return sendbuf;
}


which will get all input until the user presses enter.

And yes, your question is asked correctly =)
umm... or just use getline:

1
2
string foo;
getline(cin,foo);
meh, who likes easy solutions? =)
But that will only help with the spaces, and I'll have to assure that a string compares to an exact comparison for it to result true.

That's still leaving it complicating on the par that a user can input any thing they wish(with as much or little space possible, ultimately making it a pain to be able to catch all possible entries with spaces or not)and that must be understood logically and handled by the program. I'm sorry if you don't get it.....

I tried. I know every thing can't be handled, too many choices, but I want my program to handle things that are input logically(know what to do if what you entered is incorrect, not upper/lower case or not a fitting answer)rather than to just quit and take the easy way.

Most people just prompt some thing, and if you enter some thing wrong they give the logic for it to just quit rather than to handle the wrong input and clarify it(correct it, let the user know it's wrong, etc).

Sorry if you don't understand....I tried. :(

Please don't tell me I'm dumb for wanting to make a program like this.
Last edited on
So are you talking about different (edit) data types? Like integers and other special chars?
Last edited on
No, that's not what I meant.

I want to be able to program the program to handle spaces and treat them as would with out them so if the user inputs some thing that may not be exactly fitting for one possibility then it will be able to catch a differently input string.

For example:

"What is your name?"

Jessica.

"Your name is Jessica."

------------------------------
"What is your name?"

jessica.

"Your name is jessica."(WRONG! A name must be capitalized. Error must be treated.)

------------------------------
"What is your name?"

JeSs i C a

"Your name is JeSs i C a."(Wrong again! It should be able to catch this error that spaces don't go between letters in your name.)

Do you get my point?

If left untreated, the program will either panic/freeze or lose focus if it isn't given the logic to work around such possible errors a user may input and treat them the right way.

Understood now?
Last edited on
Okay, I think I may understand. You are actually talking about a common programming question.

http://en.wikipedia.org/wiki/Parsing_expression_grammar
UGHHH. No...Just forget it. Delete the whole question....No one understands.
Last edited on
Spoon licker was talking about recovering from an error.
Which is pure logic. Ya, I got it. It's not an error with reading in the strings. It's an error in handling it.
That's what I meant. Any tips on how to program better for error handling?
Topic archived. No new replies allowed.