Reading in an unknown number of numbers

This is a discussion on Reading in an unknown number of numbers within the C++ Programming forums, part of the General Programming Boards category; I hit a little barrier while trying to implement an RPN calculator today. I want to have expressions like this ...

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Reading in an unknown number of numbers

    I hit a little barrier while trying to implement an RPN calculator today. I want to have expressions like this in a file:

    Code:
    33 10 +
    But that could be any number of numbers and I have no idea how to read them in.

    Code:
    int main (int argc, char *argv[])
    {
            FILE            *f = 0;
            int             ch = 0;
            Stack           *stk = new Stack (256);
    
            f = fopen ("z:\\test.rpn", "r");
            if (! f) {
                    cout << "Error opening file\n";
                    return 1;
            }
    
            while ((ch = fgetc (f)) != EOF) {  // The offending line
                    switch (ch) {
                            case '+': {
                                            char    one, two, ans;
    
                                            one = stk->pop ();
                                            two = stk->pop ();
                                            ans = one + two;
                                            stk->push (ans);
    
                                            break;
                                      }
                            case '-':
                                    break;
                            case '/':
                                    break;
                            case '*':
                                    break;
                            default: // assumed to be a literal to push
                                    stk->push (ch);
                    }
            }
    
            fclose (f);
            delete stk;
             return 0;
    }
    I don't have a clue how to read in an unknown number of numbers, as the title suggests.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    from the tutorial:

    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char str[10];
    
      //Creates an instance of ofstream, and opens example.txt
      ofstream a_file ( "example.txt" );
      // Outputs to example.txt through a_file
      a_file<<"This text will now be inside of example.txt";
      // Close the file stream explicitly
      a_file.close();
      //Opens for reading the file
      ifstream b_file ( "example.txt" );
      //Reads one string from the file
      b_file>> str;
      //Should output 'this'
      cout<< str <<"\n";
      cin.get();    // wait for a keypress
      // b_file is closed implicitly here
    }
    put the reading from the file in a loop terminating at eof (or your own terminator), convert the read data to integers, and away you go.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    But that could be any number of numbers
    The data is delineated by spaces, so you can read them in one at a time until there aren't anymore. The most efficient way to read them in is by reading in the whole line at one shot, and then using your program to separate out the pieces. Because you have numbers and characters intermixed in the data, you have to read the whole thing in as a string, so the trick is being able to convert the pieces of the string that are numbers to numeric types. In C++, you can use stringstreams to do that, e.g.
    Code:
    #include <iostream>
    #include <string> //string type, find()
    #include <cctype> //isdigit()
    #include <sstream> //istringstream
    
    using namespace std;
    
    
    int main()
    {
    	string data = "30 10 + 20 -";
    
    	int start = 0;
    	int pos = data.find(" ", start);
    
    	string piece = data.substr(start, pos - start);
    	
    	double theNum = 0.0;
    	if(isdigit(piece[0]))
    	{
    		istringstream inStr(piece); //creates a "stringstream" from which 
                                                //you can read data into any type using >>
    		inStr>>theNum;
    	}
    
    	cout<<theNum<<endl;
    	cout<<theNum + 10.0<<endl;  //to prove theNum is a numeric type
    
    	return 0;
    }
    Last edited by 7stud; 01-31-2006 at 03:06 PM.

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    When you eventually get this far...

    Calculator program

    Good luck

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 11:13 AM
  3. question: reading numbers into an array
    By Lince in forum C Programming
    Replies: 15
    Last Post: 11-15-2006, 03:41 AM
  4. reading a text file printing line number
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-16-2005, 11:31 AM
  5. reading a number from a file
    By the_head in forum C Programming
    Replies: 2
    Last Post: 10-02-2003, 10:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21