Hello i need some help to understand my work heres the code

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <fstream> 
using namespace std; 
struct cell1
{    
	int value1;
}
cell1Data;
struct cell2
{    
	int value2;
}
cell2Data;
int main(void)
{
	ifstream myfile("example.txt");    
	if(!myfile.good()) 
	return 0;     
	vector<cell1> data1;
    vector<cell2> data2;
	string line1;
    string line2;
	istringstream iss1;
	istringstream iss2;
	while(getline(myfile, line1))
	{      
		for(int a = 0; a < line1.length(); a++) 
		{
			if(line1[a] == ';')
			{
				line1.erase(a, line1.length());
			}
		}
		iss1.clear();        
		iss1.str(line1);        
		iss1 >> cell1Data.value1;        
		data1.push_back(cell1Data); 
	}
	while(getline(myfile, line2))
	{      
		for(int b = 0; b < line2.length(); b++) 
		{
			if(line2[b] == ';')
			{
				line2.erase(b, line2.length());
			}
		}
		iss2.clear();        
		iss2.str(line2);        
		iss2 >> cell2Data.value2;        
		data2.push_back(cell2Data); 
	} 
	for(int c = 0; c < data1.size(); c++)
		cout<<data1[c].value1<< endl; 
	for(int d = 0; d < data2.size(); d++)
		cout<<data2[d].value2<< endl;
	return 0;
}

the problem is when i use the first while loop i cant make the other one to work so i dont know whats affecting what really since (i think) i doubled everything

Recommended Answers

All 4 Replies

what i really wnat to do in the while loops is to filtrate so in the first one i'll want the first part of the text in one array and then the next part in one array but i put same code in there too test whats wrong :)
so the other while loop should erase the first part of the text but i'll handle that later

Post a sample of your input file and a sample of what your output should be, and what output you are getting please.

Chris

well heres the input

5623;62234
12;3432
14;74
90;59223
05;23
92;39
44;11123
87;99
01;34
45;88
55;53433

the output should be like

5623
12
14
90
05
92
44
87
01
45
55

in one of them cause in the real text later its different values i'd like to compare later on

62234
3432
74
59223
23
39
11123
99
34
88
53433

the other part

Sounds like you actually want something like this....

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

int main(int argc, char *argv[]){
    ifstream example("Exmaple.txt");
    string line;
    istringstream iss;
    vector<int> part1;
    vector<int> part2;
    int temp1, temp2;
    
    while(getline(example, line)){
                           line.replace(line.find(";"), 1, " ");
                           iss.clear();
                           iss.str(line);
                           iss >> temp1 >> temp2;
                           part1.push_back(temp1);
                           part2.push_back(temp2);
    }
    example.close();
    
    cout << "Part 1:" << endl;
    for(int i = 0; i < part1.size(); i++)
            cout << part1[i] << endl;

    cout << endl << "Part 2:" << endl;
    for(int i = 0; i < part2.size(); i++)
            cout << part2[i] << endl;
    
    return 0;
}
commented: tight clean code +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.