Mar 27, 2021 at 10:15am UTC
How do I put an error message to this? like for example: you accidentally pressed a letter intead of a number in grades then an error message will show
#include<iostream>
using namespace std;
int main()
{
double assignment1, assignment2, seatwork1, seatwork2, quiz1, quiz2, majorexam, grade, major, assignment, seatwork, quiz;
string name;
cout<<"Enter Student name: ";
cin>>name;
cout<<"Enter grade for assignment 1: ";
cin>>assignment1;
cout<<"Enter grade for assignment 2: ";
cin>>assignment2;
cout<<"\nEnter grade for seatwork 1: ";
cin>>seatwork1;
cout<<"Enter grade for seatwork 2: ";
cin>>seatwork2;
cout<<"\nEnter grade for quiz 1: ";
cin>>quiz1;
cout<<"Enter grade for quiz 2: ";
cin>>quiz2;
cout<<"\nEnter grade for major exam: ";
cin>>majorexam;
float aveAssignment=(assignment1+assignment2)/2;
assignment = (aveAssignment)*.1;
float aveSeatwork=(seatwork1+seatwork2)/2;
seatwork = (aveSeatwork)*.2;
float aveQuiz=(quiz1+quiz2)/2;
quiz = (aveQuiz)*.3;
major = majorexam*.4;
grade = (assignment+seatwork+quiz+major);
cout<<"Final Grade is: "<<grade;
return 0;
}
Mar 27, 2021 at 12:10pm UTC
You can do something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <iostream>
#include <string>
#include <limits>
using namespace std;
double getNum(const std::string& prm)
{
double d {};
while ((std::cout << prm) && (!(std::cin >> d) || std::cin.peek() != '\n' )) {
std::cout << "Not a number\n" ;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n' );
return d;
}
int main()
{
string name;
cout << "Enter Student name: " ;
getline(cin, name);
const auto assignment1 {getNum("Enter grade for assignment 1: " )};
const auto assignment2 {getNum("Enter grade for assignment 2: " )};
const auto seatwork1 {getNum("Enter grade for seatwork 1: " )};
const auto seatwork2 {getNum("Enter grade for seatwork 2: " )};
const auto quiz1 {getNum("Enter grade for quiz 1: " )};
const auto quiz2 {getNum("Enter grade for quiz 2: " )};
const auto majorexam {getNum("Enter grade for major exam: " )};
const auto aveAssignment {(assignment1 + assignment2) / 2.0};
const auto assignment {aveAssignment * .1};
const auto aveSeatwork {(seatwork1 + seatwork2) / 2.0};
const auto seatwork {aveSeatwork * .2};
const auto aveQuiz {(quiz1 + quiz2) / 2.0};
const auto quiz {aveQuiz * .3};
const auto major {majorexam * .4};
const auto grade {assignment + seatwork + quiz + major};
cout << "Final Grade is: " << grade << '\n' ;
}
Last edited on Mar 27, 2021 at 12:11pm UTC
Mar 27, 2021 at 12:46pm UTC
Both codes doesn't work
Last edited on Mar 27, 2021 at 12:51pm UTC
Mar 27, 2021 at 1:09pm UTC
It doesn't work on the shell either.
Maybe C++17 required?
Mar 27, 2021 at 1:53pm UTC
Yes, C++17 is required. But in 2021 with C++20 now the current standard, C++17 should be the minimum.
Mar 27, 2021 at 3:49pm UTC
I agree, just a shame that the shell only supports C++14