Home > Mobile >  How to input text in the struct variable and then compare it?
How to input text in the struct variable and then compare it?

Time:12-06

By the task I need to calculate the percent of foreign excellent students (so the country of origin isn't "Ukraine" and the mean mark is greater than 3). But it wouldn't work and I don't know if I input text in the struct variable correctly and than compare it. Please explain this.

The code:

#include <iostream>
#include <ctime>
#include <Windows.h>
#include <cmath>
#include <iomanip>
using namespace std;

struct Student {
    char country[15];
    int course;
    float meanMark;
};
int main() {
    Student s[4];
    //strcpy_s(s.country, "hjkhj");
    //s.country = "ffff";
    for (int i = 0; i < 4; i  ) {
        cout << "Student" << i   1 << ": " << "\n";
        std::cin >> s[i].country;
        cin >> s[i].course;
        cin >> s[i].meanMark;
    }
    char u[8] = "Ukraine";
    int k = 0;
    for (int i = 0; i < 4; i  ) {
        if (s[i].country != u && s[i].meanMark > 3) {
            k  ;
        }
    }
    float percent = k / 4 * 100;
    cout << "percent = " << percent << "%" << endl;
}

Here is the input:
First goes the country of origin, than course, than mean mark

https://i.stack.imgur.com/kzdKz.png
Here is the output:
The result should be 25%
https://i.stack.imgur.com/7lhNd.png

CodePudding user response:

I think that what u need is to modify the line where you calculate the percentage to

float percent = k / 4.0 * 100.0;

CodePudding user response:

Okay so i changed char to string and it didn't work again, but then i changed places with / 4 and * 100 so the equation looks like float percent = k * 100 / 4;.... and it worked...

  •  Tags:  
  • c
  • Related