我试图根据用户输入n
的内容来循环分配分数和总计。我一直在寻找并且只是打击哑弹,这就是目前int i
变量的整体处理。我无法正确循环,现在这甚至无法编译,因为那里有令人讨厌的i
。你取出i
,程序运行正常,但n
输入没有变化。
/**
File: project_2_14.cpp
Description: Write a program that calculates the total grade for N classroom exercises as a percentage. The user should input the value for N followed by each of the N scores and totals. Calculate the overall percentage (sum of the total points earned divided by the total points possible.) and output it as a percentage. Sample input and output is shown below.
Created: Friday, September 11th, 2015
Author:
email:
*/
#include<iostream>
#include<vector>
using namespace std;
int main()
{
float s; // assignment score
float t; // total points worth
float p; // percentage
int n = 0;
//input the number of assignments
cout << "How many assignments are there? " << endl;
cin >> n;
for (int i=0; i < =n; i++)
{
//input the total points earned for assignment
cout << "What is the score earned for this assignment? ";
cin >> s;
//input the number of points assignment is worth
cout << "How many points was the assignment worth? ";
cin >> t;
//calculate percentage
p = (s / t)*100;
}
//output score
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "Total score: " << p << "%"<< endl;
return 0;
}
答案 0 :(得分:1)
你应该删除int n = 0;它应该只是int n。
你应该使用&lt; =,两个字符之间没有任何空格。
编辑: 正如您在此处所见:http://ideone.com/SeNum8它已经正确循环:)
#include<iostream>
#include<vector>
using namespace std;
int main()
{
float s; // assignment score
float t; // total points worth
float p; // percentage
int n;
//input the number of assignments
cout << "How many assignments are there? " << endl;
cin >> n;
for (int i=0; i <=n; i++)
{
//input the total points earned for assignment
cout << "What is the score earned for this assignment? ";
cin >> s;
//input the number of points assignment is worth
cout << "How many points was the assignment worth? ";
cin >> t;
//calculate percentage
p = (s / t)*100;
}
//output score
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "Total score: " << p << "%"<< endl;
return 0;
}