Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

how to input Data Files into Dev C++

Options
  • 11-05-2007 5:48pm
    #1
    Registered Users Posts: 53 ✭✭


    Hi lads, I'm after getting Dev C++ on the PC and I'm used to writing programmes on the Macs. I am unable to input data files into my programmes, when i compile and run them the window opens but i cant input them, any suggestions :confused:


Comments

  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Hmm a bit of code would help :)

    take this for example should work:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
      
      string mystring;
    
      cin >> mystring;
    
      cout << "I inputed" << mystring << endl;
      return 0;
    }
    


  • Registered Users Posts: 53 ✭✭Mike Funnelly


    this is the code here,

    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <string>
    using namespace std;

    void read_names(string names[], int& names_size, int capacity);
    //void print_names(string names[], int names_size);
    void read_votes(int votes[][10], string names[], int names_size);
    void print_votes(int votes[][10], string names[], int names_size);
    int get_index_of_name(string names[], int names_size, string find_name);

    void sort_names(string names[], int names_size);
    void swap_names(string names[], int index1, int index2);
    int index_of_greatest_name(string names[], int names_size);


    int main()
    {
    int size;
    int capacity = 100;
    string names[capacity];


    read_names(names, size, capacity);
    // print_names(names, size);

    int votes[size][10];
    read_votes(votes, names, size);
    print_votes(votes, names, size);

    system("Pause");return 0;
    };

    void read_names(string names[], int& names_size, int capacity)
    {

    string sentinel = "xxx";
    names_size = 0;
    string input;
    cin >> input;
    while((names_size < capacity)&&(input != sentinel))
    {
    names[names_size] = input;
    names_size++;
    cin >> input;
    }
    }

    void read_votes(int votes[][10], string names[], int names_size)
    {
    int sentinel = -1;
    int input_ward;
    cin >> input_ward;
    while(input_ward != sentinel)
    {
    input_ward--;
    string input_name;
    cin >> input_name;
    int index;

    index = get_index_of_name(names, names_size, input_name);

    int input_votes;
    // if ((index!=-1)&& (votes[index][input_ward]==0))
    //{
    cin >> input_votes;

    //cout << index;
    //cout << input_ward;
    // cout << input_votes;

    // cout << "========" << endl;

    votes[index][input_ward] = input_votes;
    // }
    cin >> input_ward;

    }
    }

    /*void print_names(int names[], int names_size)
    {

    for(int i=0; i<names_size; i++)
    {
    cout << names;
    }

    }*/

    int get_index_of_name(string names[], int names_size, string find_name)
    {

    int index = -1;
    for(int i=0; i<names_size; i++)
    {
    if(names == find_name)
    {
    index=i;
    }
    }
    return index;
    }

    void print_votes(int votes[][10], string names[], int names_size)
    { int total =0;

    for(int i=0; i<names_size; i++)
    { cout << names<<":";
    for(int j=0; j<10; j++)
    {
    cout << votes[j] << " ";
    total= total+votes[j];
    }
    cout << "total" <<total;
    cout << endl;
    }

    }



    The input file would be more or less an array, here's some of it below

    Green
    Brown
    Jones
    Murray
    Kelly
    Ahern
    xxx
    6 Kelly 321
    6 Ahern 523
    7 Green 123
    7 Brown 412
    1 Green 99
    2 Murray 84
    2 Kelly 315
    2 Ahern 158
    3 Green 97
    3 Brown 612
    3 Jones 178
    4 Murray 609
    4 Kelly 241
    4 Ahern 109
    5 Green 410
    5 Brown 306
    5 Jones 145
    5 Murray 142
    5 Kelly 290
    1 Brown 305
    1 Jones 471
    1 Murray 521
    1 Kelly 362
    8 Murray 831
    8 Kelly 720
    8 Ahern 432
    9 Green 764

    but the problem i'm having is inputting the data into it. I had it running on the macs in the college, with Sub Etha Edit and the Terminal, but i'm trying to run it on the pc and the black window pops up after i compile and run the program but i just don't know how to input the data from the saved file.


    the programme should out out put the results of a vote and each candidates name.


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Why don't you create a struct array with data of this:
    struct Votes {
    string name;
    int votes;
    };
    
    

    Then in the main create an array:
    int size = 0;
    Votes votearray[100];
    
    ifstream inputfile;
    inputfile.open("myfile.txt");
    
    if (inputfile.fail())
     cerr << "Error opening file";
    
    for (;;)
    {
    
    inputfile >> Votes[size].name >> Votes[size].votes;
    
    size++;
    
    if (inputfile.eof())
     break;
    }
    

    And have file data like:

    4 Ahern
    5 Green
    5 Brown
    5 Jones
    5 Murray
    5 Kelly

    Then you can simply iterthrough the array like:
    int totalvotes = 0;
    for (int k=0; k < size; k++)
    {
     totalvotes+= Votesarray[k].vote;
     cout << "Name: Votesarray[k].name << " Votes: " << Votesarray[k].votes << endl;
    }
    cout << "\nTotal Votes: " << totalvotes << endl;
    

    I dunno could be reading into this all wrong but the way you doing it seems kinda awkward and confusing


  • Registered Users Posts: 53 ✭✭Mike Funnelly


    its not a problem i'm having with the code, it's just i don't know how to get the Dev C++ to read data from a saved file


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Well the thing i posted before had showed how to deal with reading in data. The ifstream stuff. I don't know why you saying reading in with DevCPP as that just an interface for a C++ compiler and C++ is a standard - Any compiler will be the same (up to a certain degree)


  • Advertisement
  • Registered Users Posts: 981 ✭✭✭fasty


    When you use Terminal to compile your code on a Mac what commands do you type?

    And then to run the program what do you type?


  • Registered Users Posts: 53 ✭✭Mike Funnelly


    To make

    make programmename.cpp

    to run

    ./programmename

    is it something similar on the pc?:confused:


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Well with devcpp , that looks after all the compiling via the interface. Guess you can run parameters too, what libraries to include etc. You simple run by executing the executable (by double clicking it).


Advertisement