gpt4 book ai didi

c++ - 如何循环程序的某些方面

转载 作者:行者123 更新时间:2023-11-30 05:38:58 25 4
gpt4 key购买 nike

我正在尝试根据用户为 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;
}

最佳答案

你应该删除 int n = 0;它应该只是 int n。

并且您应该使用 <=,两个字符之间没有任何空格。

编辑:正如您在这里看到的: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;
}

关于c++ - 如何循环程序的某些方面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32533849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com