Hi. I am pretty new to C++, and trying to understand how to use switch and case statement. So I was practicing with some bubble sorting algorithms, and it seems case 1 and 2 is not workig in my code. Can someone figure out why it's keep breaking please? The purpose of program is I am inputing random numbers to text file, and trying to sort them, and time however long it takes. Thanks for the help!

Below is my Sorting.h File

//Header File for Sorting.cpp
#include <iostream>
#include <fstream>
#include <time.h>
#include <cstdlib>

using namespace std;

class Sort{
    clock_t start,finish;   // Clock

public:
// Bubble Sort
    void Bubble(int list[], int n) {

    start = clock();    // Start clock
    double duration1;

    //Sorting
    int N,temp,i;
    for(N=0;N<n-1;N++)
        {
        for(i=0;i<(n-1-N);i++)   
        {
            if(list[i]<list[i+1])      
            {
            temp=list[i];
            list[i]=list[i+1];
            list[i]=temp;
            }
            cout << list[i] << " ";
        }
    }

    ofstream myfile;    // Output file "timing.txt"
    myfile.open ("timing.txt");

    finish = clock();   // Stop clock
    duration1 = (double)(finish - start)/CLOCKS_PER_SEC;
        myfile << "1. Execution time of Bubble Sort: " << duration1 << " seconds" << endl;

    myfile.close();     // File Close
    }


    void display(int list[], int n)
{
    cout<<"The elements of the array are:\n";
    for(int N=0;N<n;N++)
        cout<<list[N]<<" ";
}
};

Below is my CPP file

/* 
Purpose: Bubble sort, Merge sort, Heap sort, Quick sort, Bucket sort
*/
#include "Sorting.h"

int main () {
    cout << "Main Start" << endl;

// 1st Argument
    int list[1000],n,N,input;
    cout << "Input total # of numbers to be sorted: "; 

    // Output Unsorted Numbers
    ofstream myfile;
    myfile.open ("input-N.txt");

    srand((unsigned)time(0)); 
    cin >> N;   // Input # of numbers to be sorted
        cout << "\nUnsorted Numbers:\n";
    for(int index=0; index<N; index++) { 
        list[N] = (rand()%10000)+100; // Random number of 100 <= N <= 10,000
        cout << list[N] << endl; 
        myfile << list[N] << endl;
    }
    myfile.close();


// 2nd Argument
    {
    cout << "\n\nPick a Computing Mode:\n";
    cout << "1. Bubble Sort\n";
    cout << "2. View Unsorted List again\n";
    cout << "3. Exit\n";
    cout << "\nSelection: ";
    cin >> input;

    switch (input) {
        Sort Sorting;
        case 1:
            Sorting.Bubble(list,n);
            break;
        case 2:
            Sorting.display(list,n);
            break;

        case 6: break;
        default: cout << "\nInvalid Choice";
        }
    return 0;
    }
}

Since your description is quite vague, we don't really know what to look for.
Since all switches break, I don't understand what you're describing.

All I can suggest is make sure your methods are correct.

By the way, the statement
list[N] = (rand()%10000)+100; // Random number of 100 <= N <= 10,000
gives random numbers from 100 to 10099

Perhaps it would be helpful for us, if you post the errors you get while you compile the code.

Can someone figure out why it's keep breaking please?

In your code, you have break statement, thats why its breaking. There is no problem with switch case as far as I know. Perhaps there might be error in Sorting.Bubble(list,n); or Sorting.display(list,n); methods.

Also, keep this statement Sort Sorting; outside switch case.

switch statements use an int type so if you are storing the value you are getting from the user in a char variable than you need to use a char for your cases. 1 is not the same as '1'. Your code should look like this.

switch(input)
{
    case '1': //...
              break;
    case '2': //...
              break;
    //...
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.