iostream

Why does std::getline() skip input after a formatted extraction?

落花浮王杯 提交于 2020-01-28 10:16:40
问题 I have the following piece of code that prompts the user for their name and state: #include <iostream> #include <string> int main() { std::string name; std::string state; if (std::cin >> name && std::getline(std::cin, state)) { std::cout << "Your name is " << name << " and you live in " << state; } } What I find is that the name has been successfully extracted, but not the state. Here is the input and resulting output: Input: "John" "New Hampshire" Output: "Your name is John and you live in "

Remove memory from the middle of a file

不羁的心 提交于 2020-01-26 04:34:13
问题 I have a binary format which is build up like that: magic number name size blob name size blob name size blob ... it is build up to easy move through the file and find the right entry. But I would like also to remove an entry (let's call it a chunk as it is one). I guess I can use std::copy/memmove with some iostream iterators to move the chunks behind the one to delete and copy them over the chunk to delete. But then I have the space I deleted at the end filled with unusable data(I could

Looking to round the final answer to 2 decimals C++

余生长醉 提交于 2020-01-25 05:07:10
问题 I'm trying to round my final answer to 2 decimals so it is Dollars and Cents. I'm new to coding, and can't figure it out. I want to round "w" in the line that says "The amount you need to charge is" Here's my code: #include <iostream> using namespace std; int main() { string Choice; float x, w; cout << "Please enter the amount needed." << endl; cin >> x; w = x/(1-0.0275); cout << "The amount you need to charge is $"<< w << "." << endl; return (0); } 回答1: According to the example here http:/

Why does iostream take so much flash space on an MCU?

时间秒杀一切 提交于 2020-01-24 19:34:06
问题 I use GCC 5.2.0 to compile code for an EFM32 MCU (based on a Cortex-M core). I notice an awful increase in code size when I want to #include <iostream> . For example, let's compile the following code for an EFM32WG "Wonder Gecko" chip: #include "em_device.h" #include "em_chip.h" #include <iostream> int main(void) { CHIP_Init(); while (1) { } } This code will result in 172048 bytes of code, whereas without #include <iostream> it is only 1440 bytes. I usually just use cout for debug output (by

Why does cout.precision() affect the whole stream?

时光怂恿深爱的人放手 提交于 2020-01-24 16:33:45
问题 I feel I'm asking a very basic question, but I haven't been able to find an answer here or in Google. I recall we were taught this at school, but alas it has vanished over years. Why does cout.precision() ( std::ios_base::precision() ) affect the whole stream when called in the middle of the output list? I know the rule that std::setprecision() should be used to change precision on the fly, and that cout.precision() is going to spoil the output with the value it returns. But what is the

C++ io streams versus mmap

*爱你&永不变心* 提交于 2020-01-24 12:32:13
问题 I am starting a small project for a key-value store, in C++. I am wondering how C++ std streams compare to mmap in terms of scalability and performance. How does using ifstream::seekg on a file that wouldn't fit in RAM compare to using mmap/lseek? 回答1: Ultimately, any Linux user-land application is using syscalls(2), including the C++ I/O library. With great care , mmap and madvise (or lseek + read & posix_fadvise ) could be more efficient that C++ streams (which are using read and other

Resetting output flags in C++

你离开我真会死。 提交于 2020-01-24 11:19:10
问题 I'm intending to reset all output flags to default on the lines where I end using the resetiosflags function. It provides erroneous output when I attempt to do it in this manner, contrary to my expectations. #include <iostream> #include <iomanip> using namespace std; int main() { bool first; int second; long third; float fourth; float fifth; double sixth; cout << "Enter bool, int, long, float, float, and double values: "; cin >> first >> second >> third >> fourth >> fifth >> sixth; cout <<

Outputting cerr using cout

◇◆丶佛笑我妖孽 提交于 2020-01-24 02:53:06
问题 I came across a piece of code that does basically the following: #include <iostream> using namespace std; int main() { cout << cerr << " Hi."; return 0; } Output: 0x601088 Hi. First of all, why would anyone do 'cout << cerr' it does not make sense. Second of all, what is the meaning of the output above? Worth to mention that on my machine the above code compiles and executes without errors. However a much more complex code (doing the same thing as above) on a different machine (server ssh

Printing a pointer with <iostream> [duplicate]

荒凉一梦 提交于 2020-01-23 05:58:06
问题 This question already has answers here : cout << with char* argument prints string, not pointer value (6 answers) Closed 2 years ago . Why does #include <iostream> using namespace std; int main() { cout << (char*)0x10 << endl; } segfault, but #include <iostream> using namespace std; int main() { cout << (void*)0x10 << endl; } seems to work just fine? 回答1: Because cout::operator <<(void*) prints a memory address, and cout::operator <<(char*) prints a null-terminated character array, and you

Printing a pointer with <iostream> [duplicate]

旧城冷巷雨未停 提交于 2020-01-23 05:58:05
问题 This question already has answers here : cout << with char* argument prints string, not pointer value (6 answers) Closed 2 years ago . Why does #include <iostream> using namespace std; int main() { cout << (char*)0x10 << endl; } segfault, but #include <iostream> using namespace std; int main() { cout << (void*)0x10 << endl; } seems to work just fine? 回答1: Because cout::operator <<(void*) prints a memory address, and cout::operator <<(char*) prints a null-terminated character array, and you