C++文件I/O作业问题:从含字母与空白字符的文本文件中提取并正确格式化输出数字
Fixing C++ File I/O to Extract Complete Numeric Values
Hey there! I see the issue with your current code right away—you're reading individual digits and printing them one by one, instead of grouping consecutive numeric characters (including decimal points) into full values. Let's fix that up so you get the exact output you're expecting.
What's Wrong with the Original Code?
- Your first
whileloop reads every character, but only prints single digits. It doesn't handle decimal points or group digits into complete numbers. - The second
while(in_stream >> num)loop never runs because the first loop already reads all the way to the end of the file, leaving the stream in an EOF state.
Corrected Code
Here's the revised version that properly extracts full numeric values (including decimals) from your text file:
#include <iostream> #include <fstream> #include <string> #include <cctype> #include <sstream> using namespace std; int main() { char ans; cout << "Welcome to Number Finder!\n"; cout << "============================\n"; do { char in_file_name[20]; ifstream in_stream; cout << "Which file do you want us to find numbers at?\n"; cin >> in_file_name; cout << endl; in_stream.open(in_file_name); if (in_stream.fail()) { cout << in_file_name << " is not found.\n"; cout << "-----" << endl; cout << "Do you want to process another file? \n"; cin >> ans; continue; // Skip rest of the loop if file can't be opened } cout << "We found the following magic numbers in " << in_file_name << ": "; string num_str; char c; bool has_decimal = false; while (in_stream.get(c)) { // Check if character is a digit or the first decimal point in the current number if (isdigit(c) || (!has_decimal && c == '.')) { num_str += c; if (c == '.') { has_decimal = true; } } else { // If we have collected a number string, convert and print it if (!num_str.empty()) { // Convert string to double double num; istringstream ss(num_str); ss >> num; cout << num << " "; // Reset for next number num_str.clear(); has_decimal = false; } } } // Don't forget to print the last number if the file ends with a numeric sequence if (!num_str.empty()) { double num; istringstream ss(num_str); ss >> num; cout << num << " "; } cout << endl; cout << "-----" << endl; cout << "Do you want to process another file? \n"; cin >> ans; in_stream.close(); } while (ans == 'Y' || ans == 'y'); cout << "Thank you for using the Number Finder! Bye for now!"; return 0; }
Key Changes Explained
- Collect numeric sequences: We use a
string(num_str) to build up each full number as we read characters. - Handle decimals: We track if we've already added a decimal point to the current number with
has_decimal, so we don't allow multiple decimals in one value. - Convert and print: When we hit a non-numeric/non-decimal character, we convert the collected string to a
doubleand print it, then reset our tracking variables. - File end handling: After the loop, we check if there's an unfinished number string (in case the file ends with a number) and print it.
- Error handling improvement: If the file fails to open, we skip the rest of the loop and immediately ask about processing another file.
Testing this with your sample input file will give you exactly the expected output:
We found the following magic numbers in test2.txt: 87 98.5 4 9 0 94.3 98.4 84
内容的提问来源于stack exchange,提问作者dollarortwo
相关产品推荐
相关产品推荐

