Hello all im new to this forum and im a C++ beginner too so please forgive me if i did something wrong here..
This is what i want....
If i enter value :120' for one of 3 subjects as the code, it shows ( Example:- Enter Math Marks = 120) its keep repeat "Enter Math Marks" until i enter the right value ( mth>=0 && mth <= 100) to continue to next phase...I want to change this code, if i enter "120" for "Enter Math Marks" it should say "Enter Math Marks" one time only and if didnt enter a correct value after one time then the "Enter Math Marks" value should be zero (0) and continue to the next phase ( "Enter English Marks" and so on)
remove the whole do-while loop,
and all the try catch blocks.
replace it with basic logic instead:
cout << "Enter Math Marks =";
cin>>mth;
mth *= (mth>=0 && mth <=100);
you probably want to reserve try/catch to handle critical errors that require you to stop the program or do strong (not just a print, but actual data manipulation and cleanup) handling rather than using it as a funky and overly complex if/else replacement. This feels overthunk and bloated.
int main()
{
char un [200];
int sid, mth, eng, sft, total, err=0;
float avg;
cout << "Enter Username =";
cin >>un;
cout << "Student Name is: " << un << endl;
cout << "Enter Student ID =";
cin>>sid;
cout << "Enter Math Marks =";
cin>>mth;
mth = (mth>=0 && mth <=100);
cout << "Enter English Marks =";
cin>>eng;
eng=(eng>=0 && eng <=100);
cout << "Enter Software Marks =";
cin>>sft;
sft=(sft>=0 && sft <=100);
When i run the program i get this
1 2 3 4 5 6 7 8 9 10
nter Username =Asd
Student Name is: Asd
Enter Student ID =1234
Enter Math Marks =150
Enter English Marks =130
Enter Software Marks =-120
Total Marks =0
Average =0
Sorry you are Fail
Sorry you failed one subject so you need to repeat all 3 subjects
Well it doesnt calculate the Total Marks and Average but thats not what i want xd
I want when i enter invalid value (mth>=0 && mth<=100) for "Enter Math Marks", i want it to repeat and ask it again one more time and when i reenter another invalid value then i want its value set as zero (Enter Math Marks = 0) and continue..
*= vs = you missed part of it. its multiplying what they typed in by 0 or 1 (false or true) to clear it to zero if its out of bounds.
oh, you do want it to ask twice, initially, and if they goof up, one more, then zero out?
in that case you do need to either loop or unrolled loop it, I didn't understand you there.
#include<iostream>
usingnamespace std;
int main()
{
char un [200];
int sid, mth, eng, sft, total, err=0;
float avg;
cout << "Enter Username =";
cin >>un;
cout << "Student Name is: " << un << endl;
cout << "Enter Student ID =";
cin>>sid;
//this is an 'unrolled loop' -- it was just easier to do that way for 2 items and the extra message
cout << "Enter Math Marks =";
cin>>mth;
if(mth < 0 || mth > 100)
{
cout << "Invalid, try again:\n";
//optional additional text here?
cin>>mth;
}
mth *= (mth>=0 && mth <=100);
cout << mth << endl; //testing statement, does this partial program work for you up to here??
}