Error In code Please Help!!

Hi guys,
I am learning C++
and found this exercise about doing a "cola machine"
( one of this exercise http://www.cplusplus.com/forum/articles/12974/#msg62326 )

This is how I figure to do it (just the beginning of it) but it is not working...
Just like that...
I have no idea why...
there must be something I change i did not notice...
for it was working before...

please help
Thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <iostream>

using namespace std;
int DNumber;
string a[4];
int n;

int main() {
    a[0] = "1) Cola";
    a[1] = "2) Fanta";
    a[2] = "3) Sprite";
    a[3] = "4) Water";
    a[4] = "5) Lemonade";
    cout << "Please choose any of this flavors:" << endl;
    cout << a[0] << endl;
    cout << a[1] << endl;
    cout << a[2] << endl;
    cout << a[3] << endl;
    cout << a[4] << endl;
    cin >> DNumber;

    return 0;
}
You must include header <string>. Also any array declared as having N elements provides indexes to its elements in the range 0, N - 1
Last edited on
I already add the header
and change the n elements

but still not working it gives me this error
"warning: no newline at end of file"
That's a warning not an error. Just add a newline after line 23.
I did so
and now it works

But the Run Fails...

RUN FAILED(exit value 1, total time: 2s)
Show the current code.
Hi there,
read this: http://www.tutorialspoint.com/cprogramming/ on arrays.
Change the size of your array to five. this should fix your problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;
int DNumber;
string a[5];
int n;

int main() {
    a[0] = "1) Cola";
    a[1] = "2) Fanta";
    a[2] = "3) Sprite";
    a[3] = "4) Water";
    a[4] = "5) Lemonade";
    cout << "Please choose any of this flavors:" << endl;
    cout << a[0] << endl;
    cout << a[1] << endl;
    cout << a[2] << endl;
    cout << a[3] << endl;
    cout << a[4] << endl;
	cout << "Size of my array is : " <<sizeof(a) / sizeof(a[0]) << endl;
    cin >> DNumber;

    return 0;
}

Last edited on
Duh
yeah... that happens

thanks a lot!
Lesson learn the hard way! n.n
Topic archived. No new replies allowed.