0

So I have a txt file that looks like this.

Input1.txt

!
awesome is

Joseph
guess I

the main point is to create a code that will rearrange the inputs into a readable sentence like this

answ1.txt

I guess Joseph is awesome !

I have my code here

lab.txt

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



struct nodeinfo
{
    string word;
    nodeinfo * next;
};


void add (string inputword, nodeinfo ** ref)
{ 
    //create new node
    nodeinfo * temp = new nodeinfo();
    nodeinfo * temp2 = *ref;

    //word into node
    temp ->word = inputword;
    //since its LIFO, temp is last so its next has to be NULL
    temp->next = NULL;

    //if list is empty, then the new node is the head
    if (*ref == NULL)
    {
        *ref = temp;
        return;
    }
    //if not empty, we go to last node
    //change second to last to temp
    while(temp2->next!=NULL)
    {
        temp2 = temp2->next;
    }
    temp2->next = temp;
    return;

}

void rewind(nodeinfo * lead)
{
    if (lead == NULL)
    {
        return;
    }
    rewind(lead->next);
    cout << lead ->word<< " ";


}

int main(int argc, char *argv[])
{
    
    if (argc >=1)
    {
    freopen("output.txt", "w", stdout);
    fstream file;
    string filename;
    filename = argv[0];
    string word;
    nodeinfo *head = NULL;
    file.open(filename);
    while(file >> word)
    {
        //pass ref pointer and word
    add(word, &head);
    }
    rewind(head);
    return 0;
    }
}

   

if I manually input my input file name as "input.txt" in my code like this

file.open("input1.txt")

it will work, but I am having a hard time using arc and argue to open my file and do what I want it to do.

ideally, I would call my program to work like this

./input1.txt 

can anyone offer me solutions and what not?

  • 2
    It's probably just a typo. If `argc >= 1` , then `argv[0]` is the name of the program that's being called and `argv[1]` would be the first parameter. See also: https://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean – Lukas-T Sep 23 '20 at 16:50
  • This doesn't address the question, but get in the habit of initializing objects with meaningful values rather than default initializing them and later overwriting the default values. In this case, that means removing `fstream file;` and changing `file.open(filename);` to `fstream file(filename);`. – Pete Becker Sep 23 '20 at 17:50

0 Answers0