程序运行正常,但唯一的错误是百分比值显示inf。当我初始化变量= 1。答案百分比就是我输入的* 100。
我尝试过不同地更改公式和初始化值。 这是我一段时间以来一直尝试更改的程序代码。
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
//Declaration of Variables;
int option,total=1;
float Y1=1,Y2=0,Y3=0,Y4=0,p1=1,p2=0,p3=0,p4=0;
//Selection of Program;
while (true)
{
cout <<"\nPlease enter program option for BSC: ";
cout<<"\nPlease enter program option for BIT: ";
cout<<"\nPlease enter program option for BBA: ";
cout<<"\nPlease enter program option for BA: ";
cout<<"\n***************************************";
//User Prompt to input option;
cout<<"\nplease enter option\n";
cin>>option;
//Choice of Correct answer;
if(option==1 || option<=4)
{
cout<<"\nSuccess";
cout<<"\n***************************************";
break;
}
//Choice of Wrong answer;
else
{
cout<<"\nInvalid option\n";
cout<<"Please select correct option\n";
}
}
//Enter number of students in each year;
cout<<"\nplease Enter number of students in year 1: ";
cin>>Y1;
cout<<"\nplease Enter number of students in year 2: ";
cin>>Y2;
cout<<"\nplease Enter number of students in year 3: ";
cin>>Y3;
cout<<"\nplease Enter number of students in year 4: ";
cin>>Y4;
cout<<"\n***************************************";
//Dept. Name based on user's choice;
switch(option)
{
case 1:
cout<<"\n********DEPT. OF COMPUTER SCIENCE********";
break;
case 2:
cout<<"\n********DEPT. OF INFORMATION & TECHNOLOGY********";
break;
case 3:
cout<<"\n********DEPT. OF BUSINESS********";
break;
case 4:
cout<<"\n********DEPT. OF FINE ARTS********";
break;
}
//Number of students in each year;
cout<<"\n***************************************";
cout<<"\nThe no. of students in year 1 is: "<<Y1;
cout<<"\nThe no. of students in year 2 is: "<<Y2;
cout<<"\nThe no. of students in year 3 is: "<<Y3;
cout<<"\nThe no. of students in year 4 is: "<<Y4;
cout<<"\n***************************************";
//Percentage of students in each year;
p1=(Y1/total)*100.00;
cout<<"\n\npercentage of year 1 students is: "<< p1 << "%";
p2=(Y2*100.00)/total;
cout<<"\npercentage of year 2 students is: "<< p2 << "%";
p3=(Y3/total)*100.00;
cout<<"\npercentage of year 3 students is: "<< p3 << "%";
p4=(Y4/total)*100.00;
cout<<"\npercentage of year 4 students is: "<< p4 <<"%";
cout<<"\n***************************************\n";
//Total number of students in the program;
total=Y1+Y2+Y3+Y4;
cout<< "\nTotal no. of students in program is: "<<total;
//Limit of students in each year;
if (Y1>100)
{
cout<<"\n\nThe limit of students enrolled in Y1 is exceeded";
}
if (Y2>100)
{
cout<<"\n\nThe limit of students enrolled in Y2 is exceeded";
}
if (Y3>100)
{
cout<<"\n\nThe limit of students enrolled in Y3 is exceeded";
}
if (Y4>100)
{
cout<<"\n\nThe limit of students enrolled in Y4 is exceeded";
}
}