请教程序报错的原因

来源:百度知道 编辑:UC知道 时间:2024/06/23 14:19:22
#include <iostream>
#include <string>
using namespace std;

void main()
{
int i;
string str="G01X10Y20",strG_,strX_,strY_;
for (int i=0;i<str.length();i++)
{

if (str[i]==G)
strG_=substr(i+1,2);
cout<<"G数值"<<strG_<<endl;

if (str[i]==X)
strX_=substr(i+1,2);
cout<<"G数值"<<strX_<<endl;
if (str[i]==Y)
strY_=substr(i+1,2);
cout<<"G数值"<<strY_<<endl;
}

}
运行后,出现报错
f:\vc test\readnum\2.cpp(28) : fatal error C1010: unexpected end of file while looking for precompiled header directive
Error executing cl.exe.
不知如何解决

首先,if (str[i]==G)应该为if (str[i]=='G')
因为要和字符比较,而不是和变量G比较。后面也一样
其次,substr()是对象的方法,要指明是那个对象
改为str.substr()
再次,cout<<"G数值"<<strG_<<endl;应该放在循环体外面。
还有,没有.h的头文件有些编译器是找不到的,改成
#include <iostream.h>
#include <string.h>

改好了的,你试试吧。

#include <iostream.h>
#include <string.h>
using namespace std;

void main()
{
int i;
string str="G01X10Y20",strG_,strX_,strY_;
for (int i=0;i<str.length();i++)
{
if (str[i]=='G')
strG_=str.substr(i+1,2);

if (str[i]=='X')
strX_=str.substr(i+1,2);

if (str[i]=='Y')
strY_=str.substr(i+1,2);

}
cout<<"G数值"<<strG_<<endl;
cout<<"X数值"<<strX_<<endl;
cout<<"Y数值"<<strY_<<endl;
}

第一 i重定义了
第二 str[i]