1

I have a problem with the code below. Here, I want to write a program which will take input from a file and store it in a vector of structure, but when I declare a structure type vector it is showing an error.

#include<iostream>
#include<fstream>
#include<vector>

using namespace std;

struct input
{
    char process[10];
    int burst_time;
    int arrival_time;
}input;

int main()
{
    ifstream myfile;
    vector<input> store;// problem with initializing vector

    myfile.open("input.txt",ios::in);

    if(myfile.is_open())
    {
        while(!myfile.eof())
        {
            myfile>>input.process;
            myfile>>input.burst_time;
            myfile>>input.arrival_time;

            store.push_back(input);//showing problem in this line also
        }
    }

    myfile.close();
    return 0;
}
Gray
  • 7,050
  • 2
  • 29
  • 52
  • 3
    Side note: [Don't do `while(!myfile.eof())`](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong#comment18550194_5605125). – jrok Oct 15 '13 at 13:06
  • Also, it might be safer to use an `std::string` for `process` instead of a fixed-length `char` array. – juanchopanza Oct 15 '13 at 13:18
  • 1
    For future reference, when you are asking for help on StackOverflow about an error, be sure to include the error message in your question for quick debugging. – Gray Oct 15 '13 at 13:31
  • In any case: what does the string in `process` look like? Inputting strings can be tricky, because they don't naturally separate; you generally need some parsing. (And of course: what are the contents of `"input.txt"`?) – James Kanze Oct 15 '13 at 13:58
  • input.txt is like the format below. p1 3 4 (line by line each value separated) – Shaswata Shaha Oct 15 '13 at 14:03

2 Answers2

7

You have hidden the name input to be an instance of struct input. Un-hide it:

struct intput
{
 // as before
};
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • but this code still showing problem.... it is giving problem on the first three lines of inside while loop – Shaswata Shaha Oct 15 '13 at 13:39
  • @ShaswataShaha that is a different issue, you may be better off asking a separate question. – juanchopanza Oct 15 '13 at 13:51
  • after fixing it as suggested the code is showing problem in other portion..is it wise to ask another question for that. can someone fix the whole code as i described what i really wanted to do with my code here.i am new in c++ so really stuck here – Shaswata Shaha Oct 15 '13 at 14:00
  • @ShaswataShaha Don't expect people to find out what problems exist in your code and fix them for you. Identify your issues as precisely as possible (e.g. error messages) and ask specific questions, then maybe you'll be shown how to fix them. – iavr Oct 15 '13 at 14:21
  • @ShaswataShaha I think you need to post a different question, explaining what you expect, what happens, and what errors you get. – juanchopanza Oct 15 '13 at 17:26
0

This is the thing, very simple:

When you declare

struct input
{
    char process[10];
    int burst_time;
    int arrival_time;
} input;

you are defining a struct type named input but also a variable named input, so in the main the compiler gets confused, it doesn't know if you refer to variable or type, so just rename the struct variable declaration and refer to it as its own name like in this:

#include<iostream>
#include<fstream>
#include<vector>

using namespace std;

struct input // Type defined as "input"
{
    char process[10];
    int burst_time;
    int arrival_time;
} input1; // And variable defined as "input1" consider this to be declared in main, to be a local variable.

int main()
{
    ifstream myfile;
    vector<input> store{};// problem solved referring to type and not variable

    myfile.open("input.txt",ios::in);

    if(myfile.is_open())
    {
        while(!myfile.eof())
        {
            myfile>>input1.process; // Here now the compiler understands that you are referring to the variable and not the type
            myfile>>input1.burst_time;
            myfile>>input1.arrival_time;

            store.push_back(input1);
        }
    }

    myfile.close();
    return 0;
}

This way the compiler will not complain.

Consider also always you declare a new type (like your structure) with first character as upper case. Input instead, and variables with first character lower case input to not get confused and avoid this kind of mistakes.

Ibrahim CS
  • 119
  • 2
  • 9