请帮忙...我对我收到的错误感到困惑 [英] Please help...i am confused with the errors that I am receiving

查看:97
本文介绍了请帮忙...我对我收到的错误感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个尝试使用while循环的程序。我尝试编译代码,它给我的错误与代码的那部分没有任何关系,所以我想知道是否有人可以帮助我理解为什么我会收到这些错误。

这是我的代码:



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

void studentGrade(string);

int main
{
string name =zero;
int scoreOne,scoreTwo,scoreThree,scoreFour,scoreFive,scoreSix,scoreSeven,scoreEight,scoreNine,scoreTen;
浮动平均值= 0; //存储所有等级的平均值
浮动总数= 0; //存储成绩总和
char letterGrade; //存储学生的最终字母等级
if(name =done)
{
cout<< 计划已经完成。 ;
}
其他
{
studentGrade(姓名);
}
}
void studentGrade(字符串名称)
{
while(name!=done)
{
cout< < 输入学生的姓名。 ;
cin>>名称 ;
cout<< 输入<<名称<< 有10个考试成绩。 ;
cin>> scoreOne>>得分2>>得分三>> scoreFour>> scoreFive>> scoreSix>>得分>> scoreEight>> scoreNine>>得分;
total = scoreOne + scoreTwo + scoreThree + scoreFour + scoreFive + scoreSix + scoreSeven + scoreEight + scoreNine + scoreTen;
平均=总数/ 10;
if(平均值> 89.5)
letterGrade ='A';
else if(平均值> 79.5)
letterGrade ='B';
else if(平均值> 69.5)
letterGrade ='C';
else if(平均值> 59.5)
letterGrade ='D';
else
letterGrade ='F';
cout<< 学生的名字:<<名称<<结束
cout<< 平均等级:<<平均<<结束
cout<< 字母等级:<< letterGrade<<结束
}
}







这些是我收到的错误:



 prog4.cpp:14:警告:扩展初始化程序列表仅适用于-std = c ++ 0x或-std = gnu + + 0x 
prog4.cpp:16:错误:在'name'之前预期的primary-expression
prog4.cpp:16:错误:在'name'之前预期'}'
prog4.cpp: 16:错误:在'name'之前预期','或';'
prog4.cpp:21:错误:在'if'
prog4.cpp之前的预期unqualified-id:25:错误:预期不合格-id'之前的'-id'
prog4.cpp:29:错误:'}'令牌之前的预期声明

f



line 14是:int main



我尝试过:



我尝试添加#include< string>因为我在网上看到了,但它没有改变第二个错误。谢谢你能提供帮助。

解决方案

引用:

第14行: int main

所以看看那一行:

 int main 
{
// ... ...
}

这不是有效的C / C ++。函数定义包含



  1. 返回类型( int 此处),

  2. 一个名称( main 此处),

  3. 括号括起来的参数列表(缺失),并且

  4. 用括号括起来的身体。



所以它一定是

 int main()
{
//函数体

//返回语句(代码中不存在) )
返回0;
}

所有其他错误都来自初始错误,并在修复第一个错误后消失。





修复上述内容后,您将收到一个以前未检测到的新错误:

< pre lang =text>错误:无法将'name.std :: basic_string< ...>'转换为'bool'

std:string 赋值运算符返回对字符串本身的引用,该引用无法转换为 bool 。它可以根据其他解决方案修复(您使用赋值运算符 = 而不是比较运算符 == ) 。


在你的第一个 if 语句中,相等的比较运算符是 == ,而不是 =


尝试类似:

  if (name ==   done


This is my first program trying to use "while" for a loop. I try to compile the code and it gives me errors that don't have anything to do with that part of the code, so I was wondering if someone could help me understand why I am getting these errors.
Here is my code:

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

void studentGrade (string) ;

int main
{
	string name = "zero" ;
	int scoreOne, scoreTwo, scoreThree, scoreFour, scoreFive, scoreSix, scoreSeven, scoreEight, scoreNine, scoreTen ;
	float average = 0 ;			//stores the average of all grades
	float total = 0 ;			//stores sum of grades
	char letterGrade ;			//stores the final letter grade of student
	if (name = "done")
	{
		cout << "Program is done." ;
	}
	else 
	{
		studentGrade (name) ;
	}
}
void studentGrade (string name)
{
	while (name != "done")
	{
		cout << "Enter student's name." ;
		cin >> name ;
		cout << "Enter " << name << "'s 10 test scores." ;
		cin >> scoreOne >> scoreTwo >> scoreThree >> scoreFour >> scoreFive >> scoreSix >> scoreSeven >> scoreEight >> scoreNine >> scoreTen ;
		total = scoreOne + scoreTwo + scoreThree + scoreFour + scoreFive + scoreSix + scoreSeven + scoreEight + scoreNine + scoreTen ;
		average = total / 10 ;
		if (average > 89.5)
			letterGrade = 'A' ;
		else if (average > 79.5)
			letterGrade = 'B' ;
		else if (average > 69.5)
			letterGrade = 'C' ;
		else if (average > 59.5)
			letterGrade = 'D' ;
		else
			letterGrade = 'F' ;
		cout << "Student's name: " << name << endl ;
		cout << "Average Grade: " << average << endl ;
		cout << "Letter Grade: " << letterGrade << endl ;
	}
}




And these are the errors that I am receiving:

prog4.cpp:14: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x
prog4.cpp:16: error: expected primary-expression before ‘name’
prog4.cpp:16: error: expected ‘}’ before ‘name’
prog4.cpp:16: error: expected ‘,’ or ‘;’ before ‘name’
prog4.cpp:21: error: expected unqualified-id before ‘if’
prog4.cpp:25: error: expected unqualified-id before ‘else’
prog4.cpp:29: error: expected declaration before ‘}’ token

f

line 14 being: int main

What I have tried:

I have tried adding in the #include<string> because I saw that online, but it didn't change the second error. Thank you if you can help.

解决方案

Quote:

line 14 being: int main

So have a look at that line:

int main
{
    // ...
}

That is not valid C/C++. A function definition consists of


  1. a return type (int here),
  2. a name (main here),
  3. a parameter list enclosed by parentheses (missing), and
  4. the body enclosed by braces.


So it must be

int main()
{
    // function body

    // Return statement (does not exist in your code)
    return 0;
}

All the other errors are sourced by the initial error and will vanish after fixing the first error.


After fixing the above you will get a new error which has not been detected before:

error: could not convert ‘name.std::basic_string<...>’ to ‘bool’

The std:string assignment operator returns a reference to the string itself which can't be converted to bool. It can be fixed according to the other solutions (you used the assignment operator = instead of the comparison operator ==).


In your first if statement, the comparison operator for equality is ==, not =.


Try something like:

if (name == "done")


这篇关于请帮忙...我对我收到的错误感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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