C++ 用C++;

C++ 用C++;,c++,file-io,C++,File Io,这是我的密码: #include <iostream> #include <fstream> #include <string> using namespace std; int main() { string fname,lname; double pincrease //pay increase percentage,pay; ifstream infile; ofstream outfile; infile.open("C:\\Users\\Co

这是我的密码:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()

{

string fname,lname;
double pincrease //pay increase percentage,pay;
ifstream infile;
ofstream outfile;

infile.open("C:\\Users\\Connor\\Desktop");
outfile.open("C:\\Users\\Connor\\Desktop");

while(!infile.eof())
{
    infile>>lname>>fname>>pay>>pincrease;
    pay = pay*(pay*pincrease);
    outfile<<fname<<" "<<lname<<" "<<pay<<"\n";
    cin.clear();
}

infile.close();
outfile.close();

}
信息的格式为
姓氏:名字:薪酬:加薪百分比

当我写入
输出文件时,我有意交换姓名和姓氏的订单,并排除工资增长百分比

我正在尝试读取
infle
的内容,修改它们,然后将其写入
outfile


然而,当我执行代码时,我启动了一个无限循环,但我不确定如何修复它。

这些语句都不可能打开文件:

infile.open("C:\\Users\\Connor\\Desktop");
outfile.open("C:\\Users\\Connor\\Desktop");
相反,他们试图(很可能失败)打开你的桌面文件夹。更确切地说,您可能想要的是:

infile.open("C:\\Users\\Connor\\Desktop\\infile");
outfile.open("C:\\Users\\Connor\\Desktop\\outfile");
当然,最后的
infile
outfile
应该替换为实际的文件名

您可以通过检查
infle.is\u open()
outfile.is\u open()
来测试文件是否成功打开。您可以添加显式的
if
语句来测试这一点:

if (!infile.is_open())
    // report/handle that you couldn't open input file

if (!outfile.is_open())
    // report/handle that you couldn't open output file
对于主循环,您不应该像以前那样测试
eof
。更确切地说,请使用以下内容:

while(infile>>lname>>fname>>pay>>pincrease)
{
    pay = pay*(pay*pincrease);
    outfile<<fname<<" "<<lname<<" "<<pay<<"\n";
    cin.clear();
}
while(infle>>lname>>fname>>pay>>pincrease)
{
pay=pay*(pay*pincrease);

outfile这些语句都不可能打开文件:

infile.open("C:\\Users\\Connor\\Desktop");
outfile.open("C:\\Users\\Connor\\Desktop");
相反,他们试图(可能会失败)打开您的桌面文件夹。相反,您可能想要类似以下内容:

infile.open("C:\\Users\\Connor\\Desktop\\infile");
outfile.open("C:\\Users\\Connor\\Desktop\\outfile");
当然,最后的
infile
outfile
应该替换为实际的文件名

您可以通过检查
infle.is\u open()
outfile.is\u open()
来测试文件是否成功打开。您可以添加显式
if
语句来测试:

if (!infile.is_open())
    // report/handle that you couldn't open input file

if (!outfile.is_open())
    // report/handle that you couldn't open output file
对于主循环,您不应该像测试那样测试
eof
。相反,请使用以下方法:

while(infile>>lname>>fname>>pay>>pincrease)
{
    pay = pay*(pay*pincrease);
    outfile<<fname<<" "<<lname<<" "<<pay<<"\n";
    cin.clear();
}
while(infle>>lname>>fname>>pay>>pincrease)
{
pay=pay*(pay*pincrease);

outfile这些语句都不可能打开文件:

infile.open("C:\\Users\\Connor\\Desktop");
outfile.open("C:\\Users\\Connor\\Desktop");
相反,他们试图(可能会失败)打开您的桌面文件夹。相反,您可能想要类似以下内容:

infile.open("C:\\Users\\Connor\\Desktop\\infile");
outfile.open("C:\\Users\\Connor\\Desktop\\outfile");
当然,最后的
infile
outfile
应该替换为实际的文件名

您可以通过检查
infle.is\u open()
outfile.is\u open()
来测试文件是否成功打开。您可以添加显式
if
语句来测试:

if (!infile.is_open())
    // report/handle that you couldn't open input file

if (!outfile.is_open())
    // report/handle that you couldn't open output file
对于主循环,您不应该像测试那样测试
eof
。相反,请使用以下方法:

while(infile>>lname>>fname>>pay>>pincrease)
{
    pay = pay*(pay*pincrease);
    outfile<<fname<<" "<<lname<<" "<<pay<<"\n";
    cin.clear();
}
while(infle>>lname>>fname>>pay>>pincrease)
{
pay=pay*(pay*pincrease);

outfile这些语句都不可能打开文件:

infile.open("C:\\Users\\Connor\\Desktop");
outfile.open("C:\\Users\\Connor\\Desktop");
相反,他们试图(可能会失败)打开您的桌面文件夹。相反,您可能想要类似以下内容:

infile.open("C:\\Users\\Connor\\Desktop\\infile");
outfile.open("C:\\Users\\Connor\\Desktop\\outfile");
当然,最后的
infile
outfile
应该替换为实际的文件名

您可以通过检查
infle.is\u open()
outfile.is\u open()
来测试文件是否成功打开。您可以添加显式
if
语句来测试:

if (!infile.is_open())
    // report/handle that you couldn't open input file

if (!outfile.is_open())
    // report/handle that you couldn't open output file
对于主循环,您不应该像测试那样测试
eof
。相反,请使用以下方法:

while(infile>>lname>>fname>>pay>>pincrease)
{
    pay = pay*(pay*pincrease);
    outfile<<fname<<" "<<lname<<" "<<pay<<"\n";
    cin.clear();
}
while(infle>>lname>>fname>>pay>>pincrease)
{
pay=pay*(pay*pincrease);
输出文件替换此文件

infile.open("C:\\Users\\Connor\\Desktop");
outfile.open("C:\\Users\\Connor\\Desktop");
用这个

infile.open("C:\\Users\\Connor\\infile.txt");
outfile.open("C:\\Users\\Connor\\outfile.txt");
由于两个原因,您发布的代码将无法编译。一是变量
pay
尚未声明,二是您已注释掉以下代码的终止

double pincrease //pay increase percentage,pay;
请尝试下面的代码。希望对您有所帮助

            int main()

            {

                string fname,lname;
                double pincrease; //pay increase percentage,pay;
                double pay;
                ifstream infile;
                ofstream outfile;

                infile.open("C:\\Users\\Connor\\infile.txt");
                outfile.open("C:\\Users\\Connor\\outfile.txt");

                while(!infile.eof())
                {
                    infile>>lname>>fname>>pay>>pincrease;
                    pay = pay*(pay*pincrease);
                    outfile<<std::setprecision(2)<<std::showpoint << std::fixed;;
                    outfile<<fname<<" "<<lname<<" "<<pay<<"\n";
                    cin.clear();
                }

                infile.close();
                outfile.close();

            }
intmain()
{
字符串fname,lname;
double pincrease;//加薪百分比,工资;
双薪;
河流充填;
出流孔的直径;
infle.open(“C:\\Users\\Connor\\infle.txt”);
outfile.open(“C:\\Users\\Connor\\outfile.txt”);
而(!infle.eof())
{
填充>>lname>>fname>>支付>>平仓;
pay=pay*(pay*pincrease);
输出文件替换此文件

infile.open("C:\\Users\\Connor\\Desktop");
outfile.open("C:\\Users\\Connor\\Desktop");
用这个

infile.open("C:\\Users\\Connor\\infile.txt");
outfile.open("C:\\Users\\Connor\\outfile.txt");
由于两个原因,您发布的代码将无法编译。一是变量
pay
尚未声明,二是您已注释掉以下代码的终止

double pincrease //pay increase percentage,pay;
请尝试下面的代码。希望对您有所帮助

            int main()

            {

                string fname,lname;
                double pincrease; //pay increase percentage,pay;
                double pay;
                ifstream infile;
                ofstream outfile;

                infile.open("C:\\Users\\Connor\\infile.txt");
                outfile.open("C:\\Users\\Connor\\outfile.txt");

                while(!infile.eof())
                {
                    infile>>lname>>fname>>pay>>pincrease;
                    pay = pay*(pay*pincrease);
                    outfile<<std::setprecision(2)<<std::showpoint << std::fixed;;
                    outfile<<fname<<" "<<lname<<" "<<pay<<"\n";
                    cin.clear();
                }

                infile.close();
                outfile.close();

            }
intmain()
{
字符串fname,lname;
double pincrease;//加薪百分比,工资;
双薪;
河流充填;
出流孔的直径;
infle.open(“C:\\Users\\Connor\\infle.txt”);
outfile.open(“C:\\Users\\Connor\\outfile.txt”);
而(!infle.eof())
{
填充>>lname>>fname>>支付>>平仓;
pay=pay*(pay*pincrease);
输出文件替换此文件

infile.open("C:\\Users\\Connor\\Desktop");
outfile.open("C:\\Users\\Connor\\Desktop");
用这个

infile.open("C:\\Users\\Connor\\infile.txt");
outfile.open("C:\\Users\\Connor\\outfile.txt");
由于两个原因,您发布的代码将无法编译。一是变量
pay
尚未声明,二是您已注释掉以下代码的终止

double pincrease //pay increase percentage,pay;
请尝试下面的代码。希望对您有所帮助

            int main()

            {

                string fname,lname;
                double pincrease; //pay increase percentage,pay;
                double pay;
                ifstream infile;
                ofstream outfile;

                infile.open("C:\\Users\\Connor\\infile.txt");
                outfile.open("C:\\Users\\Connor\\outfile.txt");

                while(!infile.eof())
                {
                    infile>>lname>>fname>>pay>>pincrease;
                    pay = pay*(pay*pincrease);
                    outfile<<std::setprecision(2)<<std::showpoint << std::fixed;;
                    outfile<<fname<<" "<<lname<<" "<<pay<<"\n";
                    cin.clear();
                }

                infile.close();
                outfile.close();

            }
intmain()
{
字符串fname,lname;
double pincrease;//加薪百分比,工资;
双薪;
河流充填;
出流孔的直径;
infle.open(“C:\\Users\\Connor\\infle.txt”);
outfile.open(“C:\\Users\\Connor\\outfile.txt”);
而(!infle.eof())
{
填充>>lname>>fname>>支付>>平仓;
pay=pay*(pay*pincrease);
输出文件替换此文件

infile.open("C:\\Users\\Connor\\Desktop");
outfile.open("C:\\Users\\Connor\\Desktop");
用这个

infile.open("C:\\Users\\Connor\\infile.txt");
outfile.open("C:\\Users\\Connor\\outfile.txt");
由于两个原因,您发布的代码无法编译。第一,变量
pay
未被编译