File I/O, Vectors, and Classes

Hi there,

So, I am lost and stuck on how to do this project. One of the issues I'm having is that the code iNumbers.push_back(new Integer("12")); is not working. I am getting the error "cannot convert from 'Integer +' to 'const int'.

Also, I don't exactly know how to parse the numbers and separate them into two text files. Could someone help me and point me to the right direction.
Any insight on this would be greatly appreciated!

The attached Text file http://programming.msjc.edu/cppii/labs/labsb/lab09/Numbers.txt contains both whole and floating point numbers. Of course they are in text format and not binary. Your task is to read and parse the numbers and separate them into two text files double.txt and integer.txt.

Specifics
Open the text file and read its contents one line at a time. Determine if the line read from the file is a double or an integer. You are to place the integers in a vector called iNumbers and the doubles in a vector called dNumbers. The vector iNumbers should hold pointers to the Integer class and dNumbers should hold pointers to the Double class. When you add a value to one of the vectors you should use new and call the constructor that takes a string. For example:


iNumbers.push_back(new Integer("12.23"));

Once you have the file parsed create an overloaded writeNumbers function. One of these functions should take a vector of pointers to the Integer class, the other should take a vector of pointers to the Double class. These functions should write the contents of the vector to the appropriate text file. When you are finished you should have two files one that holds int values, the other that holds double values.

Note:
You created a toString function and added to your classes. You should use this function to write the string to the text file.

main.cpp
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
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <vector>
#include "Integer.h"

using namespace std;

int main()
{
	ifstream ifile("Numbers.txt", ios::in);
	vector<int>iNumbers;

	if (!ifile.is_open())
	{
		std::cerr << "There was a problem opening the input file!\n";
		exit(1);
	}

	int integer = 0;

	while (ifile >> integer)
	{
		iNumbers.push_back(new Integer("12"));
	}

	for (int i = 0; i < dNumbers.size(); ++i)
	{
		std::cout << dNumbers[i] << std::endl;
	}
	
	return 0;
}


Integer.h
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
#ifndef INTEGER
#define INTEGER

#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>

using std::string;

class Integer
{
private:
	int data;
public:
	Integer();
	Integer(const Integer &i);
	Integer(int i);

	string toString();
	void equals(string s);
	bool isNaN(const string s) const;
	Integer(string s);

	void equals(int i);
	int toInt() const;
};

#endif 


Integer.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "Integer.h"

using namespace std;

Integer::Integer()
	:data(0)
{

}
Integer::Integer(const Integer &i)
{
	equals(i.toInt());
}

Integer::Integer(int i)
{
	equals(i);
}

Integer::Integer(string s)
{
	this->equals(s);
}

void Integer::equals(int i)
{
	data = i;
}

int Integer::toInt() const
{
	return data;
}

string Integer::toString()
{
	stringstream ss;
	ss << data;
	string newString = ss.str();
	cout << "The string equals " << newString << endl;

	return newString;
}

void Integer::equals(string s)
{
	if (isNaN(s) == true)
	{
		cout << "Cannot assign a character to Class Integer" << endl;
	}
	else if (isNaN(s) == false)
	{
		cout << "Valid number" << endl;
		equals(stoi(s));
	}
}

bool Integer::isNaN(const string s) const
{
	int points = 0;
	string::const_iterator it = s.begin();

	while (it != s.end())
	{
		if (isdigit(*it))
		{
			it += 1;
			continue;
		}
		else if (*it == '.')
		{
			it += 1;
			points++;
			continue;
		}
		else
			return true;
	}

	if (points >= 2)
		return true;
	else
		return false;
}
Last edited on
You have a vector if ints, but your class does not have any conversion operator, so the error is telling you that.

Maybe try declaring iNumbers to hold
pointers to the Integer class
instead of just ints.
Topic archived. No new replies allowed.