Text input and console crash ...

I made this programme and when I insert more than 10 digits this shows:http://prntscr.com/39l3pw p.s. the programme is supposed to make sum of last m digits of number n please help? also I have an question

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

using namespace std;

main()
{
      int n,m,broj,br,zbir;
      cin >> n;
      cin >> m;
      broj = 1;
      zbir = 0;
      int i=0,oj[i];
      for(i=0;i<m;i++)
      {
      broj = broj*10;                 
      }
      br=n%broj;
      for(i=0;i<m;i++)
      {
      oj[i] = br - (br/10)*10;
      br=br/10;
      zbir += oj[i];           
      }
      cout << "zbir je:" << zbir;
      system("PAUSE");
      return 0;
}


I need help with imputing text, so how do I make a programme which takes the first letter of a word that is inputed and writes it? like:

fruit -> f

sorry for bad english :P
main() should be int main()

What size is the array supposed to be? Right now it appears you are creating an array oj with 0 elements.
int i=0,oj[i];

Then later you try to access elements up to m in array oj that won't exist.
1
2
3
for(i=0;i<m;i++)
      {
      oj[i] = br - (br/10)*10;
Thanks for reply, i have changed array size to m, and i don't thhink that int main() is the problem because I always use main() and it is not a problem, still same problem, help??
If I only use main() - the program doesn't even compile.

Try changing to data type of n if you want it to work for larger numbers. <int> only allows you to go up so far. For example, I made n a long long int.

123456789012345//n
10//m
----
zbir je:31 



Edit: Also - broj and br may have to be redefined as well. If m >= 10, the formula doesn't give the correct result.
Last edited on
Topic archived. No new replies allowed.