Code is not working

I'm new in C++ programing, so I don't no why me code is crashing.I have to sort the information and output it. This is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  #include <fcntl.h>
#include <io.h>
#include <iostream>
#include <locale>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
const int CMax=100;
struct Abonentas{
string pavarde;
int numeris;
string gatve;
} ;
void Ivedimas(Abonentas a[],int &n);
void Rikiavimas(Abonentas a[],int n);
void Isvedimas(Abonentas a[],int n);
int main()
{setlocale(LC_CTYPE,"Lithuanian");
Abonentas a[CMax];
int n=0;
Ivedimas(a,n);
Rikiavimas(a,n);
Isvedimas(a,n);
 return 0;
}
void Ivedimas(Abonentas a[],int &n){
ifstream duom("U2.txt");
char simb[20];
char simb1[15];
while(!duom.eof()){
    duom.get(simb,sizeof simb);
    a[n].pavarde=simb;
    duom>>a[n].numeris;
    duom.get(simb1,sizeof simb1);
    a[n].gatve=simb1;
    duom.ignore();
    n++;
}
duom.close();
}
void Rikiavimas(Abonentas a[],int n){
for (int i = 0; i > n - 1; i++)
    for (int j = i + 1; j < n; j++)
       if (a[j].pavarde < a[i].pavarde) {
          Abonentas p = a[i];
          a[i]= a[j];
          a[j]= p;
    }
}
void Isvedimas(Abonentas a[],int n){
ofstream rez("Rezultatai.txt");
string pav;
cout<<" Įveskine norimos gatvės pavadinimą : ";
cin>>pav;
for(int i=0;i<=n;i++){
    if(pav==a[i].gatve) rez<<a[i].pavarde<<setw(10)<<a[i].numeris<<endl;
}
rez.close();
}

Please help.
How does it crash?
where does it crash? Do you get any information about which line?
There is usually some kind of information.

I suspect it might be you writing outside the bounds of an array, for example, in your Rikiavimas function.
Last edited on
When I build it and run, program just stops
you aren't being very clear.
what do you mean by "stops"? Does it hang? are you stuck in an infinite loop?
you said it crashed and now you say it stops.

edit:
I'm not at my home machine so I've found an online compiler.
I deleted #include <io.h> as I don't think you need it, and then when I ran it I got a segmentation fault, which means that you are probably writing over the bounds of an array.
Last edited on
sorry i'm new at this. then i run the program it starts black window pops out then suddenly it writes that 1.exe has stop running
sorry i'm new at this

Sorry mate. no worries.
it looks like you might be writing past the end of an array in your program.

in your for loops you could add this line of code:
 
cout << "value of i is " << i << endl;

(if the looping variable is i of course). And then run it. when it crashes you could examine the output to see what the value of the loop variable was just before the crash.

is there any reason you are passing by reference in your Ivedimas function?
Last edited on
i think that the problem is in Rikiavimas function, but i don't know how to make that that words which i want output would be in alfabetical order.
Yes, this sort of logic:
1
2
3
4
5
6
7
for (int i = 0; i > n - 1; i++)
    for (int j = i + 1; j < n; j++)
       if (a[j].pavarde < a[i].pavarde) {
          Abonentas p = a[i];
          a[i]= a[j];
          a[j]= p;
    }

makes it easy to go beyond the bounds of an array.

print out the value of n after your line 22:
1
2
3
cout << "value of n is " << n << endl;



what is it's value?
can it be what the problem is in Ivedimas function because i tried to run just Ivedimas and count<<n ,but it stopped either
Can I just question the logic of:
for (int i = 0; i > n - 1; i++)

Would this ever return anything, if i is 0?

I would bet my hat on that it's just supposed to be:
for (int i = 0; i < n - 1; i++)
Last edited on
Topic archived. No new replies allowed.