View Screenshot for better understanding
#include <iostream>
using namespace std;
int main() {
int a; // number of classes held
int b; // number of classed attended
int percent;
cout<<"Number of classes held "<<endl;
cin>>a;
cout<<"Number of classes attended "<<endl;
cin>>b;
percent = (b*100)/a;
if (percent>75 && percent<=100) {
cout<<"Congratulation you are allowed to sit in the examination your attendence is "<<percent<<"%";
} else if (percent<75) {
int m;
cout<<"Do you have any medical cause? (Respond in '1' for yes or '0' for no) "<<endl;
cin>>m;
if (m==1){
cout<<"You are allwed due to a medical cause your percentage is "<<percent<<"%";
} else if (m==0) {
cout<<"You are not allowed to sit in the examination your percentage is "<<percent<<"%";
} else if (m!=1 && m!=0){
cout<<"Invalid Responce";
}
}
else {
cout<<"invalid attendence";
}
return 0;
}
cout<<"Number of classes held "<<endl;
cin>>a;
cout<<"Number of classes attended "<<endl;
cin>>b;
percent = (b*100)/a;
if (percent>75 && percent<=100) {
cout<<"Congratulation you are allowed to sit in the examination your attendence is "<<percent<<"%";
} else if (percent<75) {
int m;
cout<<"Do you have any medical cause? (Respond in '1' for yes or '0' for no) "<<endl;
cin>>m;
if (m==1){
cout<<"You are allwed due to a medical cause your percentage is "<<percent<<"%";
} else if (m==0) {
cout<<"You are not allowed to sit in the examination your percentage is "<<percent<<"%";
} else if (m!=1 && m!=0){
cout<<"Invalid Responce";
}
}
else {
cout<<"invalid attendence";
}
return 0;
}
I want the exact same output but instead of 0 and 1 like integers I want that user should type Y or N, I tried to create a char variable but I am not getting it right, it says y and n is not declared, I know its s very silly and basic question but I just started learning c , help your junior.
CodePudding user response:
Try using char
:
char m;
std::cin >> m;
if (m == 'y')
// do something
else if (m == 'n')
// do something else
CodePudding user response:
I think I understood the problem. To do this, you can also include the string
library with #include <string>
. So you can enter the "y" and "n" values you want as strings.
I leave a sample code for you.
#include <iostream>
#include <string>
using namespace std;
int main() {
string answer;
cout << "yes or no=? ";
cin >> answer;
if(answer == "y")
{
cout << "YES!";
}
else if(answer == "n")
{
cout << "NO!";
}
else {
cout << "TRY AGAIN!";
}
return 0;
}