Hello :)

Sorry if i am wasting your time and i am sure there is a simple conclusion for this but anyhoo, heres me problem

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

string sName;

int main()
{
cout << "Hey what is your name? " << endl;
cin >> sName;
cout << "Hello " << sName << endl;
cout << "How are you" << endl;
cin >> sName;
cout << "Good" << endl;
cout << "dasdasdas" << endl;
cin >> sName;
cout << "dsadasdasd" << endl;
cin >> sName;
cout << "dasdasda" << endl;
cout << "dasdasasd" << endl;
cin >> sName;
cout << "dsffsdfsddf" << endl;
cout << "fsdfsdfsd" << endl;
cin >> sName;
cout << "fsdfsdfsdf" << endl;
return 0;
}


when i type something after hey what is your name, it just says everything ive got after cout like it doesnt give me time to type something again it just says all the output
i think some kind of pause code after cout
mm i dont really know how to explain it haha

no trollin' pls :)



No errors in your code. This should work as you expected.
Well sleep(seconds you want to sleep) causes a pause, but there aren't any errors in your code. I tried it and it worked fine for me.
closed account (2EyCpfjN)
Try taking what you've posted, copying and pasting it into your compiler, compiling it, and test it.
If it does not work (it just worked for me without changing any of your code), it may be a problem on your end.
What did you type in when this happened? Did the string you typed in have many spaces? More than 5?
closed account (2EyCpfjN)
What did you type in when this happened? Did the string you typed in have many spaces? More than 5?

Who's name needs more than 5 spaces? o.O
LOL :) thought brokenpunx perhaps typed in a random test string!

I'm curious - because only the characters up to the first space will show and, I'm not sure why exactly, but when you type in a string with multiple spaces it's as if the spaces are buffered or something and the subsequent inputs are bypassed!

So if you type in John Abc Xyz, it will just say Hello John (space so didn't show the rest) then not read in for "How are you" or "dasdasdas".

So if you type in "John Abc Xyz Pqr Stu Abc Xyz Abc" then you will get the result that brokenpunx had!

Perhaps someone know why this happens?
Last edited on
Because, as you suggested, input is (usually) buffered. When you type something, it goes to a buffer stored in memory which periodically gets flushed to the file associated with a program's standard input (stdin or std::cin) depending on what program you're trying to send input to. When you use std::cin's operator>>(), data can space-, tab- or newline separated.

If you read from standard input three times using std::cin >> str; and the user types in "Hello, I am John" the program will recieve "Hello,", "I" and "am" as sequential reads as opposed to one block of data.

use std::getline():
std::getline(std::cin, str);
closed account (2EyCpfjN)
@chrisname,
I never understood how, exactly,
std::getline(std::cin, str);
worked, can you explain?
it can be short-handed to
getline(cin, str);
right?
But even then, I don't understand how it works.
What is the difference between that and regular
cin >> ....;
Thanks chrisname :)
std::cin is an std::istream object associated with standard input. In the std::istream class (or, more probably, one of it's parent classes) operator >> is overloaded. It extracts a single block of characters which can either be all up to the first space, tab or newline or everything in the buffer if there's nothing there. Also, operator>>() will leave the last space, tab or newline in the buffer and only extract what's before it (which is why your program may skip inputs - it sees the whitespace and takes that to mean end-of-line ("EOF")). To counter this you can use std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') which will remove everything left in the buffer up to (and (probably) including) the last newline (or whatever you put instead of the '\n').

std::getline() puts all whitespace (including the newline) into the string you supply, so you don't have to do the std::cin.ignore() thing.

I prefer to use std::getline(). For dealing with integers you should use a stringstream, like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <sstream>
#include <string>

int main()
{
        std::stringstream sstrm;
        std::string str;
        int i;

        std::cout << "Enter a number: " << std::flush;
        std::getline(std::cin, str);
        sstrm << str;
        sstrm >> i;

        return 0;
}


And yes, you can use it without the std:: suffix if you put using namespace std;. However I personally don't advise this for several reasons.
closed account (2EyCpfjN)
chrisname, I honestly don't understand half of what you wrote. You don't need to explain it if you don't want to, but if you have the time, it'd be appreciated.

... Think "I'm explaining to a teenager, what would I have to say to get my point across?"
why? Because I am an aspiring teenager and I barely know what you mean by "overloaded" :p
I don't know what istream is.
I think i know what a buffer is. But I'm not sure.
Understand I am totally new to this.
And if you explain any of this, please also explain what a operator>>() is.

Haha, sorry for being so new :/
Sorry.

a pierce o wrote:
I barely know what you mean by "overloaded"

http://cplusplus.com/doc/tutorial/functions2/#function_overload

I don't know what istream is.

std::istream (input stream) is a class ( http://cplusplus.com/doc/tutorial/classes/ , http://cplusplus.com/doc/tutorial/classes2/ ) which allows you to access a file as a stream.
Wikipedia wrote:
In computing, the term stream is used in a number of ways, in all cases referring to a sequence of data elements made available over time. A stream can be thought of as a conveyor belt that allows items to be processed one at a time rather than in large batches.

So a stream sends you all the data in the file continuously. It actually doesn't (hence buffering) but we can pretend it does.

I think i know what a buffer is. But I'm not sure.

A buffer is just something used to store something else. You could think of a bucket as a buffer. When you put a bucket under a running tap, the bucket is storing the water. You could say it was "buffering" it.
Wikipedia wrote:
In computing, a buffer is a region of memory used to temporarily hold data while it is being moved from one place to another. Typically, the data is stored in a buffer as it is retrieved from an input device (such as a keyboard) or just before it is sent to an output device (such as a printer).


please also explain what a operator>>() is.

http://cplusplus.com/doc/tutorial/classes2/
Topic archived. No new replies allowed.