How can i change this [Try Catch] C++ code?


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)

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  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;
  do {
    try{   
    cout << "Enter Math Marks =";
    cin>>mth;
    if(!(mth>=0 && mth <=100))
    {
    throw (mth);
    }
    
    cout << "Enter English Marks =";
    cin>>eng;
    if(!(eng>=0 && eng <=100))
    {
    throw (eng);
    }
    
    cout << "Enter Software Marks =";
    cin>>sft;
    if(!(sft>=0 && sft <=100))
    {
    throw (sft);
    }
   err=0;
    }
    catch (int e)
    {
   cout << " Invalid Marks"<<endl;
    err= 1;
  }
}while(err);
    
    total = mth+eng+sft;
    cout<<"Total Marks ="<<total<<endl;
    
    avg = total/3.0;
    cout<<"Average =" <<avg<<endl;
    
    
    if(avg>=75)
    cout<<"Very Good"<<endl;
    
    else
    
    if(avg >=50 && avg<74)
    cout <<"Good"<<endl;

    else

    if(avg>35 && avg<=49)
    cout<< "Sorry you are Fail"<<endl;
    else
    cout<< "Sorry you are Fail"<<endl;
    
    if(mth>=90 && eng >= 90 && sft>=90)
    cout<<"First Class"<<endl;
    else
if(mth < 50 || eng < 50 || sft < 50)
cout<<"Sorry you failed one subject so you need to repeat all 3 subjects"<<endl;
    
   
    
    return 0;


}
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.
Last edited on
Hello sir
Thank you for your rply and i did exactly like u said but it doesnt work..

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

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..

Please can you help me :c
Last edited on
*= 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.

so all that together then..

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
#include<iostream>
using namespace 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??
}
Last edited on
Yea that is exactly what i wanted...Thank you soo much sir i rly appreciate your help :D
Topic archived. No new replies allowed.