iostream

C++ match string in file and get line number

删除回忆录丶 提交于 2019-12-02 03:11:34
I have a file with the top 1000 baby names. I want to ask the user for a name...search the file...and tell the user what rank that name is for boy names and what rank for girl names. If it isn't in boy names or girl names, it tells the user it's not among the popular names for that gender. The file is laid out like this: Rank Boy-Names Girl-Names 1 Jacob Emily 2 Michael Emma . . . Desired output for input Michael would be: Michael is 2nd most popular among boy names. If Michael is not in girl names it should say: Michael is not among the most popular girl names Though if it was, it would say:

Strange BUG in C++ iostream?

给你一囗甜甜゛ 提交于 2019-12-02 02:55:28
问题 is this a bug in iostream? ... #include<iostream> void money_conversion (){ constexpr double dollars_in_yen=0.01; constexpr double dollars_in_euro=1.16; constexpr double dollars_in_pound=1.33; std::cout<<"Supported valutes : yen ('y'), euros('e'), pounds('p').\n"; std::cout<<"Please enter the value + valute that you want to convert into dollars! :"; double value=1; char valute=0; while(true){ std::cin>>value>>valute; if(valute=='y') std::cout<<"\n\n"<<value<<" yens is "<<value*dollars_in_yen<

std::cin.clear() fails to restore input stream in a good state

本小妞迷上赌 提交于 2019-12-02 01:58:41
In order to test bool i/o, I tried to run this short program: #include <iostream> int main() { while(true) { bool f; if (std::cin >> f) std::cout << f << '\n'; else { std::cout << "i/o error\n"; std::cin.clear(); } } return 0; } Here is the output I get: g++ -Wall -ansi -pedantic -o boolio boolio.cpp ./boolio 0 0 1 1 2 i/o error - i/o error t i/o error i/o error i/o error ... (infinite loop) I wonder why I get an infinite loop when I enter 't', and how to prevent it. Thanks. Add this line after clearing cin: std::cin.ignore(); This way, the stream ignores whatever is left on its buffer.

Change iostreams in child process

自古美人都是妖i 提交于 2019-12-02 01:22:51
问题 Right now, I'm working on a project in which I need to start a child process to execute a new program in Linux using C++, and I need to redirect standard input and output (as in C++, they are cin and cout ) to a file. This means that in the child process, the standard input and output are both files. The child process will read input from a file (whose name would be input.txt ), and output to a file (whose name would be output.txt ). By using cin.rdbuf() and cout.rdbuf() , I can actually

Value stored when istream read fails

青春壹個敷衍的年華 提交于 2019-12-02 00:28:11
Sample code: #include <iostream> int main() { int x = 5; std::cin >> x; std::cout << x << '\n'; } On one particular implementation the following behaviour occurs: Input: 6 ; output 6 Input: a ; output: 0 Input: (end-of-file); output 5 Input: (whitespace followed by end-of-file); output 5 So, on failure, the cin >> x is assigning 0 to x if it was a failure to convert text to int; but it is not assigning 0 if the failure was due to end-of-file. Is this correct behaviour? If not, what is the correct behaviour, according to the C++ Standard? I have a recollection from previously discussing on SO

Effects on Input Variable after Failed Input Stream

六月ゝ 毕业季﹏ 提交于 2019-12-01 23:30:22
I was working on the following code. #include <iostream> int main() { std::cout << "Enter numbers separated by whitespace (use -1 to quit): "; int i = 0; while (i != -1) { std::cin >> i; std::cout << "You entered " << i << '\n'; } } I know that using while (std::cin >> i) would have been better but I don't understand a specific occurrence. If I provide an invalid input, the loop becomes infinite because the Input Stream enters a failbit state. My question is that what happens to the input variable i ? In my case, it becomes 0 regardless of the previous value entered. Why does it change to 0

Change iostreams in child process

一世执手 提交于 2019-12-01 22:15:48
Right now, I'm working on a project in which I need to start a child process to execute a new program in Linux using C++, and I need to redirect standard input and output (as in C++, they are cin and cout ) to a file. This means that in the child process, the standard input and output are both files. The child process will read input from a file (whose name would be input.txt ), and output to a file (whose name would be output.txt ). By using cin.rdbuf() and cout.rdbuf() , I can actually redirect cin and cout in the parent process. But it doesn't work when the child process starts an execl()

Long double is printed incorrectly with iostreams on MinGW

此生再无相见时 提交于 2019-12-01 21:19:09
Consider the code #include <iostream> int main() { std::cout << 4.2L; } Compiling it on MinGW and running results in the following output: > g++ test.cc > a.exe -7.89773e-278 Is it a bug in MinGW and is there a fix or workaround? Update: There is a similar issue with printf described in this question : #include <cstdio> int main() { std::printf("%Lg", 4.2L); // prints -7.89773e-278 } However, the issue with printf can be fixed by defining __USE_MINGW_ANSI_STDIO while this one can't, so I think it deserves a separate question. This is not a MinGW bug ... controversial as that statement may seem

stringstream unsigned conversion broken?

流过昼夜 提交于 2019-12-01 21:03:03
问题 Consider this program: #include <iostream> #include <string> #include <sstream> #include <cassert> int main() { std::istringstream stream( "-1" ); unsigned short n = 0; stream >> n; assert( stream.fail() && n == 0 ); std::cout << "can't convert -1 to unsigned short" << std::endl; return 0; } I tried this on gcc (version 4.0.1 Apple Inc. build 5490) on OS X 10.5.6 and the assertion is true; it fails to convert -1 to an unsigned short. In Visual Studio 2005 (and 2008) however, the assertion

ios::nocreate error while compiling a C++ code

五迷三道 提交于 2019-12-01 19:58:36
While, compiling a package, written in C++ on RHEL 5.0. I am getting the following error. > error: nocreate is not a member of std::ios The source-code corresponds to: ifstream tempStr( argv[4] , ios::in | ios::nocreate ); I have tried #g++ -O -Wno-deprecated <file.cpp> -o <file> as well as: #g++ -O -o <file> Please suggest a solution. ios::nocreate is not part of standard C++ - what are you expecting it to do? Edit: From a Google, it seems like it was intended to prevent the file being created if it doesn't already exist. This is the default for ifstreams anyway, so you can just say: ifstream