0

I am creating a program to add large numbers in C++.

I want to take the number from a file and store it in a linked list, but if there is a decimal I want to just keep the record of index of the dot and not store it into the list.

So, when the while loop encounters a dot, it just keeps the index and skips to the next one.

For some reason, my code gets stuck in infinite loop. Here is my code:

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

    struct node
    {
     int data;
     node *next;
    };
   int main(int argc, char *argv[]){
    int digit;
    node * head1 = NULL;
    node * tail1 = NULL;
    node * temp1 = NULL;
    ifstream fStream;
    fStream.open(argv[1]);
    while(!fStream.eof()){
    fStream >> digit;
    if(!isdigit(digit))
      {
        fStream >> digit;
         cout <<"done";
       }
      //fStream >> digit;
      temp1 = new node();
      temp1->data = digit;
      temp1->next = head1;
      head1 = temp1;
    }
    fStream.close();
    node * head2 = NULL;
    node * tail2 = NULL;
    node * temp2 = NULL;
    ifstream fStream1;
    fStream1.open(argv[2]);
    cout<<argv[2]<<endl;
    while(!fStream1.eof()){
     fStream1 >> digit;
     temp2 = new node();
     temp2->data = digit;
     temp2->next = head2;
     head2 = temp2;
    }
    fStream1.close();
    while(temp1!=NULL){
    cout <<temp1->data<<" ";
    temp1 = temp1->next;
    }
    cout<<endl;
    while(temp2!=NULL){
    cout <<temp2->data<<" ";
    temp2 = temp2->next;
    }
    cout<<endl;
    }

Can anyone help?

Cole
  • 431
  • 3
  • 15
  • 5
    I recommend you take some time to read [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Some programmer dude Nov 18 '17 at 23:01
  • Also, the [`std::isdigit`](http://en.cppreference.com/w/cpp/string/byte/isdigit) checks if a *character* is a digit. You read *integers*, which will only be "digits" if they are equal to the current encoding for the characters (in the case of [ASCII](http://en.cppreference.com/w/cpp/language/ascii) is the *numbers* 48 to 57). – Some programmer dude Nov 18 '17 at 23:03
  • 1
    When you read into an `int`, like in `fStream1 >> digit;` it will not read the dot, because that cannot be part of an integer. And when you loop and try to read again, the dot is still there and will not be read, ever. So then you are stuck there and will never reach the end of file. – Bo Persson Nov 18 '17 at 23:55
  • How do I move to the next digit other than . – Gurpyar Brar Nov 19 '17 at 00:51
  • @BoPersson How do I move to the next digit. – Gurpyar Brar Nov 19 '17 at 01:08

0 Answers0