这是我的代码。。
#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
double BMI;
string str;
stringstream ss;
fstream input("input.dat", ios::in);
fstream output("output.dat", ios::out);
getline(input, str);
ss << str;
string temp;
output << str<<'\n';
while (!ss.eof()) {
if (stringstream(temp) >> BMI) {
cout << BMI << " ";
if (BMI >= 25) {
output << "overweight" << '\n';
}
else if (BMI >= 18.5) {
output << "normal weight" << '\n';
}
else {
output << "under weight" << '\n';
}
}
ss >> temp;
}
}
我想要的是判断结果在每行的后面,怎么改写代码?
如LI 12.8 underweight
SONG 11.7 underweight
这样的形式,而不是在文本的最后像下图这样的输出形式
输入文件是这样的
NAME BMI
Ali 11.2
Beatriz 14.6
Charles 7.8
Diya 15.2
NUR ATHIRAH BINTI MANAF 14.2
NUR HUSNA BINTI AZMI 14.4
NUR NABILA BINTI ZAKARIA 7.6
NURHAFIZAH BINTI SHUKRI 5
NURHASNATUL AIDA BINTI SUHIZLIZANI IHSAN 11.1
NURMAZLI AZRIN BINTI MOHD RAZALI 7.3
NURSYAHIRA SABRINA BINTI MOHD. SUKRI 14.5
RAFEENA BINTI MOHAMAD RABII 13.9
HILMAN SYAFIE BIN ZAILANI 9.5
INTAN NORATHIRAH BINTI ANNUAR 19.1
ISMA REDHA BIN ABD RAHIM 9.1
LEE KEEN SHEN 25.2
LIM WEI WEN 27.1
MAIZATUL HUSNA BINTI MARSONO 44.2
MUHAMMAD ALIF IRFAN BIN ROSLI 34
MUHAMMAD SYAFIQ BIN MAT SHOKERI 23.9
MUHAMMAD SYAHMI BIN MUHAMMAD ZALY SHAH 31.9
MUHAMMED AFIQ AFWAN BIN MOHD SALMI 32.7
NAZATUL SYAHIRAH BINTI NAZARUDDIN 33.8
NG LI TIAN 23.1
NG ZHAN LUN 32.4
NIK NUR ARISHA BINTI NIK ISMAIL 37.7
NOR SYUHADA BINTI ANUAR 28.1
NUR AFIQAH BINTI IBRAHIM 28.2
NUR ATIQAH BINTI AHMAD ZAKI 24
NUR AZMINA FATINI BINTI MAHADZIR 27.5
NUR WAFA BINTI ABDUL HADI 34.6
NURAINI SYAHIRA BINTI ROZMAN 34.4
NURSYAZWANI BINTI HASNIZAM 30
NURUL NADIA BINTI ASRIN 20.9
ONG TING JIE 17.6
OOI CHEE SEANG 11.1
PRIYANGKA A/P SELVAKUMARAN 16
SHANGGAVEE A/P VELOO 7.7
SHURAIKH `EZZUDDIN BIN SHUKRI 10.5
TEO ZHI XIAN 13.8
WONG YEN PING 14.6
YEO YI SHENG 13.8
MUHAMAD RAFSHANJANI ICHMAWAN 8.1
NUR ATHIRAH BINTI MANAF 30.2
NUR HUSNA BINTI AZMI 26.5
NUR NABILA BINTI ZAKARIA 33.9
NURHAFIZAH BINTI SHUKRI 21.1
NURHASNATUL AIDA BINTI SUHIZLIZANI IHSAN 30.6
NURMAZLI AZRIN BINTI MOHD RAZALI 33.4
NURSYAHIRA SABRINA BINTI MOHD. SUKRI 35.2
你得给出input.dat的内容
就你的程序,只调用了1次getline,应该只输出1行。
因为不知道你的input.dat,我按照我的理解写了一个
#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
double BMI;
string name;
string temp;
stringstream ss;
fstream input("c:\\input.dat", ios::in);
fstream output("c:\\output.dat", ios::out);
while (!input.eof())
{
input >> name;
input >> BMI;
output << name << " ";
output << BMI << " ";
if (BMI >= 25) {
output << "overweight" << '\n';
}
else if (BMI >= 18.5) {
output << "normal weight" << '\n';
}
else {
output << "under weight" << '\n';
}
}
}
这是运行结果:
#include<iostream>
#include<fstream>
#include<string>
#include<stdio.h>
using namespace std;
int main()
{
double BMI;
string str;
char name[100];
fstream input("c:\\input.dat", ios::in);
fstream output("c:\\output.dat", ios::out);
while (!input.eof())
{
getline(input, str);
sscanf(str.c_str(), "%[^\t]\t%lf", &name, &BMI);
output << name << " ";
output << BMI << " ";
if (BMI >= 25) {
output << "overweight" << '\n';
}
else if (BMI >= 18.5) {
output << "normal weight" << '\n';
}
else {
output << "under weight" << '\n';
}
}
}
input.dat(注意,名字和bmi之间是tab,不是空格!!)
a a 123.5
b bb 231.1
c 17.6
d 27
ee ee ee 40
output.dat
a a 123.5 overweight
b bb 231.1 overweight
c 17.6 under weight
d 27 overweight
ee ee ee 40 overweight