1

I get this error, so I got this suggestion to try methods... I had an epic fail. This is my code, I don't know what I'm doing wrong, please explain and correct me, I want to learn, not just fix the problem:

#include <iostream> #include <string> #include <stdlib.h> #include <windows.h> using namespace std; class datum { public: int leto; int mesec; int dan; }; class racun { string naslov; float cena; // Skupna cena na računu int i; // Števec public: datum izdaje; //racuna void nastavi_izracunaj_izpisi() { izdaje.dan = rand() % 30 + 1; //Dan izdaje.mesec = rand() % 12 + 1; //Mesec izdaje.leto = rand() % 30 + 1985; //Leto i = rand() % 100; // Koliko računov smo imeli. int produkti; produkti = rand() % i + 200; //Koliko produktov smo imeli int produkt1[200]; //cena prvega produkta int produkt2[200]; //cena drugega produkta int a; //števec produktov a=0; //ki ga nastavimo na nič do { produkt1[a] = rand() % 200; produkt2[a] = rand() % 200; a=a+1; }while(a!=produkti); int b; //kateri produkt bo izpisalo b = rand() % 200; cout<<"Kupili ste:"<<produkti<<" produktov"<<endl; //izpis za produkte do { cena=produkt1[b]+produkt2[b]; i++; }while(i!= produkti); cout<<"Cena računa brez ddv je: "<<cena<<endl; //DDV float ddv = 1.12797374897; float cena2; float cenaddv; cena2=cena/ddv; cenaddv=cena+cena2; cout<<"Cena računa z ddv je: "<<cenaddv<<endl; } }; int main() { racun nekaj; nekaj::nastavi_izracunaj_izpisi(); //Nena dela, FAG system("PAUSE"); return 0; } 
5
  • 4
    nekaj::nastavi_izracunaj_izpisi() should be nekaj.nastavi_izracunaj_izpisi() - you're calling a member function on an object. :: is used for static member function, with the class name rather than the object name as prefix. Commented Nov 25, 2013 at 1:05
  • now my programm just crashes... Why is that? :O Commented Nov 25, 2013 at 1:13
  • I don't know - I haven't looked at it that carefully. It's more useful if you learn how to find out rather than me working it out and telling you - I suggest you start by putting std::cout statements throughout the code until you can see exactly which line is crashing (because the output from the prior line's shown, but not the following line). If you can't work out why it crashes there, then ask us. Alternatively, use an interactive debugger like gdb or Visual Studio's. Commented Nov 25, 2013 at 1:19
  • I'm now looking where my code crashes... If I find anything I'll post here. Commented Nov 25, 2013 at 1:23
  • wow, I found the error, not sure why it is happening. It is in this line: "int produkt1[200]; //cena prvega produkta" "int produkt2[200]; //cena drugega produkta" Commented Nov 25, 2013 at 1:26

1 Answer 1

2
nekaj::nastavi_izracunaj_izpisi(); // should be nekaj.nastavi_izracunaj_izpisi(); 

It's the right way to call a method. Or to access a member (as @Ben Voigt said).

// Tehre an integer division by 0 on this line produkti = rand() % i + 200; // because this line always return 0 i = rand() % 100; // Koliko računov smo imeli. 
Sign up to request clarification or add additional context in comments.

8 Comments

s/call a method/access a member/
Euh... It compiled but it crashed your console!?!
yes, It compiled, then when I run it, it crashed my console (program), as I'm using console app...
It's because of this: "int produkt1[200]; //cena prvega produkta", what is wrong whit this line?
No it's not this line. I strongly suggest you to use a debugger, to place a breakpoint and to run your code line by line. Visual studio express if you are on Windows, Eclipse or Netbeans on a linux distro (or Windows again). See my edit.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.