dimanche 18 novembre 2018

Error in the code,when running in visual studio 2017 and not in codeblocks

#include<iostream>
#include<string>

struct student {
    std::string name;

    int age;

   float marks;

};

student initiateStudent(std::string name, int age, float marks)
{
    student s;
    s.age = age;
    s.marks = marks;
    s.name = name;
    return s;

}
student* highestScore(student** stud, int total)
{
    float temp = (*stud)->marks;
    student **counter= new (student*);

    *counter = *stud;
    for (int i = 0; i < total; i++)
    {
       // std::cout<<(*stud)->marks;
        if (temp < (*stud)->marks)
        {
            *counter = *stud;
            temp = (*stud)->marks;
        }
        (*stud)++;
    }
    *stud = *counter;
    delete counter;
    return *stud;
}
int main()
{
        int totalStudents = 1;

        std::string name;

        int age;

        float marks;

        std::cin >> totalStudents;

        student *stud = new student[totalStudents];

        for (int i = 0; i < totalStudents; i++) {
            std::cout << "\nEnter Name: ";
            std::cin >> name;
            std::cout << "\nEnter age: ";
            std::cin >> age;
            std::cout << "\nEnter Marks: ";
            std::cin >> marks;

            stud[i] = initiateStudent(name, age, marks);
            //std::cout << "\n Name: " << stud[i].name << "\n" << stud[i].marks;

        }

        student *topper = highestScore(&stud, totalStudents);

        //std::cout << "\nPrinting in Main : " << topper->name;
        std::cout<<std::endl << topper->name << " is the topper with " << topper->marks << " marks" << std::endl;


        delete[] stud;
        std::cin.get();

        return 0;
    }

here is the error that pops up when i am done entering values for all the students: Visual studio error

but when i run the same code in code::blocks, it runs smooth and displays the ouput:

same code on codeblocks

but even the codeblock window shows an error, idk what is the problem, any help will be great.

Aucun commentaire:

Enregistrer un commentaire