Create a program that contains the following functions:
-create two dynamic stacks with integer data contained in an external file.
-Sort items in stacks by the selection sort method
-Record outputs in an external file.
Main function main ()-menu selection functions and check the status of the data

I dont know if this is right and i cant do the input function/it is wrong/ can somebody help me and if i have something wrong with this code /sorry for bad english/-Example: input: s1: 1,3,5,9,10 / s2:2,3,4,6,7,11 sorting with selection sort-> output: 1,2,3,3,4,5,6,7,8,9,10,11

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include<fstream>

using namespace std;

typedef struct elem {
    int key;
    elem *next;
} *stack;

void push(stack& S, int n) {
    stack p = S;
    S = new elem;
    S->key = n;
    S->next = p;
}

int pop(stack& S) {
    int n;
    stack p = S;
    n = S->key;
    S = S->next;
    delete p;
    return n;
}

void file_input()
{
    int a;

    ifstream file_1;
    file_1.open("file.txt", ios::in);
    if (file_1)
    {
        while (!file_1.eof())
        {
            file_1 >> a;
            push(s1, a);
        }
        file_1.close();
        cout << endl << "Done" << endl << endl;
    }
    else                          
        cout << "Error" << endl;

int sort2(stack& s1, stack& s2) {
    int m;
    m = pop(s1);
    while (s1)
    if (m<s1->key) push(s2, pop(s1));
    else { push(s2, m); m = pop(s1); }
    return m;
}

void sort(stack& s1) {
    stack s2 = NULL;
    ofstream file;

    file.open("output.txt", ios::out);
    if (file) {
        while (s1) {
            file << sort2(s1, s2) << " ";
            if (s2) file << sort2(s2, s1) << " ";
        }
        file << endl;
        file.close();
        cout << "Saved in output.txt";
    }
    else {
        cout << "output.txt can't open";
        while (s1) {
            cout << sort2(s1, s2) << " ";
            if (s2) cout << sort2(s2, s1) << " ";
        }
    }
    cout << endl;
}


int main() {
    stack Stack = NULL;
    int n, t;

    srand(static_cast<unsigned int>(time(NULL)));
    while (1) {
        cout << "0 for EXIT, N = ";
        cin >> n;
        if (n<1) return 0;

        for (int i = 0; i<n; i++) {
            t = rand() % 1000;
            cout << t << " ";
            push(Stack, t);
        }
        cout << "\n\nSorting...\n";
        sort(Stack);
        cout << endl << endl;
    }
}

2 immediate problems I see with your file_input function, is the closing brace is missing and you're trying to access a variable that doesn't exist, s1.

Aside from that and assuming the data for each stack is on separate lines in one file and that you intend for the stack to be LIFO, then a third stack will be the easiest way to get the sort order you want:

#include <iostream>
#include<fstream>
#include <sstream>
using namespace std;
typedef struct elem
{
    int key = 0;
    elem *next = NULL;
} *stack;
void push(stack& S, int n)
{
    stack p = new elem;
    p->key = n;
    p->next = S;
    S = p;

}
int pop(stack& S)
{
    int n;
    stack p = S;
    n = S->key;
    S = S->next;
    delete p;
    return n;
}

void file_input(string fileName, stack& s1, stack& s2)
{
    int a;
    ifstream file_1;
    file_1.open(fileName, ios::in);
    if (file_1)
    {
        string temp = "";
        if (getline(file_1, temp))
        {
            stringstream ss(temp);
            while (ss >> a)
            {
                push(s1, a);
                char charTemp;
                ss >> charTemp;
            }
        }
        if (getline(file_1, temp))
        {
            stringstream ss(temp);
            while (ss >> a)
            {
                push(s2, a);
                char charTemp;
                ss >> charTemp;
            }
        }
        file_1.close();
        cout << endl << "Done" << endl << endl;
    }
    else
    {
        cout << "Error" << endl;
    }
}
int MaxElem(stack& s1, stack& s2)
{
    if (s1->key > s2->key)
    {
        return pop(s1);
    }
    else
    {
        return pop(s2);
    }
}
void sort(stack& s1, stack& s2)
{
    stack s3 = NULL;
    while (s1 != NULL && s2 != NULL)
    {
        push(s3, MaxElem(s1, s2));
    }
    if (s1 != NULL)
    {
        while (s1 != NULL)
        {
            push(s3, pop(s1));
        }
    }
    else if (s2 != NULL)
    {
        while (s2 != NULL)
        {
            push(s3, pop(s2));
        }
    }
    ofstream file;
    file.open("output.txt");
    while (s3 != NULL)
    {
        file << pop(s3) << " ";
    }
    file.close();

}
int main()
{
    stack Stack1 = NULL;
    stack Stack2 = NULL;
    file_input("File1.txt", Stack1, Stack2);
    cout << "\n\nSorting...\n";
    sort(Stack1, Stack2);
    cout << endl << endl;
    return 0;
}

You'll notice I refactored the pop function a little. You'll find it more intuitive and easier to understand if you assign the new values to p then assign p to S.

Thank you very much but when i run it it show error in input file
in console i see
Error

Soring...

Press any key to continue...

How to fix this? I am using Visual Studio 2013

Do you use 3 stacks? I see s3

assuming the data for each stack is on separate lines in one file

Is the file formatted like this?

a third stack will be the easiest way to get the sort order you want:

Using LIFO(Last In First Out) stacks, every time you input data it comes out reversed. Without the third stack the data in the output file will be in descending order instead of ascending order.

I dont know how to do this

copy and paste this into a new file:

1,3,5,9,10
2,3,4,6,7,11

It is the same error! Can I create input file and input numbers in 2 stacks by myself? For exampla when i run it:

Please input numbers for s1:......
     .....           for s2:....
Sorting....
/Sorted stacks/

You could, but first you should check the spelling of the file name you're passing to the file_input function. If you're using my example and your file isn't named "File1.txt" then you'll get the error message.

Yes my file is file1.txt it is in the same folder but i get error again-In which folder should be file1.txt i put in in console appliation folder and it dont work-Do you try to run in your visual studio?

Yes I use VS. The text file should be in the project folder. The easiest way to get it in the right place is to add a new text file to the project. For c++ the text file template is in the4 Utility folder. Then copy and paste the data into this file.

I do it but the same error. Can you send me somehow your project? Or i dont know why i get the same error when your is the same and u dont get error? I will give you e-mail or something

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.