iostream

Input Validation to make sure only number c++

安稳与你 提交于 2019-11-29 16:15:10
Ok, I'm trying to get good at using pointers so I'm trying to write a input validation for the user input to make sure that anything that isn't a number is handled correctly. When I use isdigit() isn't working for me. I still get an exception when I enter a alphabet. Any suggestions? Thanks. Check this out: #include<iostream> #include<algorithm> #include<string> #include<cctype> using namespace std; void EnterNumbers(int * , int); int main() { int input = 0; int *myArray; cout << "Please enter the number of test scores\n\n"; cin >> input; //Allocate Array myArray = new int[input]; EnterNumbers

How to read from std::cin until the end of the stream?

亡梦爱人 提交于 2019-11-29 15:47:48
My problem is, that I want to read the input from std::cin but don't know how long the input is. Also I have to char and can't use std::string . There are two ways I have to handle: a) The user inputs text and when he hits [ENTER] the program stops reading. b) The user redirects std::cin to a file (like .\a.oput < file ) which can hold multiple lines. Edit: Just noticed that std::cin.eof() is always false also in the case of reading form a file. For a) I could read until \n occures. For b) Edit: No (I could read until std::cin.eof() occures.) But when I don't know whether I'm getting an a) or

Misunderstanding about ostream class and operator <<

末鹿安然 提交于 2019-11-29 13:43:31
After looking at the ostream::operator << c++ reference, I noticed the following declarations: ostream& operator<< (bool val); ostream& operator<< (short val); ostream& operator<< (unsigned short val); ostream& operator<< (int val); ostream& operator<< (unsigned int val); ostream& operator<< (long val); ostream& operator<< (unsigned long val); ostream& operator<< (float val); ostream& operator<< (double val); ostream& operator<< (long double val); ostream& operator<< (void* val); ostream& operator<< (streambuf* sb ); ostream& operator<< (ostream& (*pf)(ostream&)); ostream& operator<< (ios& (

Resetting the End of file state of a ifstream object in C++

假如想象 提交于 2019-11-29 13:26:43
I was wondering if there was a way to reset the eof state in C++? For a file, you can just seek to any position. For example, to rewind to the beginning: std::ifstream infile("hello.txt"); while (infile.read(...)) { /*...*/ } // etc etc infile.clear(); // clear fail and eof bits infile.seekg(0, std::ios::beg); // back to the start! If you already read past the end, you have to reset the error flags with clear() as @Jerry Coffin suggests. Presumably you mean on an iostream. In this case, the stream's clear() should do the job. I agree with the answer above, but ran into this same issue tonight.

C++: assign cin to an ifstream variable?

萝らか妹 提交于 2019-11-29 13:09:46
You know the common stdio idiom that stdin is specified by a filename of "-", e.g. if ((strcmp(fname, "-")) fp = fopen(fname); else fp = stdin; What's the best way to do this with an ifstream instance? I've received a bit of code that has an ifstream as part of a class and I'd like to add code to do the equivalent, something like: if ( filename == "-") logstream = cin; // **how do I do this*?* else logstream.open( filename.c_str() ); cin is not an ifstream , but if you can use istream instead, then you're in to win. Otherwise, if you're prepared to be non-portable, just open /dev/stdin or /dev

Why does string extraction from a stream set the eof bit?

大憨熊 提交于 2019-11-29 13:04:34
问题 Let's say we have a stream containing simply: hello Note that there's no extra \n at the end like there often is in a text file. Now, the following simple code shows that the eof bit is set on the stream after extracting a single std::string . int main(int argc, const char* argv[]) { std::stringstream ss("hello"); std::string result; ss >> result; std::cout << ss.eof() << std::endl; // Outputs 1 return 0; } However, I can't see why this would happen according to the standard (I'm reading C+

Why two EOF needed as input? [duplicate]

蹲街弑〆低调 提交于 2019-11-29 12:22:25
This question already has an answer here: Canonical vs. non-canonical terminal input 1 answer When I run the code below, I use three inputs (in Ubuntu terminal): abc(Ctrl+D)(Ctrl+D) abc(Ctrl+D)(Enter)(Ctrl+D) abc(Enter)(Ctrl+D) The code reacts well in all cases. My question is: why in 1) and 2) I need two EOF? #include <iostream> int main() { int character; while((character=std::cin.get())!=EOF){} std::cout << std::endl << character << std::endl; } That's how the "EOF" character works (in "canonical" mode input, which is the default). It's actually never sent to the application, so it would be

Program is skipping over Getline() without taking user input [duplicate]

偶尔善良 提交于 2019-11-29 11:56:56
This question already has an answer here: Why does std::getline() skip input after a formatted extraction? 3 answers This is a very strange problem, when my program asks the user for the address, instead of waiting for input, it seems to skip the getline() function completely Answerinput: cout << "would you like to add another entry to the archive? (Y/N):"; cin >> answer; cout << endl; cout << endl; answer = toupper(answer); switch(answer) { case 'Y': Entrynumber++; cout << "began record number " << Entrynumber << "+ 1." << endl; cout << "Enter the last name of the person to be entered" <<

No matching constructor for initalization of 'ostream_iterator<int>'

人盡茶涼 提交于 2019-11-29 10:52:31
for the code, why error, osteam_iterator is a template class ,why no matching constructor for initalization of 'ostream_iterator', please give a help , thank you. define ostream_iterator template > class _LIBCPP_VISIBLE ostream_iterator int main(int argc, const char * argv[]) { vector<int> sentence1; sentence1.reserve(5);// 设置每次分配内存的大小 sentence1.push_back(1); sentence1.push_back(2); sentence1.push_back(3); sentence1.push_back(4); sentence1.push_back(5); int c = 5; copy(sentence1.begin(), sentence1.end(), ostream_iterator<int>(cout, 1)); cout << endl; The ostream_iterator class definition looks

stringstream temporary ostream return problem

倖福魔咒の 提交于 2019-11-29 09:39:45
问题 I'm creating a logger with the following sections: // #define LOG(x) // for release mode #define LOG(x) log(x) log(const string& str); log(const ostream& str); With the idea to do: LOG("Test"); LOG(string("Testing") + " 123"); stringstream s; LOG(s << "Testing" << 1 << "two" << 3); This all works as intended, but when I do: LOG(stringstream() << "Testing" << 1 << "two" << 3); It does not work: void log(const ostream& os) { std::streambuf* buf = os.rdbuf(); if( buf && typeid(*buf) == typeid