Why am I getting a segmentation fault?

0 votes
asked Feb 7, 2018 by Dylan Sluyter (130 points)
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>

using namespace std;

string drinks[5] = {"Soda", "Water", "Coffee", "Tea", "Shakes"};

int printNumberRows()
{
    for(int i = 1; i <= 5; i++)
    {
        cout << i << "." << drinks[i] << endl;
    }
}

int main()
{
    printNumberRows();
    
    cout << "Pick a drink of your choice" << endl;
    int x;
    cin >> x;
    
    return 0;
}

1 Answer

+2 votes
answered Feb 7, 2018 by anonymous
You should iterate from 0 to 4 over drinks array.

for(int i = 0; i < 5; i++)
{
    cout << i << "." << drinks[i] << endl;
}
commented Feb 7, 2018 by Dylan Sluyter (130 points)
Oh yes thank you!
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...