Hi! I am a newbie at C++ and would love to have some help in solving this problem.

Whenever I include spaces in the input, the whole thing loops endlessly, I can only write one word or a sentence with no spaces.

1
2
3
4
5
6
7
8
9
  {
    string input;
    cout << "Hi there! I am CALE (Chatbot At Library Education), what can I help you with?" << endl ;
    while (input=="exit" );
    {
     cin >> input ; 
    std::string sInput = ""; 
    cout << "Okay! your concern is noted, Please wait for our library admin to get in touch with you soon. Thank you!" << endl ; 
    }


I would love to ask for your help, and if you can, make it easier to understand. t-t, thank you!
Last edited on
When >> meets space character such as ' ' , '\t' ,'\n' and so on. It'll stop read what you input and execute the following statements. And if you input a sentence with spaces, the sentence will store in input buffer temporarily. And >> will continuously read the sentence,when it meets space,it will stop and execute next statement and read next word in sentence and meets the space and stop again and execute next statement......until the whole sentence is finished. So if you want to input a sentence with spaces, you can use getline() function.You can use like this----getline(cin,input);
By the way,there is a ';' after "while" statement, is that correct?
Last edited on
When using the >> operator to read a string it will first ignore any leading whitespace characters (spaces, tabs, line breaks, etc.) then it will read all characters until it finds the next newline character. So if the stream contains the content " hello heston!" it will ignore the spaces at the beginning and put "hello" in the string. The remaining content " heston!" is left in the stream and can be read by using >> again or other read operations.

As againtry mentioned, you can use std::getline to read whole lines. Note that this does not work so well in combination with >> (you might still want to use >> to read numbers and other things).

What you could do is to use std::getline to read each line and then use std::istringstream to extract the numbers and other things from the line using >> if you need to.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <sstream>
#include <string>

int main()
{
	int age;
	{
		std::cout << "Age: ";
		std::string line;
		std::getline(std::cin, line);
		std::istringstream iss(line);
		iss >> age;
	}
	
	std::string name;
	{
		std::cout << "Name: ";
		std::getline(std::cin, name);
	}
	
	std::cout << name << " is " << age << " years old.\n";
}

Another way is to use >> like normal and simply use std::ws (to ignore all leading whitespace characters) before every call to std::getline. This is simple to do but it doesn't allow the user to enter empty lines and any whitespace characters at the beginning of the line will not be part of the string. Often this is a good thing but not always.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <iomanip>
#include <string>

int main()
{
	std::cout << "Age: ";
	int age;
	std::cin >> age;
	
	std::cout << "Name: ";
	std::string name;
	std::getline(std::cin >> std::ws, name);
	
	std::cout << name << " is " << age << " years old.\n";
}

Note that
 
std::getline(std::cin >> std::ws, name);
means the same as
1
2
std::cin >> std::ws;
std::getline(std::cin, name);
Last edited on
Here is a reference to cin, getline() and ignore() - not to mention clear() which crops up too - to handle the otherwise uncontrolled 'black stream/screen of death'
https://www.tutorialspoint.com/what-is-the-use-of-cin-ignore-in-cplusplus
"operator >>"
There are actually many. That operator is a text-book example of function overloading.

1
2
3
4
int age;
std::string name;
std::cin >> age;
std::cin >> name;

What is common to these two operator >> is that both skip leading whitespace and both take as many characters of content as possible.

The difference is in what they accept as "content" and what they do with it.
Let say that stream contains:
  3.14\n

(Something that we see as whitespace, number, and newline.)
If we read to string, the whitespace is removed from string first, then "word" 3.14 is stored into the string and only the newline remains in the stream.
If we read to int, the whitespace is removed from string first, then value 3 is stored into the int and the stream still contains:
.14\n

So, the operator >> for int stops not only on whitespace, but also on any character that does not look like integer. (Note: 0xF3 is an integer.)

What happens if you try to read non-int into int? For example, the
.14\n

The operator >> for int can't read the . and therefore can't read anything and it will set the stream into failed state.
All read operations, both >> and getline(), will automatically fail when stream is in failed state. The clear() can clear that state.
Last edited on
Good afternoon to everyone,

I would like to thank you for imparting your knowledge to me, I really appreciate it! I shall try your suggestions and come back with the results, I like this community, you are all so helpful. Cheers!

Sincerely,
Your freshman coder.
Hi guys, update!

I fixed it! With all your help, though it is a simple problem for you guys, it was a huge one for me. I appreciate all of you. :DD I know that we all probably would never meet in real life, but take with you the fact that you all helped a freshman such as me to slowly cruise my journey through college. May you all be healthy and your days be fruitful, thank you.

Sincerely,
Hesh
Topic archived. No new replies allowed.