帮助解决C ++错误!! [英] Help with C++ Error!!

查看:73
本文介绍了帮助解决C ++错误!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直收到此错误:

错误C2143:语法错误:缺少';'之前'}'



  #include   <   iostream  >  
#include < fstream >
#include < iomanip >

使用 命名空间标准;

int main( int argc, const char * argv [])
{
ifstream input;
input.open( text.txt);

int employeeid,numberofemployees;
float 每小时,毛钱,工作时间,税务,netpay;
float taxrate = 0 ;
char taxstatus [ 2 ];

numberofemployees = 0 ;

while (输入>> employeeid>> hoursworked>> hourlyrate>> taxstatus){

grosspay = hoursworked * hourlyrate;

如果(grosspay> 1000 )taxrate = 0 30 ;
其他 如果(grosspay> 800 )taxrate = 0 20 ;
其他 如果(grosspay> 500 )taxrate = 0 10 ;
else taxrate = 0 0 ;
ifstream输入;
if ((((strcmp(taxstatus, H)== 0 ))||((strcmp(taxstatus, h)== 0 )))&&(grosspay> 500)){
taxrate = taxrate - 。 05 ;
}
ifstream输入;
if ((((strcmp(taxstatus, S)== 0 ))||((strcmp(taxstatus, s)== 0 ))){
taxrate = taxrate - 。 05 ;
}
ifstream输入;
if (((((strcmp (taxstatus, M)== 0 ))||((strcmp(taxstatus, m)== 0 ))){
taxrate = taxrate - 。 05 ;

}
taxamount = grosspay * taxrate;
netpay = grosspay-taxamount;
cout<< 员工ID为:<< employeeid<< endl;
cout<< 工作时间为:<< hoursworked<< endl;
cout<< 每小时费率为:<< hourlyrate<< ENDL;
cout<< 总收入是:<< grosspay<< ; ENDL;
cout<< TAXAMOUNT IS:<< TAXAMOUNT<< ENDL;
cout<< NET PAY是:<< netpay<< ENDL;

}
input.close();
system( pause);
return 0 ;
}

解决方案

在这两行中添加一个括号



  if ((((strcmp(taxstatus,  S)==  0 ))||((strcmp(taxstatus,  s)==  0 )))){


if ((((strcmp(taxstatus, M)== 0 ))||((strcmp(taxstatus, m)== 0 )))){


它是一个太多打开的括号,但写得更干净的代码:



 < span class =code-keyword> if ((((strcmp(taxstatus,  H)==  0 ))||((strcmp(taxstatus,  h)==  0 )))&& (grosspay> 500)){

// 是足够的bur使用空间
if (((strcmp(taxstatus, H)== 0 )||((strcmp(taxstatus, h)== 0 )&&(grosspay> 500)){





而不是

 strcmp(taxstatus,S)== 0)





你最好写一下



 switch(taxstatus [0 ])
{
case'H:'
case'h':





因为它是一个单个角色!!!



input.open(text.txt); //文件应该有一个完整的路径


不是 .05 一个无效的数字文字?只是在这里猜测。







嗯。似乎我错了,我从来没有见过有人真正开始浮点(或相关)的文字与小数点和永远不会做这样自己,但它似乎是好的。怪异。

I keep getting this error:
error C2143: syntax error : missing ';' before '}'

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main(int argc, const char * argv[])
{
ifstream input;
input.open("text.txt");

int employeeid, numberofemployees;
float hourlyrate, grosspay, hoursworked , taxamount, netpay;
float taxrate=0;
char taxstatus[2];

numberofemployees=0;

while (input>>employeeid>>hoursworked>>hourlyrate>>taxstatus){

grosspay = hoursworked*hourlyrate;

if (grosspay > 1000) taxrate= 0.30;
else if (grosspay > 800 ) taxrate=  0.20;
else if (grosspay > 500) taxrate = 0.10;
else taxrate=0.0;
ifstream input;
if ((((strcmp(taxstatus, "H")==0))||((strcmp(taxstatus, "h")==0))) && (grosspay>500)){
taxrate=taxrate-.05;
}
ifstream input;
if ((((strcmp(taxstatus, "S")==0))||((strcmp(taxstatus, "s")==0))){
taxrate=taxrate-.05;
}
ifstream input;
if ((((strcmp(taxstatus, "M")==0))||((strcmp(taxstatus, "m")==0))){
taxrate=taxrate-.05;

}
taxamount=grosspay*taxrate;
netpay=grosspay-taxamount;
cout<<"THE EMPLOYEE ID IS:"<< employeeid<<endl;
cout<<"THE HOURS WORKED ARE:" <<hoursworked<<endl;
cout<<"THE HOURLY RATE IS:"<< hourlyrate<<endl;
cout<<"THE GROSS PAY IS:" <<grosspay<<endl;
cout<<"THE TAXAMOUNT IS:"<< taxamount<<endl;
cout<<"THE NET PAY IS:"<< netpay<<endl;

}
input.close();
system("pause");
return 0;
}

解决方案

Add one bracket in both these lines

if ((((strcmp(taxstatus, "S")==0))||((strcmp(taxstatus, "s")==0)))){


if ((((strcmp(taxstatus, "M")==0))||((strcmp(taxstatus, "m")==0)))){


it is a bracket too much opened, but write cleaner code:

if ((((strcmp(taxstatus, "H")==0))||((strcmp(taxstatus, "h")==0))) && (grosspay>500)){

//is is enough bur use space
if ( ( (strcmp(taxstatus, "H")==0) || ((strcmp(taxstatus, "h")==0) && (grosspay>500) ) {



instead of

strcmp(taxstatus, "S")==0)



you better write

switch( taxstatus[0] )
{
case 'H:'
case 'h':



because it is a single character!!!

input.open("text.txt"); //file should have a full path


Isn't .05 an invalid numeric literal? Just guessing here.



Huh. Seems I'm wrong. I've never seen anyone actually begin a floating point (or related) literal with the decimal point and would never do so myself, but it appears to be OK. Weird.


这篇关于帮助解决C ++错误!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆