Home > Back-end >  C Calculator Check
C Calculator Check

Time:12-16

Is this code Write. I want to get Average their marks ande student rank ? Each class has 40 students In the school, you have 2 streams, the Bioscience stream, and the Math stream.

In the Bio stream, there are 3 classes where each class has 40 students.

In the Math stream, there are 5 classes where each class has 40 students.

Bio students take Biology, Physics, and Chemistry.

Maths students take Combined Maths, Physics, and Chemistry.

#include <iostream>
using namespace std ;

int main() {
  string stream, subject , classes;
  string Bio,  Math ;
  int totalMarks,CombinedMathsMarks, PhysicsMarks, ChemistryMarks,biologyMarks;
  string A,B,C,D,E;
  cout << "Type your stream: ";
  cin >> stream;
  cout << "Type your subject: ";
  cin >> subject;
  cout << "Type your classes: ";
  cin >> classes;
  if (stream == Math ) { 
    
    if(classes == A ){
        cout << "Type your CombinedMathsMarks: ";
        cin >> CombinedMathsMarks;
        cout << "Type your Physics Marks: ";
        cin >> PhysicsMarks;
        cout << "Type your Chemistry Marks: ";
        cin >> ChemistryMarks;
        totalMarks = CombinedMathsMarks PhysicsMarks ChemistryMarks;
        cout << totalMarks;
    }
    if(classes ==B ){
        cout << "Type your CombinedMathsMarks: ";
        cin >> CombinedMathsMarks;
        cout << "Type your Physics Marks: ";
        cin >> PhysicsMarks;
        cout << "Type your Chemistry Marks: ";
        cin >> ChemistryMarks;
        totalMarks = CombinedMathsMarks PhysicsMarks ChemistryMarks;
        cout << totalMarks;
    }
    if(classes ==C ){
        cout << "Type your CombinedMathsMarks: ";
        cin >> CombinedMathsMarks;
        cout << "Type your Physics Marks: ";
        cin >> PhysicsMarks;
        cout << "Type your Chemistry Marks: ";
        cin >> ChemistryMarks;
        totalMarks = CombinedMathsMarks PhysicsMarks ChemistryMarks;
        cout << totalMarks;
    }
    if(classes ==D ){
        cout << "Type your CombinedMathsMarks: ";
        cin >> CombinedMathsMarks;
        cout << "Type your Physics Marks: ";
        cin >> PhysicsMarks;
        cout << "Type your Chemistry Marks: ";
        cin >> ChemistryMarks;
        totalMarks = CombinedMathsMarks PhysicsMarks ChemistryMarks;
        cout << totalMarks;
    }
    if(classes == E ){
        cout << "Type your CombinedMathsMarks: ";
        cin >> CombinedMathsMarks;
        cout << "Type your Physics Marks: ";
        cin >> PhysicsMarks;
        cout << "Type your Chemistry Marks: ";
        cin >> ChemistryMarks;
        totalMarks = CombinedMathsMarks PhysicsMarks ChemistryMarks;
        cout << totalMarks;
    }


}
if (stream == Bio ) {
    if(classes == A ){
        cout << "Type your biologyMarks Marks: ";
        cin >> biologyMarks;
        cout << "Type your Physics Marks: ";
        cin >> PhysicsMarks;
        cout << "Type your Chemistry Marks: ";
        cin >> ChemistryMarks;
        totalMarks = CombinedMathsMarks PhysicsMarks ChemistryMarks;
        cout << totalMarks;
    }
    if(classes ==B ){
        cout << "Type your biology Marks : ";
        cin >> biologyMarks;
        cout << "Type your Physics Marks: ";
        cin >> PhysicsMarks;
        cout << "Type your Chemistry Marks: ";
        cin >> ChemistryMarks;
        totalMarks = CombinedMathsMarks PhysicsMarks ChemistryMarks;
        cout << totalMarks;
    }
    if(classes ==C ){
        cout << "Type your biology Marks: ";
        cin >> biologyMarks;
        cout << "Type your Physics Marks: ";
        cin >> PhysicsMarks;
        cout << "Type your Chemistry Marks: ";
        cin >> ChemistryMarks;
        totalMarks = CombinedMathsMarks PhysicsMarks ChemistryMarks;
        cout << totalMarks;
    }
    
    
    
    }  
  
  return 0;
}

CodePudding user response:

There are some minor issues with your code. First, I don't really see the point of

cout << "Type your subject: ";
cin >> subject;

Then, the real reason your code is not giving any result is that you are doing the comparison wrong when using the == operator in the if statements. The thing is A is the name of the variable string, not the actual string which you are comparing. So, you can either assign it correctly at the start. Like, string A = "A" and similarly for other cases. Or else, you can directly change the if statements to

classes == "A"
stream == "Math"

Another thing you might wanna check in your code is that, in the Bio Stream if block, for the output statement you have used

totalMarks = CombinedMathsMarks   PhysicsMarks   ChemistryMarks;

which was to be meant for the Maths Stream block. Instead, it should be

totalMarks = biologyMarks   PhysicsMarks   ChemistryMarks;

CodePudding user response:

Error is that you have defined classes as String and not using "" them in if condition.

Like

  • incorrect code - if (stream == Math )

  • correct code - if (stream == "Math" )

Similarly A is replaced with "A" and all other variables.

CodePudding user response:

I found some issues in your code and your understanding of the problem statement.

For example, you have 2 string variables called Math and Bio but they don't have a value. Meaning, Math = Null and Bio = Null.... but you are comparing against the variable name (which is not a value)

So when user enters a value, it is compared to Null or "" and so, the comparison will be False.

If you want to define constants, one way is to say: string Bio = "Bio", string A = "A".... etc

Another way is to "get rid of all the string variables" and put the Words in quote like other answers have suggested.

If you are considering programming standards, it is best to define constants as #define. This is useful for when you are using the same values multiple times. In your case, you actually do not need to accept anything from the user

Also, VERY important. In general, when you are asking the user to send something, you need to be more clear as to what you are expecting. So, when you say "type your stream", give the valid values too in a bracket:

cout << "Type your stream (Bio/Math): ";

This is because people tend to write in different ways, all caps, all small or it could be represented by a number Bio = 0 and Math = 1. You have to be clear as to what you are expecting from user and yet try to handle the off cases

Next, you are asking inputs for NO reason and just confusing the user. The problem statement is that you have 40 students per class. so just write 3 nested loops (For each stream, for each class and for students in each class)

Finally, all you are doing is repeating the same code of total marks. You are printing the "total marks of 1 person only". You don't need the huge code for it.

What you need to do is:

  1. have a loop to collect values of multiple students. Note; 40 for each math class and 40 for Bio class. No need to actually ask the user
  2. Maintain counter for each class to the number of students in each class, in each stream and sum the total in each
  3. Combine total of each student and compare totals to determine rank. That part of your question is unclear by the way. Every student' ranks? Every class rank? What is the question?
  4. At the end, you need to take sum of marks for each stream and divide by number of students in the stream.

I can code for it but I don't want to spoon feed the answers, so please write it on your own.

  •  Tags:  
  • c
  • Related