Pointers and reading in .txt file

So we're supposed to use pointers to allocate memory and addresses of the variable, reading a text file on each line which begins with a char corresponding to integer, double, and string, printing the data and it's location and adding it to a sum or concatenation. Then printing the final sums and concatenation and their addresses.
First: Am I using "new" and "delete" in the wrong places?
Second: Before I had new and delete I was setting *iPtr = &IntSum, which is wrong, but I couldn't get my program to read the char and properly print the corresponding output (ie, the line where the char is m, doesn't output the error message, and when I only had the if statement with 'i' written it would print all of the text)
Third: How do I print the address of the sums and concatenation if they aren't associated with the pointers

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
  #include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
	char a;
	ifstream clab12;
	clab12.open("clab12.txt");

	//variables to hold information
	string strHold = "";
	int intSum = 0;
	double doubSum = 0;
	
	//pointers
	string * sPtr = new string;
	int * iPtr = new int;
	double * dPtr = new double;

	
	while(clab12>>a)
		{
		if (a='s')
		{
			clab12>>*sPtr;
			cout<<*sPtr<<"\t"<<sPtr<<endl;
			strHold = strHold+*sPtr+" ";
			delete sPtr;
		}

		else if (a='i')
		{
			clab12>>*iPtr;
			cout<<*iPtr<<"\t"<<iPtr<<endl;
			intSum = intSum + *iPtr;
			delete iPtr;
		}

		else if (a='d')
		{
			clab12>>doubSum;
			cout<<*dPtr<<"\t"<<dPtr<<endl;
			doubSum = doubSum + *dPtr;
			delete dPtr;
		}

		else
		{
			cout<<"Data type is not correct"<<endl;

		}
		/* Output sums and addresses of sums???
		cout<<"Sum----------Address"<<endl;
		cout<<strHold<<endl;
		cout<<intSum<<endl;
		cout<<doubSum<<endl;
		*/
		}
	
	
	clab12.close();

	return 0;
}


the text file clab12.txt is as follows
i 800
d 2.75
m gxxz
i 25
i 17
s April
s Showers
d 0.03
s Bring
s May
b 2.19
s Flowers
q
Well, this assingment doesn't make overly sense:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
	while(clab12>>a)
		{
		if (a=='s') // Note: = assign / == compare
		{
	string * sPtr = new string; // Note: moved from line 18
			clab12>>*sPtr;
			cout<<*sPtr<<"\t"<<sPtr<<endl;
			strHold = strHold+*sPtr+" ";
			delete sPtr;
		}

... // Note: Change the other else branches like above

		else // Note: you need an else for 'q'
		{
			cout<<"Data type is not correct"<<endl;

		}
		}
// Note: move it out of the loop
		cout<<"Sum----------Address"<<endl;
		cout<<strHold<<"\t"<<&strHold<<endl;
...
Okay it worked out! Thank you for your help!
Topic archived. No new replies allowed.