0

I am brand new to stack overflow here so thank you all for your patience in helping me with my issue. I am writing a program in C++ that implements the insertion sort by sorting numbers from a .txt file. It accepts a file, shows the contents and then asks the user if they want to sort the numbers. When I key "y" it is supposed to initiate the insertion sort algorithm in my code. However right now all it does is finish compiling. Any advice is greatly appreciated.

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

//Read file input and display contents
ifstream& readfile(ifstream& file)
{
    string line;
    if (getline(file, line))
    {
        cout << line;
    }
    return file;
}

int main()
{
    string fileName, line, r;
    int n, i, j, k, temp;
    int a[n];
    ifstream fs;

    cout << "enter file: ";
    cin >> fileName;

    ifstream file(fileName);
    if (file.is_open())
    {
        while (readfile(file))
        {
        }
    }
    cout << endl << endl << "Sort File? y or n? ";
    cin >> r;
    if (r == "y")
    {
        for (i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        for (i = 1; i < n; i++)
        {
            for (j = i; j >= 1; j--)
            {
                if (a[j] < a[j - 1])
                {
                    temp = a[j];
                    a[j] = a[j - 1];
                    a[j - 1] = temp;
                }
                else
                {
                    break;
                }
            }
        }
        for (k = 0; k < n; k++)
        {
            cout << a[k] << endl;
        }
    }
    else
    {
        cout << endl << "error" << endl;
    }

    cin.get();
    return 0;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
A.Borger
  • 1
  • 1
  • You should set `n` properly before `int a[n];` – MikeCAT Sep 21 '15 at 15:04
  • 1
    @MikeCAT VLA's arent standard c++ anyways. – πάντα ῥεῖ Sep 21 '15 at 15:05
  • 1
    `When I key "y" it is supposed to initiate the insertion sort algorithm in my code. However right now all it does is finish compiling. Any advice is greatly appreciated.` I'm not sure your problem is clear. when you compile your program (different than running the resulting program), do you get any errors, or does it indeed "finished compiling"? When you run your program after compiling it, what happens? Are there errors? Does it simply terminate without doing anything? – Steve Sep 21 '15 at 15:11
  • It simply terminates without doing anything. when typing "n" instead of "y" when asked to "sort file" it works properly and outputs "error", but when the input is "y" nothing else happens and my compiler says run complete. Something isn't initiating the sorting algorithm and spiting out the numbers in a sorted array. thats the issue. No errors – A.Borger Sep 21 '15 at 15:16
  • This doesn't do what you expect: `if ( r == "y")`: it compares string pointers rather than the character you entered. – Kenney Sep 21 '15 at 15:18
  • @Kenney: No, that's one of the few things that are correct. `r` is a poor variable name and its definition was buried in the bad formatting but it's a `std::string` and that comparison will work. – Blastfurnace Sep 21 '15 at 15:34
  • Add this: `-pedantic -pedantic-errors -Wall -Wextra -Werror -Wconversion` to the compiler command line and you'll see all sorts of nastiness that's currently being hidden from you. – user4581301 Sep 21 '15 at 15:41
  • @Blastfurnace ah allright, missed that! Thanks! In that case though, after entering "y", you are supposed to enter `n` inputs (in the `for (..) { cin >> a[i]; }` (whatever `n` is); also the `cin.get()` at the end waits for more input... – Kenney Sep 21 '15 at 15:42

1 Answers1

0

n is not initialized.

for (i=0;i<n;i++) 
{
  cin >> a[i];
}

here nothing happen because (if your are lucky...) n=0. Initialize n to 5 if you want only 5 entries.

or

  • use STL containers like std::vector for example, you are using C like language here...
  • Change your end condition in your loop when users initialize insertion

For example if user insert "stop" word:

std::vector<int> vIntegers; 
std::string input; 
int n = 0; 

while ( input != "stop") 
{ 
   std::cin >> input; 
   // test if integer here 
   { 
      vIntegers.push_back(n); 
   } 
}

here the post to test if a string is an integer How do I check if a C++ string is an int?

Community
  • 1
  • 1
Foebius
  • 11
  • 4
  • I actually don't want n to be fixed. I'd rather have n be dynamic based on user input. Is this possible? – A.Borger Sep 21 '15 at 15:55