store data,read specific lines of data from a file and again store them separately into variables to be used further in program

I am working on a management system in which the user inputs some integers for different prices of items and gets the output.I need to store them in a file and then show the user the review of his week. How can i read a specific line containing lots of numbers separated by white spaces? After reading how can i store the numbers in variables again to show them to the user?
Last edited on
If you want help, you need to put more effort into your post. Show us the code you have attempted at least.
Seems like a pretty straight forward assignment.
Show us some code and I'll be able to help.
I am a beginner so any kind of help is appreciated
if you think i should use some other approach then guide me.
What i could come up with was to store all the integers in a string then store the pair of the date and string in the file and by using search algorithm find the date and get the required string. But i can figure out the syntax. IS there any other better and easy approach to do it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int no_of_hen=0;
		int price_per_hen = 0;
		int	Food_hen = 0;
		int	Shelter_hen = 0;
		int	Health_hen = 0;
		int	offsprings_hen = 0;
		int	no_of_eggs = 0;
		int	Egg_rate = 0;
		int	hens_died = 0;
int total_food = 0;
		int total_shelter = 0;
		int total_health = 0;
		int total_offspring = 0;
		int total_sell_output = 0;
		int output = 0;
		int input = 0;
		int profit_loss = 0;

//will get input from user then and generate outputs to be stored in above variables

//i was thinking of converting them to a string and them store the string as daily data
string s1 = to_string(Price_per_hen);
string s2 = to_string(Food_hen); // do this for all variables
string s3 = s1+s1; // // do this for all variables
ofstream File("File.txt", ios :: app); 
getline (s3, File);
File >> s3;
/* but when user runs program , inputs and closes the program daily
    how am i supposed to get all the data for him to review his week? */
Last edited on
I would approach this a tad differently.
Read this http://www.cplusplus.com/doc/tutorial/structures/ and look at the example program. Do you see how a data structure can help you solve this problem?



Here is some code to get you going. (one way this problem can get solved using structs)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

using namespace std;
struct Item {
    string name = "";
    double cost = 0;
    int qty = 0;

    double buy() {
        if (qty > 0) {
            qty--;
            cout << "Thank you! You paid " << cost << " dollars for a " << name << "." << endl;
        } else {
            cout << "No more " << name << "We are out of stock" << endl;
            cost = 0;

        }
        return cost;
    }
};
struct Account {
    string owner = "";
    double funds = 10000.00;

    void debit(double amount) {
        funds = funds - amount;
    }
};

int main() {
    Item item[50];
    item[0].name = "Food Hen";
    item[0].cost = 5.5;
    item[0].qty = 6;
    Account myAccount;
    myAccount.owner = "Me";
    cout << myAccount.funds << endl;
    myAccount.debit (item[0].buy ());
    cout << myAccount.funds << endl;
    return 0;
}


let us know if you have any questions.
Thank you for your help Bdanielz!
Topic archived. No new replies allowed.