注册 登录
编程论坛 C++教室

新手,请教一个输入输出流的问题。

q764959797 发布于 2017-11-13 21:45, 1016 次点击
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
int main()
{
    ofstream outf("Test_8.txt");
    char txt[255];
    cin.getline(txt,255);
    if(strlen(txt)==0)
    {
        break;
    }
    else
    {
        outf<<txt<<endl;
        cin.getline(txt,255);
    }
    outf.close();
}

报错是这样的:
error: break statement not within loop or switch

我看了下书,书上是用
while(strlen(txt)!=0)
    {
        outf<<txt<<endl;
        cin.getline(txt,255);
    }

请大佬们帮我分析一下我错的地方...谢谢了
2 回复
#2
rjsp2017-11-14 08:15
break 用于 while/for/switch 等,你if中用break的目的是什么呢?

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

int main( void )
{
    ofstream outf( "Test_8.txt" );
    for( string line; getline(cin,line); )
        outf << line << '\n';
}
#3
q7649597972017-11-14 16:23
回复 2楼 rjsp
谢谢老哥指点,明白了,我用break就是想退出 if(){}...
1