I'm facing problem in giving input from a different input.txt file

#include<bits/stdc++.h>
using namespace std;

class node{

public:
int data;
node *next;

node(int d)
{
    data=d;
    next=NULL;
}

};

void insertAtHead(node *&head,int d)
{
//cout<<" d “<<d<<” "<<endl;
if(head==NULL)
{
head=new node(d);
return;
}
else{
node *tem=new node(d);
tem->next=head;
head=tem;
return;
}
}

void print(node *head)
{

node *tem=head;
while(tem->next!=NULL)
{
    cout<<tem->data<<"->";
    tem=tem->next;
}
cout<<tem->data<<endl;

}

node *takeInput()
{
int d;
node *head=NULL;
while(cin>>d)
{
cout<<d<<endl;
insertAtHead(head,d);
}

return head;

}

int main()
{
node *head=takeInput();

print(head);

}

You can share this code using ide.codingblocks.com

  • A special url will be generated, share that url with me

I am having problem with takeinput() function

when running on vscode in input.txt file i’m giving inputs
like:-
1
3
4
5
6
7

and in terminal i gave inputfile path

check now ->


it will take input until the input number is -1


sir in these screenshots you can see when i’m giving inputfile.txt in terminal it gave continues 0 in output

that might be some issue in your software installation, run this program on ide and see, it’s running fine on ide.

sir if i’m giving input numerically then their is no problem but when i’m giving it as file then problem arise

might be some file system issue, i am a mac user so don’t know what error is coming in your compiler. Try to search it on stack overflow

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

1 Like