Unknown Error

Im having some odd problem with my code... dev c++ compiles it without errors yet program dosent work, it crashes. What might be the problem here?

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
  #include <iostream>



using namespace std;

main(){
       string rzecz;
       string b[100];
       int a[100];
       int war,kalor,i;
       
       
       cout << "Dodac posilek?" << endl;
       cout << "1.Dodaj 2.Zakoncz" << endl;
       
       cin >> war; 
       if ( war == 1) i=i+1;
       cout << "Co to bylo?" << endl;
       cin >> rzecz;
       cout << "Ile mial kalorii" << endl;
       cin >> kalor;
       b[i]=rzecz;
       a[i]=kalor;
       
       return 0;
       }
       
you haven't initialized war and i.

also b[i]=rzecz; does not make sense. rzecz is itself a string.
War and i are initialized after main (){.
Rzecz is a name of variable, variable that is typed by the program user. Lets say its a fruit name. Is that wrong approach ?
It's just i that is not initialised.

war is on line 17 with the cin.

b[i]=rzecz; is OK - b is an array of strings.

So the lesson here is to always initialise your variables. I like to do at the same time as declaration - 1 variable per line so you can comment them if necessary. Use meaningful names for variables.

You also need loops - the array size is 100?

HTH
int war,kalor,i; at line 11 dosent mean they are initialisated?

@TheIdeasMan
Yes ill add loops later on, this program does nothing atm. Problem I have is not with compilation itself but program crushing. ( When runing it happens after line 22)

Can someone try to compile this and run? maybye its the problem with my computer.


OKAY, i get it now . Thank you !!!!
Last edited on
int war,kalor,i; at line 11 dosent mean they are initialisated?

No, they're not initialised. You haven't supplied any initial values for these.

Edit: On line 17, you assign a value to war before trying to use it, so that's OK. On line 22, you do the same for kalor. But on line 18, you're attempting to use i before initialising it.
Last edited on
Yeah i had to read book again. DECLARATION is not INITIALISATION.
I mark it as solved. Thank you all
Topic archived. No new replies allowed.