Reading from a .txt file or use arrays.

This is a discussion on Reading from a .txt file or use arrays. within the C++ Programming forums, part of the General Programming Boards category; Hi, i have a question on my code im doing. Im trying to make a math program that lets the ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    2

    Unhappy Reading from a .txt file or use arrays.

    Hi, i have a question on my code im doing. Im trying to make a math program that lets the user input how many numbers he/she would like to use to Add, Subtract, etc..... I am currently using the <fstream.h> header file and i am opening a txt file called variables. Then it figures through the loop how many numbers the user wants like (Ex. if the user inputs 5 then the loop wil count all the way to 5 like this. 1,2,3,4,5 and add spaces between them.) But now i want to know how to read these numbers from the text file and create that many variables or like this (Ex. reads 1,2,3,4,5 and creates int 1, int 2, int 3, int 4, int5; ).
    I would like to know how to do that though. but im pretty sure you can use arrays and if they are esier to use then tell me how, otherwise tell me how to do that with the variables file. Heres my code so far......

    ***************************

    #include <iostream.h>
    #include <stdio.h>
    #include <fstream.h>

    void main()

    {
    int Amt;
    int i=0;
    char str[50];

    {
    cout<<"Welcome to the math program.\n";
    cout<<"How may numbers do you want to use?";
    cin>>Amt;

    }
    ofstream a_file("variables.txt");

    while(i<Amt)

    {
    i++;
    a_file<<i<<" ";
    }

    a_file.close();

    }

    *****************************

    Any help would be great,
    Thanks,
    Joe
    Attached Files Attached Files

  2. #2
    31173 h4x0r gnu-ehacks's Avatar
    Join Date
    Nov 2001
    Posts
    556
    If you keep reading the file until the value becomes null, it is possible. You just have to keep looping with the condition that the value == NULL, to stop the reading.
    What will people say if they hear that I'm a Jesus freak?
    What will people do if they find that it's true?
    I don't really care if they label me a Jesus freak, there is no disguising the truth!

    Jesus Freak, D.C. Talk

    -gnu-ehacks

  3. #3
    Registered User ski6ski's Avatar
    Join Date
    Aug 2001
    Posts
    133
    With the code provided in your post, you can do what you have done without the fstream. Here let me revise it for you.
    Code:
    #include <iostream.h> 
    #include <stdio.h> 
    
    /*void*/ int main() //do not use void.  Read the FAQ on why
    { 
         int Amt; //total numbers to loop
         int i=0;  //starting point
         char str[50]; //an array
    
         { 
              cout<<"Welcome to the math program.\n"; 
              cout<<"How may numbers do you want to use?"; 
              cin>>Amt; //user input for loop count
         } 
    
         while(i<Amt) 
         { 
              i++; 
              cout<<"adding 1 to "<<i<<endl;
         } 
    }
    C++ Is Powerful
    I use C++
    There fore I am Powerful

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    You can use the new and delete keywords to allocate arrays dynamically:

    PHP Code:
    #include <iostream.h> 

    int main()
    {

        
    intvariables NULL
        
    int  count;
        
    int  i;
        
    int  sum;

        do
        {
            
    cout << "How many variables would you like to enter ? ( 1+ )";
            
    cin >> count;
        }
        while( 
    count <= );

        
    // now count is a positive number

        
    variables = new int[count];

        if( 
    variables == NULL )
        {
            
    cerr << "Error, not enough RAM" << endl;
            return 
    1;
        }

        
    // read variables into array
        
    for( count i++ )
        {
            
    cout << "Enter variable #" << i+<< ":";
            
    cin >> variables[i];
        }

        
    sum 0;

        
    // add them all 
        
    for( count i++ )
        {
            if( 
    != cout << "+";

            
    cout << variables[i];

            
    sum sum variables[i];
        }

        
    cout << " = " << sum << endl;
        
        if( 
    variables != NULL delete [] variables;

        return 
    0;

    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Reading char arrays from a binary file
    By eaane74 in forum C++ Programming
    Replies: 17
    Last Post: 11-20-2007, 04:19 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  4. reading file and storing to arrays
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 04-27-2006, 06:17 AM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM

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