Anonymní profil Peterop – Programujte.com
 x   TIP: Přetáhni ikonu na hlavní panel pro připnutí webu

Anonymní profil Peterop – Programujte.comAnonymní profil Peterop – Programujte.com

 

Příspěvky odeslané z IP adresy 178.41.23.–

Peterop
C / C++ › program v c++ problém
30. 1. 2012   #153051

#2 KIIV
prikladám kód:

#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <conio.h>


typedef struct clovek{  
        char meno[10];
        char priezv[12];
        char narod[15];
        char mesto[15];
        int exist;  //Urcuje ci kontakt je ulozeny.
        }clovek;
       
clovek *p;   // Pointer na zaciatok pola struktur.
FILE *f;     // Pointer na vystupny subor;
FILE *r;     // Pointer na vstupny subor
int poc;     // Urcuje rozsah pola struktur.
int uloz=0;  // Urcujuce kolko kontaktov je ulozenych.
int zac=1;


/******************************************************************************/
/*Zoznam uplnych fukncnych prototypov funkcii:                                */

void menu();                       //Funckia pre vykreslovanie menu                       
void reset();                      //Funkcia pre alokovanie a resetovanie pamate
void pridaj();                     //Pridavanie kontaktov do pamate
void uprav();                      //Upravvanie kontaktov
void pis_obr();                    //Zobrazenie kontaktov na obrazovku
void pis_sub();                    //Vystup do suboru
void najdi();                      //Hladanie kontaktu- menu hladania
void vymaz();                      //Vymazanie kontaktu - podla priezviska     
void utried_m();                   //Utriedi kontakty - podla priezviska
void p_mena();                     //Vyhlada podla krstneho mena
void p_priezv();                   //Vyhlada podla priezviska
void p_narod();                    //Vyhlada podla
void poriadok();                   //Presunutie zaznamov tesne vedla seba,
                                   //aby neboli rozhadzane v poli.
void kontrola();
void vymena(clovek *,clovek *);
void txt_citaj();
void tlac_zdroj();

/******************************************************************************/


using namespace std;


int main(int argc, char *argv[])
{
    reset();  // vytvorime pole struktur (default 100)
    txt_citaj(); // Nacitame kontakty zo suboru.
    menu();
    tlac_zdroj(); //Vytlacime kontakty do suboru z ktoreho budeme neskor citat
    return EXIT_SUCCESS;
}

void reset()
{
    
     system("cls");
     if(zac==0)
     {
               printf("Kolko pamatovach miest chcete vytvorit?: ");
               scanf("%d",&poc);
     }
     else
     {
         poc=100;
         zac=0;
     }
    
     if((p=(clovek*) malloc(sizeof(clovek)*poc))==NULL){
                                                        printf("\nChyba pri vytvarani zoznamu!\n");
                                                        system("pause");
                                                        reset();
                                                        }
     else{
          for(int i=0;i<poc;i++){
                                 p[i].exist=0;
                                 }
          uloz=0;
          printf("\nZoznam bol uspesne vytvoreny\n");
          system("pause");
          return;
          }
}

void menu()
{
     char volba;
     do{
     system("color 2E");
     system("cls");
     printf("\t   *************** Program Zoznam osob *************\n\n\n\n");
     printf("\t1. Pridat kontakt.\n");
     printf("\t2. Upravit kontakt.\n");
     printf("\t3. Zmazat kontakt.\n");
     printf("\t4. Vypisat kontakty na obrazovku.\n");
     printf("\t5. Vypisat kontakty do suboru.\n");
     printf("\t6. Vyhladat kontakt.\n");
     printf("\t7. Nacitat kontakty zo suboru.\n");
     printf("\t8. Resetovat pamat.\n\n");
     printf("\t9. Koniec programu.\n\n\n");
     printf("Ulozenych kontaktov: %d Volna pamat: %d Celkova pamat: %d\n\n\n",uloz,(poc-uloz),poc);
     printf("\t\tVasa volba: ");
     fflush(stdin);
     volba=getch();
    
     //Osetrenie vstupu. Ak volbe nieje ziadna z moznosti menu prirad jej hodnotu 0. To znamena ze sa vypise chybova hlaska.
     if (!(volba=='1' || volba=='2' || volba=='3' || volba=='4' || volba=='5' || volba=='6' || volba=='7'
         || volba=='8' || volba=='9')) volba='0';
    
     switch (volba){
            case '0':
                 system("cls");
                 system("color 04");
                 printf("\nZadali ste chybnu volbu\n\n");
                 system("pause");
                 break;
            case '1':
                 pridaj();
                 break;
            case '2':
                 uprav();
                 break;
            case '3':
                 vymaz();
                 poriadok();
                 break;
            case '4':
                 pis_obr();
                 break;
            case '5':
                 pis_sub();              
                 break;
            case '6':
                 najdi();
                 break;
            case '7':
                 txt_citaj();
                 break;
            case '8':
                 free(p);
                 p=NULL;
                 reset();
                 break;
            case 'k':
                 kontrola();
                 break;
                    }
     if(volba=='1' || volba=='2' || volba=='3' || volba=='7')utried_m();
     }while(volba!='9');
}

void pridaj()
{
     int i=0;
     int hotovo=1;           //Ukazuje ci kontakt uz bol ulozeny alebo nie.    
    
     system("cls");
    
     //Otestovanie volnej pamate, ak je malo miesta navrat do menu.
     if(uloz==poc){
                   fprintf(stdout,"Nie je volna pamat\n");
                   system("pause");
                   return;
                   }
     /*Cyklus precesava pole zaznamov. Ak najde pradzne miesto a zaroven este
     nebol kontakt ulozeny (hotovo=1), takt od uzivately nacita udaje.       */
                
     while(i<=poc){
                  if(p[i].exist==0 && hotovo){
                                              hotovo=0;
                                              uloz++;
                                              p[i].exist=1;
                                              puts("Meno: ");
                                              gets(p[i].meno);
                                              puts("Priezvisko: ");
                                              gets(p[i].priezv);
                                              puts("Datum narodenia: ");
                                              gets(p[i].narod);
                                              puts("Mesto: ");
                                              gets(p[i].mesto);
                                              }
                  i++;
                  }
     printf("\nKontakt bol ulozeny.\n\n");
     system("pause");
     return;
}

void pis_obr()
{
     int i=0;
     int vypis=0;    //Premenna ukazuje kolko kontaktov bolo vypisanych na obrazovku
     int prazdny=1;  //Overenie ci mame ulozene nejake kontakty.
    
     system("cls");
     for(i=0;i<poc;i++){
                        if(p[i].exist==1){                                            //Narazili sme na kontakt.
                                          prazdny=0;                                  //Mame teda nieco ulozene, tak to vypiseme.
                                          printf("Priezvisko: %s\n",p[i].priezv);
                                          printf("Meno: %s\n",p[i].meno);
                                          printf("Datum narodenia: %s\n",p[i].narod);
                                          printf("Mesto: %s\n",p[i].mesto);
                                          printf("\n\n");
                                          vypis++;                                   //Inkrementujeme pocitadlo.
                                          if(vypis==3){                              //Ak pocitadlo je rovne 3.
                                                       vypis=0;                      //Vynulujeme ho.
                                                       system("pause");              //A odpauzujeme.
                                                       system("cls");                //Vycistíme.
                                                       }
                                          }
                        }
     if(prazdny==1){                                                                 //Ak pocas cyklu nenarazime na ziadny kontakt
                    printf("\nZoznam je prazdny!\n");                                 //Prazdny sa teda stale rovna 0
                    }
     system("pause");
     return;
}

void pis_sub()
{
     int prazdny=1;   //Overenie ci mame ulozene nejake kontakty, predpokladame ze ziadne.
    
     if((f=fopen("zoznam.txt","w"))==NULL){                                      //Test otvarania suboru
                                             printf("Chyba pri otvarani suboru, nie je mozne zapisovat na disk!\n");
                                             return;
                                             }
     for(int i=0;i<poc;i++){
                            if(p[i].exist==1){                                      //Rovnaky cyklus ako pri vystupe na obrazovku.
                                              prazdny=0;
                                              fprintf(f,"Priezvisko: %s\n",p[i].priezv);
                                              fprintf(f,"Meno: %s\n",p[i].meno);
                                              fprintf(f,"Datum narodenia: %s\n",p[i].narod);
                                              fprintf(f,"Mesto: %s\n",p[i].mesto);
                                              fprintf(f,"\n\n");
                                              }
                            }
     if(prazdny==1){
                  printf("\nZoznam je prazdny!\n");
                  system("pause");
                  }
     else{
          printf("Zoznam bol uspesne vytlaceny\n\n");
          system("pause");
          }
     fclose(f);
                                                     //Zatvorime subor.
     return;
}                
                                             
    
    
void uprav()
{
     char priezv[10];
     int i=0;
     int j=0;
     int najdeny=0;
     int prazdny=0;
         
     system("cls");
    
     for(i=0;i<poc;i++){
                        if(p[i].exist){
                                       prazdny=1;
                                       }
                        }
     if(prazdny==0){
                    printf("\nZoznam je prazdny!\n");
                    system("pause");
                    return;
                    }

     printf("\nZadajte meno kontaktu, ktory chcete upravit: \nPoz: upravi posledny vyhovujuci kontakt!\n");
     scanf("%s",&priezv);
     for(i=0;i<poc;i++){
                        if(strcmp(p[i].priezv,priezv)==0){
                                                      j=i;
                                                      najdeny=1;
                                                      }
                        }
         
     if(najdeny){
                 prazdny=1;
                 printf("\nKontakt najdeny, mozete zadavat nove udaje:\n ");
                 printf("\nMeno: ");
                 scanf("%s",p[j].meno);
                 printf("\nPriezvisko: ");
                 scanf("%s",p[j].priezv);
                 printf("\nDatum narodenia: ");
                 scanf("%s",p[j].narod);
                 printf("\nMesto: ");
                 scanf("%s",p[j].mesto);
                 system("pause");
                 return;
                 }
          else{
               printf("\nKontakt nebol najdeny\n");
               system("pause");
               return;
               }
}

void najdi()
{
     char vol;
     system("cls");
     int prazdny=0;
    
     if(uloz==0)
     {
                system("cls");
                printf("Zoznam je prazdny\n");
                system("pause");
                return;
     }
                   
     printf("\nPodla akeho kluca chcete vyhladavat?\n");
     printf("1. Podla krstneho mena.\n");
     printf("2. Podla priezviska.\n");
     printf("3. Podla datumu narodenia.\n");
     printf("4. Navrat do menu.\n\n");
     printf("\tVasa volba: ");
     scanf("%d",&vol);
         
     switch (vol){
            case 1:
                 p_mena();
                 break;
            case 2:
                 p_priezv();
                 break;
            case 3:
                 p_narod();
                 break;
            case 4:
                 return;
                 break;
                 }
}


void p_mena()
{
     char meno[10];
     int i;
     int najdeny=0;

     printf("\nZadajte krstne meno kontaktu, ktory chcete najst: ");
     scanf("%s",&meno);
     for(i=0;i<poc;i++){
                        if(strcmp(p[i].meno,meno)==0){
                                                      najdeny=1;
                                                      printf("Meno: %s\n",p[i].meno);
                                                      printf("Priezvisko: %s\n",p[i].priezv);
                                                      printf("Datum narodenia: %s\n",p[i].narod);
                                                      printf("Mesto: %s\n",p[i].mesto);
                                                      printf("\n\n");
                                                      }
                        }

          if(najdeny==0){
                         printf("\nKontakt nebol najdeny\n");
                         system("pause");
                         return;
                         }
          system("pause");
          return;
         
}

void p_priezv()
{
     char priezv[15];
     int i;
     int najdeny=0;

     printf("\nZadajte priezvisko kontaktu, ktory chcete najst: ");
     scanf("%s",&priezv);
     for(i=0;i<poc;i++){
                        if(strcmp(p[i].priezv,priezv)==0){
                                                          najdeny=1;
                                                          printf("Meno: %s\n",p[i].meno);
                                                          printf("Priezvisko: %s\n",p[i].priezv);
                                                          printf("Datum narodenia: %s\n",p[i].narod);
                                                          printf("Mesto: %s\n",p[i].mesto);
                                                          printf("\n\n");
                                                          }
                        }

          if(najdeny==0){
                         printf("\nKontakt nebol najdeny\n");
                         system("pause");
                         return;
                         }
          system("pause");
          return;
         
}

void p_narod()
{
     char narod[15];
     int i;
     int najdeny=0;


     printf("\nZadajte datum narod kontaktu, ktory chcete najst: ");
     scanf("%s",&narod);
     for(i=0;i<poc;i++){
     if(strcmp(p[i].narod,narod)==0){
                                     najdeny=1;
                                     printf("Meno: %s\n",p[i].meno);
                                     printf("Priezvisko: %s\n",p[i].priezv);
                                     printf("Datum narod: %s\n",p[i].narod);
                                     printf("Mesto: %s\n",p[i].mesto);
                                     printf("\n\n");
                                     }
                        }

          if(najdeny==0){
                         printf("\nKontakt nebol najdeny\n");
                         system("pause");
                         return;
                         }
          system("pause");
          return;
         
}

void vymaz()
{
     int i;
     char priezv[12];
     int prazdny=0;
     int najdeny=0;
    
     for(int j=0;j<poc;j++){
                            if(p[j].exist){
                                           prazdny=1;
                                           }
                            }
     if(prazdny==0){
                    printf("\nZoznam je prazdny!\n");
                    system("pause");
                    return;
                    }

     printf("Zadajte priezvisko kontaktu, ktory chcete vymazat: ");
     scanf("%s",priezv);
     for(i=0;i<poc;i++)
     {
             if(strcmp(p[i].priezv,priezv)==0)
             {
                                              najdeny=1;
                                              break;
             }
     }
     if(najdeny)
     {
                p[i].exist=0;
                uloz--;
                printf("Kontakt bol uspene vymazany.\n");
     }
     else printf("Kontakt nebol najdeny\n");
     system("pause");
     return;
}

void vymena(clovek *x, clovek *y)
{
     clovek pom;
     pom=*x;
     *x=*y;
     *y=pom;
     return;
}

void utried_m()
{
     int i=0;
     int j=0;
        
     do{
         j=i;
         if(p[j].exist==1 && p[j+1].exist==1){
                          while(j>=0 && strcmp(p[j].priezv,p[j+1].priezv)==1){
                                                              vymena(&p[j],&p[j+1]);
                                                              j--;
                                                              }
                          if(strcmp(p[j].priezv,p[j+1].priezv)==0 &&
                             strcmp(p[j].meno,p[j+1].meno)==1)
                             {
                                                              vymena(&p[j],&p[j+1]);
                             }
                                                             
                          }
         i++;
         }while(i<=poc);
         return;
}                                         

void poriadok()
{
     int i=0;
     int presun=0;
    
     do{
        if(p[i].exist==0 && p[i+1].exist==1){
                         p[i]=p[i+1];
                         p[i+1].exist=0;
                         presun==1;
                         }
        else presun=0;
        i++;
        }while(i<=poc);
}          
                     
void kontrola()
{
     system("cls");
     printf("Kontrolný vypis\n\n");
     for(int i=0;i<poc;i++)
     {
             printf("%d\n",p[i].exist);
     }
     system("pause");
     return;
}

void tlac_zdroj()
{
     system("cls");
     int prazdny=1;  // predpokladame ze zoznam je prazdny
                                              
     if(uloz==0){system("pause"); return;}

     if((r=fopen("data.txt","w"))==NULL){      //otvorime subor pre pisanie a overime jeho otvorenie
                                               printf("Chyba pri otvarani suboru\n");
                                               system("pause");
                                               return;
                                               }
     else{                                     //Ak bol subor uspesne otvoreny zacneme do neho zapisovat.
          //fprintf(r,"%d\n",uloz);
          for(int i=0;i<poc;i++){             //Prejdeme celym polom a zapiseme existujuce zaznamy.
                  if(p[i].exist==1){
                                    fprintf(r,"%s %s %s %s %s %s\n",p[i].meno,p[i].priezv,p[i].narod,p[i].mesto);
                                   
                                   }
                     

Peterop
C / C++ › program v c++ problém
30. 1. 2012   #153049

Ahojte,
potrebujem radu. Mám program spraveny v c++ na manipuláciu kontaktov. no pri spustení v MVS vypíše chybu: Unhandled exception at 0x01245870 in zadanie.exe: 0xC0000005: Access violation writing location 0x00000000.

keď ho spustím ako exe súbor tak zasa windovs vypíše: program prestal pracovať.
problém bude v databazovom súbore data.txt, pretože ak je prazdny tak to funguje. Akonáhle však súbor naplním údajmi, vyhadzuje tieto chyby.

viete v čom môže byť problém?
ďakujem

 

 

Hostujeme u Českého hostingu       ISSN 1801-1586       ⇡ Nahoru Webtea.cz logo © 20032024 Programujte.com
Zasadilo a pěstuje Webtea.cz, šéfredaktor Lukáš Churý