0

How can I return first and last names of students but all characters are capitalized? How can I refer to this in a structure that it should return just uppercase.I understood how to use the structure and algorithm for sorting but I can't understand how I can convert all the characters in the first name and last name to capital

#include <iostream>
#include <cstring>
#include <string>
#include <stdio.h>
#include <fstream>
#include <conio.h>
#include <algorithm>
#include <cctype> // duze litery touppercase keycode 

using namespace std;

#define KEY_L 76 //- alfabetyczny porzadek
#define KEY_K 75 //- zapis studentow i pliku
#define KEY_U 85 //- usuniecie studenta z 2.5 srocen
#define KEY_D 68 //- dodanie nowego studenta(im,nazw,wiek,sro,nralb)
#define KEY_X 88 //- wyjscie

struct Osoba{

    string imie;
    string nazwisko;
    int nrAlbumu;
    float srOcen;
    int wiek;
    bool operator < (const Osoba &x)const
    {
        return imie>x.imie;
        return nazwisko>x.nazwisko;
    }
};


int main(){

fstream plik;   

plik.open( "studenci.txt", ios::out | ios::app);

Osoba uczniowie[20]{};

for(int i = 0; i < 5; i++){
    cout << "podaj imie i nazwisko " << i+1 << " ucznia: " ;
    cin >> uczniowie[i].imie;
    cin >> uczniowie[i].nazwisko;
}

cout << endl;

for(int i = 0; i < 5; i++){
    cout << "podaj numer albumu " << uczniowie[i].imie <<" "<< uczniowie[i].nazwisko <<" ";
    cin >> uczniowie[i].nrAlbumu;
}

cout << endl;

for(int i = 0; i < 5; i++){
    cout << "podaj wiek " << uczniowie[i].imie <<" "<< uczniowie[i].nazwisko <<" ";
    cin >> uczniowie[i].wiek;
}

cout << endl;

for(int i = 0; i < 5; i++){
    cout << "podaj srednia ocen: " << uczniowie[i].imie <<" "<< uczniowie[i].nazwisko <<" ";
    cin >> uczniowie[i].srOcen;
}

cout << endl;

for(int i = 0; i < 5; i++){
plik << uczniowie[i].imie <<" "<< uczniowie[i].nazwisko <<" ma lat "<< uczniowie[i].wiek <<" numer albumu: "<< uczniowie[i].nrAlbumu <<" oraz średnia ocen to: "<< uczniowie[i].srOcen << endl;
} 

for(int i = 0; i < 5; i++){
cout << uczniowie[i].imie <<" "<< uczniowie[i].nazwisko <<" ma lat "<< uczniowie[i].wiek <<" numer albumu: "<< uczniowie[i].nrAlbumu <<" oraz średnia ocen to: "<< uczniowie[i].srOcen << endl;
} 

cout << "Wprowadziles dane studentow do bazdy danych!" << endl;

cout << "          MENU LISTY UCZNIOW:       " << endl;
cout << "-------------------------------------------" << endl;
cout << "..(K).. - Zapis aktualnego stanu.." << endl;
cout << "..(L).. - Listowanie Listy alfabetycznie.." << endl;
cout << "..(U).. - Usuniecie studenta ze sr.ocen 2.5.." << endl;
cout << "..(D).. - Dodanie nowego studenta i jego cech(imie,nazwisko,wiek,srOcen,nrAlbumu) nie wiecej niz 20 uczniow.." << endl;
cout << "..(X).. - Wyjscie z programu.." << endl;
cout << "-------------------------------------------" << endl;
cout << "Podaj znak(w ramce) do wywołania danej metody na liscie studentow/uczniow.."<<endl;

cout << endl;

char key = getch();
int value = key;



while(value != KEY_X){
    switch(getch()){
        case KEY_L : //listowanie + duza litera calych imion i nazwisk

for(int i = 0; i < 5; i++){
cout <<"Przed sortowaniem: "<< uczniowie[i].imie <<" "<< uczniowie[i].nazwisko <<" ma lat "<< uczniowie[i].wiek <<" numer albumu: "<< uczniowie[i].nrAlbumu <<" oraz średnia ocen to: "<< uczniowie[i].srOcen << endl;
} 

cout << endl;

sort(uczniowie,uczniowie+20); // odwrote zwrocenie dla lepszej przykladnosci i zwrocenia uwagi na alfabetycznosc :)
// here code with upper-------------------------------------------------------
cout << endl;


for(int i = 0; i < 5; i++){
cout <<"Po sortowaniu: "<< uczniowie[i].imie <<" "<< uczniowie[i].nazwisko <<" ma lat "<< uczniowie[i].wiek <<" numer albumu: "<< uczniowie[i].nrAlbumu <<" oraz średnia ocen to: "<< uczniowie[i].srOcen << endl;
} 

cout << endl;




        cout << "Nazwiska zostały wylistowane i dodana zostala wielka litera imienia i nazwiska!" << endl;

        break;
    

        case KEY_D : //dodawanie ucznia > 20 uczniow w grupie

        cout<<endl;

        break;

        case KEY_U : //usuniecie z srocen 2.5 (opcjonalnie- konkretny)

        cout<<endl;

        break;

        case KEY_K : //zapis aktualnego stanu

        cout<<endl;

        break;
        
        default: //wyjscie
        exit(1);
    }
}

key = getch();
value = key;


plik.close();

plik.open( "studenci.txt", ios::in );


if(plik.is_open())
    {
        char wiersz[10000];

        while(plik.getline(wiersz,10000)) 
        {
            cout<< wiersz << endl;
            
        }
        cout << endl;
    }


    return 0;
}
Man1exter
  • 11
  • 5
  • i suggest for you to use English as language within you code and comments. i am not able to follow what is happening in you code. – onkeliroh Dec 03 '20 at 12:23
  • these are translations for me to study so that I know when describing what exactly it does, so please do not suggest it too much – Man1exter Dec 03 '20 at 13:02

1 Answers1

0

You could iterate over each character of the string with std::toupper and go from there. Here is a useful example. Other helpful methods are described in the thread, too.

On a side note, keep in mind that your operator overload will not return a first and last name. It will leave the function after encountering the first return statement. Also, you are using bool as return value instead of a string.

RoBo
  • 43
  • 7
  • in general, I mean more to refer to the structure L / – Man1exter Dec 03 '20 at 09:24
  • The way I understand your code you iterate over the first 5 students that find themselves in your list, then sort your students in alphabetical order and want to print the now top 5 students, but first and last name in upper case? You can use `std::transform(str.begin(), str.end(),str.begin(), ::toupper);` as described [here](https://stackoverflow.com/a/735215/9395961). Otherwise I will need to ask you to elaborate on your question. – RoBo Dec 03 '20 at 09:39
  • the point is that after the user has typed out 5 students, and then after pressing the key, he will return these students with capital letters as first and last name – Man1exter Dec 03 '20 at 09:48
  • is it not possible to give a condition in the sort algorithm? – Man1exter Dec 03 '20 at 09:55
  • You can add a condition function as third parameter that will help the sort algorithm to understand what entries in the Osoba instances you would like to sort. Something like that `bool sortByName(const Person &lhs, const Person &rhs) { return lhs.name < rhs.name; }` But there is no third parameter option that would return the upper case version of the strings after the sorting is done [This](http://www.cplusplus.com/articles/NhA0RXSz/) article might help you – RoBo Dec 03 '20 at 10:32
  • Thank you very much for your willingness to help, I appreciate you, have a nice day. I will continue to head over these big letters in the name and surname of the students. – Man1exter Dec 03 '20 at 11:24