Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed Segmentation Error please help

0 votes
asked Feb 8, 2018 by zim
closed Feb 9, 2018
Hello all,

For my homework in c++ I must create a program that inputs 5 cities and prints them in order and reverse order by the users choice using arrays and seperate functions. The homework requires that you use all these functions to operate.  The program runs fine until I enter o or r for reverse order. I get a segmentation error. I think I am printing or passing the strings incorrectly.

Thanks for the help guys.

#include <iostream>
#include <string>
using namespace std;

void getCities (string cities[5]);
int getChoice (int);
void displayInOrder (string o[5]);  //is this declared correctly?
void displayInReverse (string r[5]);    //same for this one

int main ()
{
  string cities[6];
  getCities (cities);
  int result;
  int number = 0;
  result = getChoice (number);
  if (result == 1)
    {
      displayInOrder (&cities[5]);
    }
  else if (result == 2)
    {
      displayInReverse (&cities[5]);
    }
  else
    {
      cout << "Invalid entry - Must be O or R!" << endl;

    }

  //This code allows for the program to re run if the user chooses y
  string w = " ";
  cout << "Run program again (y/n)? " << endl;
  getline (cin, w);
  if (w == "y")
    {
      main ();
    }
  else
    system ("PAUSE");
  return 0;
}

//This function gets the names of all the cities
void getCities (string cities[6])
{
  cout << "Enter 5 cities" << endl;
  getline (cin, cities[0]);
  cin.ignore ();
  getline (cin, cities[1]);
  cin.ignore ();
  getline (cin, cities[2]);
  cin.ignore ();
  getline (cin, cities[3]);
  cin.ignore ();
  getline (cin, cities[4]);
}

int
getChoice (int value)
{
  string choice = " ";

  cout << "How do you want to see the cities displayed?" << endl;
  cout << "Enter o for in order, or r for in reverse (R):" << endl;
  getline (cin, choice);
  if (choice == "o" || choice == "O")
    {
      value = 1;
    }
  else if (choice == "r" || choice == "R")
    {
      value = 2;
    }
  return value;
}

//this function displays the cities in order
void displayInOrder (string o[5])
{
  cout << "Here are the cities in order: " << endl;
  cout << o[0] << " " << o[1] << " " << o[2] << " " << o[3] << " " << o[4] <<
    endl;   //the error seems to lie somewhere in here and the same in the reverse function

}

//this function displays the cities in reverse
void displayInReverse (string r[6])
{
  cout << "Here are the cities in reverse order: " << endl;
  cout << r[4] << " " << r[3] << " " << r[2] << " " << r[1] << " " << r[0] <<
    endl;
}
closed with the note: Figured it out

1 Answer

0 votes
answered Feb 9, 2018 by zim
I have figured this out. No need for help anymore.
commented Feb 10, 2018 by PC88
What was your solution to the problem?
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.
...