我如何计算主题 [英] How do I calculate the subjects

查看:109
本文介绍了我如何计算主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是代码:



  int  main() 
{

const int numSubjects = 3 ;

vector< string> prefix { first < span class =code-string> second
third};
vector< string>科目( 3 );
vector< float>成绩( 3 );

for int i = 0 ; i< 3 ; i ++){
cout<< 输入<< prefix [i]<< subject:;
cin>>受试者[I];
cout<< 输入<<的等级受试者[i]<< ;
cin>>等级[I];
}

float sum = grade [ 1 ] +成绩[ 2 ] +成绩[ 3 ];
float average =(sum / 3 );


cout<< AVERAGE GRADE;
cout<< ------------------------- -----------<< ENDL;
cout<<科目[ 1 ]<<成绩[ 1 ]<< ENDL;
cout<<科目[ 2 ]<<成绩[ 2 ]<< ENDL;
cout<<科目[ 3 ]<<成绩[ 3 ]<< ENDL;
cout<< ------------------------- -----------<< ENDL;
cout<< 平均值:<<平均<< ENDL;


}

然后我收到此错误:

ErrorPhoto [ ^ ]

请记住我昨天开始学习c ++所以我真的不太了解。



我尝试了什么:



如果我删除浮动总和行下面的所有内容,代码的上半部分将起作用

解决方案

< pre lang =c ++> vector< float>成绩( 3 );



C ++基于零:它表示第一个元素为0。

大小为3的向量包含元素0,1和2.



您的代码行为不符合您的预期,或者您不理解为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道您的代码应该做什么,它不要找不到错误,只是通过告诉你发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]



调试器仅显示您的代码正在执行的操作,您的任务是与应该执行的操作进行比较。


Here is the code:

int main()
{
	
	const int numSubjects = 3;

	vector <string> prefix { "first", "second", "third" };
	vector <string> subjects(3);
	vector <float> grades(3);

	for (int i = 0; i < 3; i++) {
		cout << "Type in your " << prefix[i] << " subject: ";
		cin >> subjects[i];
		cout << "Type in your grade for " << subjects[i] << ": ";
		cin >> grades[i];
	}

		float sum = grades[1] + grades[2] + grades[3];
		float average = (sum / 3);


		cout << "AVERAGE GRADE";
		cout << "------------------------------------" << endl;
		cout << subjects[1] << grades[1] << endl;
		cout << subjects[2] << grades[2] << endl;
		cout << subjects[3] << grades[3] << endl;
		cout << "------------------------------------" << endl;
		cout << "Average: " << average << endl;

	
}

Then I get this error:
ErrorPhoto[^]
Please keep in mind I started learning c++ yesterday so I really don't know enough.

What I have tried:

If i delete everything below the "float sum" row, the upper part of the code works

解决方案

vector <float> grades(3);


C++ is zero based: it means that first element is 0.
A vector of size 3 have elements 0, 1 and 2.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于我如何计算主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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